TypeError: 'tuple' object is not callable in PyTorch layer

Ask Questions Forum: ask Machine Learning Questions to our readersCategory: PyTorchTypeError: 'tuple' object is not callable in PyTorch layer
Chris Staff asked 6 months ago

I am getting the following error:

File "firstproject.py", line 26, in forward
x = F.sigmoid(self.lin1(x))
TypeError: 'tuple' object is not callable

How can this be fixed? This is the code:
class LitAutoEncoder(pl.LightningModule):

def __init__(self):
   self.lin1 = nn.Linear(28 * 28, 256),
   self.lin2 = nn.Linear(256, 128)
   self.lin3 = nn.Linear(128, 56)
   self.lin4 = nn.Linear(56, 10)
   self.ce = nn.CrossEntropyLoss()
def forward(self, x):
   x = F.sigmoid(self.lin1(x))
   x = F.tanh(self.lin2(x))
   x = F.relu(self.lin3(x))
   x = self.lin4(x)
   return x
def training_step(self, batch, batch_idx):
   # training_step defined the train loop. It is independent of forward
   x, y = batch
   x = x.view(x.size(0), -1)
   z = self(x)
   loss = self.ce(z, y)
   self.log(‘train_loss’, loss)
   return loss
def configure_optimizers(self):
   optimizer = torch.optim.Adam(self.parameters(), lr=1e-3)
   return optimizer

1 Answers
Best Answer
Chris Staff answered 6 months ago

Turns out there is a comma added to the layer definition that shouldn’t be there:

self.lin1 = nn.Linear(28 * 28, 256),

Removing the comma fixes the problem.

Your Answer

9 + 16 =