当我运行这段代码时,我得到了错误TypeError: 'Tensor' object is not callable
:
torch.manual_seed(50)
epoch = 5
train_loss, train_acc = 0, 0
for epochs in range(epoch):
for batch, (X, y) in enumerate(train_dataloader):
model.train()
y_logits = model(X)
print(y_logits.shape)
print(y.shape)
loss = loss(y_logits, y)
train_loss += loss
optimizer.zero_grad()
loss.backward()
optimizer.step()
错误发生在我的损失函数上。下面是我创建它的代码:
!pip install torchmetrics
from torchmetrics.classification import MulticlassAccuracy
loss = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(params=model.parameters(),
lr=0.01)
有什么办法解决这个错误吗?
1条答案
按热度按时间fdbelqdn1#
函数名和变量名应该不同。