TypeError: 'Tensor' object is not callable | text classification

I got this error while working with text dataset

for text, labels in trainloader:
    clf.train()
    output = clf(text).squeeze()
    # compute loss function
    loss = loss(output, labels.float())
    print(loss)

here is the full code


text = pd.read_pickle('seq_df.pkl')
print(text[:5]) # [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2047, 7513], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9421, 805, 289, 2321], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5906, 6519, 10411, 4729, 7458]]

target = pd.read_pickle('label_paded.pkl')
print(target[:3]) # [1, 0, 1]



X_train, X_test, y_train, y_test = train_test_split(text, target, test_size=.2, random_state=42)

X_train, X_test  = torch.Tensor(X_train).long(), torch.Tensor(X_test).long()
y_train, y_test  = torch.Tensor(y_train).float(), torch.Tensor(y_test).float()
print(X_train.shape) # torch.Size([5744, 24])
print(y_train.shape) # torch.Size([5744])
train_set = data.TensorDataset(X_train, y_train)
test_set = data.TensorDataset(X_test, y_test)

trainloader = data.DataLoader(train_set, batch_size=32, shuffle=True)
valloader = data.DataLoader(test_set, batch_size=32, shuffle=True)


class Net(dnn.Module):
    def __init__(self, vocab_size, embedding_dim, num_layers=5, dropout=.5):
        super(Net, self).__init__()
        self.Embedding = dnn.Embedding(vocab_size, embedding_dim)
        self.LSTM = dnn.LSTM(embedding_dim, 20, num_layers=num_layers, bidirectional=True, dropout=dropout,
                             batch_first=True)
        self.Linear = dnn.Linear(20 * 2, 1)

    def forward(self, x):
        x = self.Embedding(x)
        x, (hidden, state) = self.LSTM(x)
        x = torch.cat((state[-2], state[-1]), dim=1)
        x = self.Linear(x)
        return x


clf = Net(vocab_size=10926, embedding_dim=10)


optim = torch.optim.AdamW(clf.parameters(), .001)
loss = dnn.BCEWithLogitsLoss()

for text, labels in trainloader:
    clf.train()
        print(text.shape) # torch.Size([32, 24])

    output = clf(text).squeeze()
    # compute loss function
    loss = loss(output, labels.float())
    print(loss) # tensor(0.7342, grad_fn=<BinaryCrossEntropyWithLogitsBackward>)

and here is what I got

Traceback (most recent call last):
  File "C:/Users/BHAAK/Desktop/ML_PATH/dirty-hands/dirty-hands file 3/Project.py", line 322, in <module>
    loss = loss(output, labels.float())
TypeError: 'Tensor' object is not callable

I fixed the problem.

I must change the name of the loss function
from loss = loss(output, labels.float())
to lossfun = dnn.BCEWithLogitsLoss() or any name other than loss.

does any one know why this name make the problem?

You are overwriting the loss criterion name with the loss output tensor of the criterion, so that the next iteration will fail:

# first iteration
# loss is the criterion here
loss = loss(output, labels.float())
# loss is a tensor from here on

# second iteration
# this will raise this error, since you are trying to call into the tensor
loss = loss(output, labels.float())
2 Likes