Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ';int';对象没有属性';尺寸'&引用;_Python_Deep Learning_Artificial Intelligence_Pytorch - Fatal编程技术网

Python ';int';对象没有属性';尺寸'&引用;

Python ';int';对象没有属性';尺寸'&引用;,python,deep-learning,artificial-intelligence,pytorch,Python,Deep Learning,Artificial Intelligence,Pytorch,F.nll_损失:我得到了 AttributeError:“int”对象没有属性“size” 当我尝试运行此代码时。我还得到了一段模块代码 raise VALUERROR('需要2个或多个维度(得到{}')。格式(dim)) 如果输入。大小(0)!=目标大小(0): raise VALUERROR('预期的输入批次大小({})与目标批次大小({})匹配。' 格式(输入.size(0)、目标.size(0))) 只需将for循环更改为: 列车数据集中数据的: 到 列车装载机中数据的: 您能分享整

F.nll_损失:我得到了

AttributeError:“int”对象没有属性“size”

当我尝试运行此代码时。我还得到了一段模块代码

raise VALUERROR('需要2个或多个维度(得到{}')。格式(dim)) 如果输入。大小(0)!=目标大小(0):
raise VALUERROR('预期的输入批次大小({})与目标批次大小({})匹配。'
格式(输入.size(0)、目标.size(0)))


只需将for循环更改为:

列车数据集中数据的

列车装载机中数据的

您能分享整个错误消息吗?谢谢您的帮助。错误在for循环中,我刚刚发布了正确的循环。EPOCHS=3表示范围内的历元(EPOCHS):对于火车装载机中的数据:x,y=data net.zero\u grad()x=x.view(-1,28*28)output=net(x)loss=F.nll\u loss(output,y)loss.backward()optimizer.step()print(loss)No ways这是整个错误消息,对吗?不,我使用的答案是“火车数据集中的数据”我不确定我是否理解“火车装载机中的数据”。我只是想看看所有相关的代码,以及你收到的错误信息。
import torch
from torchvision import transforms, datasets
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pylab as plt

train_dataset = datasets.MNIST(root = '', train =True, download = True,
                                transform =transforms.Compose([transforms.ToTensor()]))

test_dataset = datasets.MNIST(root ='', download =True, train =False,
                               transform =transforms.Compose([transforms.ToTensor()]))
batch_size = 10
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size, shuffle =True)

test_dataset = torch.utils.data.DataLoader(test_dataset, batch_size, shuffle =True)
class Net(nn.Module):

  def __init__(self):
    super().__init__()
    self.fc1 = nn.Linear(28*28, 64)
    self.fc2 = nn.Linear(64,64)
    self.fc3 = nn.Linear(64,64)
    self.fc4 = nn.Linear(64,10)

  def forward(self, x):
      x = F.relu(self.fc1(x))
      x = F.relu(self.fc2(x))
      x = F.relu(self.fc2(x))
      x = self.fc4(x)
      return F.log_softmax(x, dim=1)

x=torch.rand((28,28))
x=x.view(-1,28*28)
net =Net()
out=net(x)
out
import torch.optim as optim

optimizer =optim.Adam(net.parameters(), lr=0.001)

EPOCHS = 3
for epoch in range(EPOCHS):
  for data in train_dataset:
      x, y = data
      net.zero_grad()
      x=x.view(-1, 28*28)
      output = net(x)
      loss = F.nll_loss(output, y)
      loss.backward()
      optimizer.step()
  print(loss)