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

username: Bahaa_Kattan
cooked: <p>I got this error while working with text dataset</p>
<pre><code class="lang-auto">for text, labels in trainloader:
    clf.train()
    output = clf(text).squeeze()
    # compute loss function
    loss = loss(output, labels.float())
    print(loss)
</code></pre>
<p>here is the full code</p>
<pre><code class="lang-auto">
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=&lt;BinaryCrossEntropyWithLogitsBackward&gt;)

</code></pre>
<p>and here is what I got</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "C:/Users/BHAAK/Desktop/ML_PATH/dirty-hands/dirty-hands file 3/Project.py", line 322, in &lt;module&gt;
    loss = loss(output, labels.float())
TypeError: 'Tensor' object is not callable

</code></pre>

username: Bahaa_Kattan
cooked: <p>I fixed the problem.</p>
<p>I must change the name of the loss function<br>
from <code>    loss = loss(output, labels.float())</code><br>
to <code>lossfun = dnn.BCEWithLogitsLoss() </code> or any name other than <code>loss</code>.</p>
<p>does any one know why this name make the problem?</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Bahaa_Kattan" data-post="2" data-topic="99776">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bahaa_kattan/48/28782_2.png" class="avatar"> Bahaa_Kattan:</div>
<blockquote>
<p>does any one know why this name make the problem?</p>
</blockquote>
</aside>
<p>You are overwriting the <code>loss</code> criterion name with the <code>loss</code> output tensor of the criterion, so that the next iteration will fail:</p>
<pre data-code-wrap="python"><code class="lang-python"># 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())
</code></pre>