View() received an invalid combination of arguments - got (tuple), but expected one of: * (torch.dtype dtype)

Joao_Vitor_Valadares: <p>Hello, I’m trying to build a Generator for my GAN and this is what it looks like:</p>
<pre><code class="lang-auto">class Generator(nn.Module):
    def __init__(self, latent_size, init_resolution=8, resolution=256, channels=3):
        super(Generator,self).__init__()
        #resolution = self.resolution
        #channels = self.channels

        self.cnn_torch = nn.Sequential()

        self.cnn_torch.add_module("Linear1", nn.Linear(latent_size, 1024, bias=False))
        self.cnn_torch.add_module("ReLu1", nn.ReLU(inplace=True))
        self.cnn_torch.add_module("Linear2", nn.Linear(1024, 128 * init_resolution * init_resolution, bias=False))
        self.cnn_torch.add_module("ReLu2", nn.ReLU(inplace=True))
        self.cnn_torch.add_module("reshape", Reshape((128, init_resolution, init_resolution)))

        crt_res = init_resolution

        #upsample
        while crt_res != resolution:
          self.cnn_torch.add_module("Upsample", nn.UpsamplingNearest2d(scale_factor=2))
          if crt_res &lt; resolution/2:
            self.cnn_torch.add_module("Upsample Conv2d", nn.Conv2d(crt_res, 256, (5, 5), padding=1, bias=False))
            self.cnn_torch.add_module("Upsample ReLU", nn.ReLU(inplace=True))
          else:
            self.cnn_torch.add_module("Upsample Conv2d - else", nn.Conv2d(crt_res, 128, (5, 5), padding=1, bias=False))

          crt_res = crt_res * 2
          assert crt_res &lt;= resolution,\
                  "Error: final resolution [{}] must equal i*2^n. Initial resolution i is [{}]. n must be a natural number.".format(resolution, init_resolution)

        self.cnn_torch.add_module("Conv2d final", nn.Conv2d(crt_res, channels, (2,2), padding=1, bias=False))
        self.cnn_torch.add_module("Tanh", nn.Tanh())
        print(self.cnn_torch)

    def forward(self, latent):
        fake_image_from_latent = self.cnn_torch(latent)      
        return fake_image_from_latent
</code></pre>
<p>But I’m getting the following error when simply trying to test it (with no images):</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-30-ccb426d28f8b&gt; in &lt;cell line: 14&gt;()
     12 
     13 # Pass the latent vector through the generator
---&gt; 14 fake_images = generator(latent_vector)
     15 
     16 # Inspect the generated fake images

5 frames
&lt;ipython-input-14-8cb072a2d5c1&gt; in forward(self, x)
      7 
      8     def forward(self, x):
----&gt; 9         return x.view(self.shape)

TypeError: view() received an invalid combination of arguments - got (tuple), but expected one of:
 * (torch.dtype dtype)
      didn't match because some of the arguments have invalid types: (!tuple of (tuple,)!)
 * (tuple of ints size)
      didn't match because some of the arguments have invalid types: (!tuple of (tuple,)!)
</code></pre>
<p>I’m trying to test it this way to see if it outputs the correct tensor shape:</p>
<pre><code class="lang-auto"># Instantiate the Generator
latent_size = 100
init_resolution = 8
resolution = 256
channels = 3

generator = Generator(latent_size, init_resolution, resolution, channels)

# Create a random input tensor that represents the latent vector
batch_size = 10
latent_vector = torch.randn(batch_size, latent_size)

# Pass the latent vector through the generator
fake_images = generator(latent_vector)

# Inspect the generated fake images
print(fake_images.shape)
</code></pre>
<p>Could anyone help, please? Thanks in advance.</p>

ptrblck: <p>Could you <code>print(self.shape)</code> and check what exactly it contains?</p>

Joao_Vitor_Valadares: <p>I tried printing <code>self.shape</code> inside my forward definition but it simply says <code>'Generator' object has no attribute 'shape'</code>, my latent vector however is torch.Size([10, 100]) as expected<br>
Also this is what the <code>print(self.cnn_torch)</code> looks like inside the Generator:</p>
<pre><code class="lang-auto">Sequential(
  (Linear1): Linear(in_features=100, out_features=1024, bias=False)
  (ReLu1): ReLU(inplace=True)
  (Linear2): Linear(in_features=1024, out_features=8192, bias=False)
  (ReLu2): ReLU(inplace=True)
  (reshape): Reshape()
  (Upsample): UpsamplingNearest2d(scale_factor=2.0, mode='nearest')
  (Upsample Conv2d): Conv2d(64, 256, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1), bias=False)
  (Upsample ReLU): ReLU(inplace=True)
  (Upsample Conv2d - else): Conv2d(128, 128, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1), bias=False)
  (Conv2d final): Conv2d(256, 3, kernel_size=(2, 2), stride=(1, 1), padding=(1, 1), bias=False)
  (Tanh): Tanh()
)
</code></pre>

ptrblck: <p>This doesn’t match the previously reported error message since it claims <code>shape</code> is an unexpected <code>tuple</code> while you are not able to <code>print</code> it now as it seems to be undefined.</p>

Joao_Vitor_Valadares: <p>OK, I forgot to include this part of the code which seems to be causing the problem, however I haven’t found a solution yet to have the same behaviour:</p>
<pre><code class="lang-auto">class Reshape(nn.Module):
    def __init__(self, *args):
        super(Reshape, self).__init__()
        self.shape = args

    def forward(self, x):
        print(self.shape) #Added this now to check
        return x.view(self.shape)
</code></pre>
<p>I was trying to replicate this Keras’ Reshape in PyTorch :<br>
<code>cnn.add(Reshape((128, init_resolution, init_resolution)))</code><br>
and as you can see I’m using it inside my Generator as the fifth layer (including ReLU layers) and this is the output of <code>print(self.shape)</code>:<br>
((128, 8, 8),)</p>

ptrblck: <p>Thanks for the code!<br>
Remove the <code>*</code> in <code>*args</code> and it should work.</p>

Joao_Vitor_Valadares: <p>Thanks for the help!</p>