CNN on FashionMnist

Micha_Rostova: <p>Hi everyone. I have wrote a cnn on FashionMnist but get this error. anybody can help?</p>
<pre><code class="lang-auto">TypeError: conv2d() received an invalid combination of arguments - got (tuple, Parameter, Parameter, tuple, tuple, tuple, int), but expected one of:
 * (Tensor input, Tensor weight, Tensor bias = None, tuple of ints stride = 1, tuple of ints padding = 0, tuple of ints dilation = 1, int groups = 1)
      didn't match because some of the arguments have invalid types: (!tuple of (Tensor,)!, !Parameter!, !Parameter!, !tuple of (int, int)!, !tuple of (int, int)!, !tuple of (int, int)!, !int!)
 * (Tensor input, Tensor weight, Tensor bias = None, tuple of ints stride = 1, str padding = "valid", tuple of ints dilation = 1, int groups = 1)
      didn't match because some of the arguments have invalid types: (!tuple of (Tensor,)!, !Parameter!, !Parameter!, !tuple of (int, int)!, !tuple of (int, int)!, !tuple of (int, int)!, !int!)
</code></pre>
<p>here is my CNN code:</p>
<pre><code class="lang-auto">
class FashionMNISTModelV2(nn.Module):
    def __init__(self, input_shape: int, hidden_units:int, output_shape: int):
        super().__init__()
        self.block_1= nn.Sequential(
            nn.Conv2d(in_channels= input_shape, out_channels= hidden_units, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d( kernel_size=2, stride=2 ),
        )
        
        self.block_2= nn.Sequential(
            nn.Conv2d(10, 40, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2),
            
        )

        self.classifier= nn.Sequential( 
            nn.Flatten(), 
            nn.Linear(in_features=40*7*7, out_features= output_shape ))

    def forward(self, x: torch.Tensor):
        x= self.block_1(x),
        x= self.block_2(x),
        x= self.classifier(x)
        return x
</code></pre>
<p>And this one is my training and testing loop:</p>
<pre><code class="lang-auto">def train_step(model: torch.nn.Module,
               data_loader: torch.utils.data.DataLoader,
               loss_fn: torch.nn.Module,
               optimizer: torch.optim.Optimizer,
               accuracy_fn):
    train_loss, train_acc = 0, 0
    for batch, (X, y) in enumerate(data_loader):
        # Send data to GPU
        
        # 1. Forward pass
        y_pred = model(X)

        # 2. Calculate loss
        loss = loss_fn(y_pred, y)
        train_loss += loss
        train_acc += accuracy_fn(y_true=y,
                                 y_pred=y_pred.argmax(dim=1)) # Go from logits -&gt; pred labels

        # 3. Optimizer zero grad
        optimizer.zero_grad()

        # 4. Loss backward
        loss.backward()

        # 5. Optimizer step
        optimizer.step()

    # Calculate loss and accuracy per epoch and print out what's happening
    train_loss /= len(data_loader)
    train_acc /= len(data_loader)
    print(f"Train loss: {train_loss:.5f} | Train accuracy: {train_acc:.2f}%")

def test_step(data_loader: torch.utils.data.DataLoader,
              model: torch.nn.Module,
              loss_fn: torch.nn.Module,
              accuracy_fn):
    test_loss, test_acc = 0, 0
    model.eval() # put model in eval mode
    # Turn on inference context manager
    with torch.inference_mode(): 
        for X, y in data_loader:
            # Send data to GPU
            
            # 1. Forward pass
            test_pred = model(X)
            
            # 2. Calculate loss and accuracy
            test_loss += loss_fn(test_pred, y)
            test_acc += accuracy_fn(y_true=y,
                y_pred=test_pred.argmax(dim=1) # Go from logits -&gt; pred labels
            )
        
        # Adjust metrics and print out
        test_loss /= len(data_loader)
        test_acc /= len(data_loader)
        print(f"Test loss: {test_loss:.5f} | Test accuracy: {test_acc:.2f}%\n")
</code></pre>

ptrblck: <p>You are creating <code>tuple</code>s by appending the comma in:</p>
<pre data-code-wrap="python"><code class="lang-python">    def forward(self, x: torch.Tensor):
        x= self.block_1(x),
        x= self.block_2(x),
</code></pre>
<p>Remove these and it should work.</p>

Micha_Rostova: <p>Thank you for answewring. I removed comma at the end, but still get same error</p>

ptrblck: <p>Your model works for me after fixing the error, so please post a minimal and executable code snippet which still reproduces the same error.</p>