GANS with Pytorch

username: manu317
cooked: <p>class Discriminator(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.model=nn.Sequential(<br>
nn.Linear(392*384,1024),<br>
nn.ReLU(),<br>
nn.Dropout(0.5),<br>
nn.Linear(1024,512),<br>
nn.ReLU(),<br>
nn.Dropout(0.5),<br>
nn.Linear(512,256),<br>
nn.ReLU(),<br>
nn.Dropout(0.5),<br>
nn.Linear(256,1),<br>
nn.Sigmoid()<br>
)</p>
<pre><code>def forward(self,x):
    x=x.view(x.size(0),672)
    output=self.model(x)
    return output
</code></pre>
<p><span class="hashtag">#generator</span><br>
class Generator(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.model=nn.Sequential(<br>
nn.Linear(1000,224),<br>
nn.ReLU(),<br>
nn.Linear(224,448),<br>
nn.ReLU(),<br>
nn.Linear(448,672),<br>
nn.Tanh())</p>
<pre><code>def forward(self,x):
    x=x.view(x.size(0),224)
    output=self.model(x)
    return output
</code></pre>
<p>loss_function=nn.BCELoss()<br>
optimizer_discriminator=torch.optim.Adam(discriminator.parameters())<br>
optimizer_generator=torch.optim.Adam(generator.parameters())</p>
<p>discriminator=Discriminator()<br>
generator=Generator()</p>
<p><span class="hashtag">#Training</span> the model</p>
<p>batch_size=1000<br>
num_epochs=10<br>
for epoch in range(num_epochs):<br>
for n,(real_samples,Labels) in enumerate(train_set):<br>
real_samples=real_samples<br>
real_sample_labels=torch.ones((batch_size,1))</p>
<pre><code>    latent_heat_samples=torch.randn((batch_size,224))
    
    generated_samples=generator(latent_heat_samples)
    generated_sample_label=torch.zeros((batch_size,1))
    
    all_samples=torch.cat((real_samples,generated_samples))
    all_sample_labels=torch.cat((real_sample_labels,generated_sample_label))
    
    #training discriminator
    
    optimizer_discriminator.zero_grad()
    discriminator_samples=discriminator(all_samples)
    loss_discriminator=loss_function(discriminator_samples,all_sample_labels)
    
    loss_discriminator.backward()
    optimizer_discriminator.step()
    
    #training generator
    
    optimizer_generator.zero_grad()
    generator_samples=generator(latent_heat_samples)
    generator_discriminator_sample=discriminator(generator_samples)
    loss_generator=loss_function(generator_discriminator_samples,real_samples)
    
    loss_generator.backward()
    optimizer_generator.step()
    
    #printing losses at each epoch
    if n==batch_size-1:
        print(f"Epoch:{epoch}, Loss D:{loss_discriminator}")
        print(f"Epoch:{epoch}, Loss G:{loss_generator}")
</code></pre>
<h2>Error as</h2>
<hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
10         latent_heat_samples=torch.randn((batch_size,224))<br>
11<br>
—&gt; 12         generated_samples=generator(latent_heat_samples)<br>
13         generated_sample_label=torch.zeros((batch_size,1))<br>
14</p>
<p>~\anaconda3\envs\gan\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
530             result = self._slow_forward(*input, **kwargs)<br>
531         else:<br>
–&gt; 532             result = self.forward(*input, **kwargs)<br>
533         for hook in self._forward_hooks.values():<br>
534             hook_result = hook(self, input, result)</p>
<p> in forward(self, x)<br>
13     def forward(self,x):<br>
14         x=x.view(x.size(0),224)<br>
—&gt; 15         output=self.model(x)<br>
16         return output<br>
17</p>
<p>~\anaconda3\envs\gan\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
530             result = self._slow_forward(*input, **kwargs)<br>
531         else:<br>
–&gt; 532             result = self.forward(*input, **kwargs)<br>
533         for hook in self._forward_hooks.values():<br>
534             hook_result = hook(self, input, result)</p>
<p>~\anaconda3\envs\gan\lib\site-packages\torch\nn\modules\container.py in forward(self, input)<br>
98     def forward(self, input):<br>
99         for module in self:<br>
–&gt; 100             input = module(input)<br>
101         return input<br>
102</p>
<p>~\anaconda3\envs\gan\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
530             result = self._slow_forward(*input, **kwargs)<br>
531         else:<br>
–&gt; 532             result = self.forward(*input, **kwargs)<br>
533         for hook in self._forward_hooks.values():<br>
534             hook_result = hook(self, input, result)</p>
<p>~\anaconda3\envs\gan\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)<br>
85<br>
86     def forward(self, input):<br>
—&gt; 87         return F.linear(input, self.weight, self.bias)<br>
88<br>
89     def extra_repr(self):</p>
<p>~\anaconda3\envs\gan\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)<br>
1368     if input.dim() == 2 and bias is not None:<br>
1369         # fused op is marginally faster<br>
-&gt; 1370         ret = torch.addmm(bias, input, weight.t())<br>
1371     else:<br>
1372         output = input.matmul(weight.t())</p>
<p>RuntimeError: size mismatch, m1: [1000 x 224], m2: [1000 x 224] at C:\Users\builder\AppData\Local\Temp\pip-req-build-e5c8dddg\aten\src\TH/generic/THTensorMath.cpp:136</p>
<p>Please suggest me to solve this error, I am new to Pytorch. Thanks in Advance.</p>

username: ptrblck
cooked: <p>The <code>view</code> operation is wrong:</p>
<pre><code class="lang-python">x=x.view(x.size(0),224)
</code></pre>
<p>since you are reshaping the output to have <code>224</code> features, while the linear layer is defined as:</p>
<pre><code class="lang-python">self.model=nn.Sequential(
    nn.Linear(1000,224),
    ...
</code></pre>
<p>You would have to pass an input with 1000 features or change the <code>in_features</code> of the linear layer.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which would make debugging easier. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>