loss.data[0] 报错invalid index of a 0-dim tensor. Use `tensor.item()` in Python or `tensor.item<T>()`

刚开始学pytorch
源码

for e in range(1000):
  out = logistic_regression(Variable(x))
  loss = criterion(out, Variable(y))
  optimizer.zero_grad()
  loss.backward()
  optimizer.step()
  if (e + 1) % 20 == 0:
      print('epoch:	{},	loss:	{}'.format(e + 1, loss.data[0]))

报错
在这里插入图片描述
修改后
把loss.data[0]后边的[0]删除就可以运行了

for e in range(1000):
  out = logistic_regression(Variable(x))
  loss = criterion(out, Variable(y))
  optimizer.zero_grad()
  loss.backward()
  optimizer.step()
  if (e + 1) % 20 == 0:
      print('epoch:	{},	loss:	{}'.format(e + 1, loss.data))

你可能感兴趣的:(深度学习,bug)