Python 计算损耗后,发出运行反向功能

Python 计算损耗后,发出运行反向功能,python,neural-network,pytorch,loss-function,Python,Neural Network,Pytorch,Loss Function,我正在谷歌colab上使用Pytorch。错误消息说梯度不存在。 我不确定错误来自哪里?我使用了本教程: 来自Google colab的错误消息 我在google colab中运行了你的代码,它对我来说很好。它应该是优化器.zero\u grad(),而不是net.zero\u grad()@abenet.zero\u grad()和优化器.zero\u grad())在这里是等效的,因为优化器只包含net@jodag的参数。哦,我不知道它也是这样工作的,谢谢你指出。但考虑到其他情况,最好习惯使

我正在谷歌colab上使用Pytorch。错误消息说梯度不存在。 我不确定错误来自哪里?我使用了本教程: 来自Google colab的错误消息

我在google colab中运行了你的代码,它对我来说很好。它应该是
优化器.zero\u grad()
,而不是
net.zero\u grad()
@abe
net.zero\u grad()
优化器.zero\u grad())
在这里是等效的,因为优化器只包含
net
@jodag的参数。哦,我不知道它也是这样工作的,谢谢你指出。但考虑到其他情况,最好习惯使用
优化器.zero\u grad()
而不是
模型。zero\u grad()
我在google colab中运行了你的代码,它对我很好。它应该是
优化器.zero\u grad()
,而不是
net.zero\u grad()
@abe
net.zero\u grad()
优化器.zero_grad()
在这里是等效的,因为优化器只包含
net
@jodag的参数。哦,我不知道它也是这样工作的,谢谢你指出。但考虑到其他情况,最好习惯使用
optimizer.zero\u grad()
而不是
model.zero\u grad()
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torchvision import transforms, datasets

#neural network class
class Net(nn.Module):
  #intialize class
  def __init__(self):
    super().__init__()
    #feedforward neural network passes data from input layer to output layer
    #fully connected layer with input shape of 28*28 pixels (flatten image to one row) and output feature is size 64. Linear means flat layer
    self.fc1 = nn.Linear(28*28,64)
    #feed in data from fc1 to fc2
    self.fc2 = nn.Linear(64,64)
    self.fc3 = nn.Linear(64,64)
    #output layer has input 64 and output of size 10 to represent 10 classes in MNIST
    self.fc4 = nn.Linear(64,10)
  #forward pass through the data 
  def forward(self, x):
    #relu is activation function and performs operation on input data
    #input and output dimenson of relu are the same
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = F.relu(self.fc3(x))
    #softmax function gets probability distribution for each class adding up to 1 for output layer
    x = F.log_softmax(self.fc4(x), dim=1)
    return x

#declare a model
net = Net()
#print(net)
#passing in random data
x = torch.rand(28,28)
#resize to represent input shape (batch size, input x, input y)
x = x.view(-1,28,28)
#print(x)

#optmizer adjusts neural network based on error calculation
import torch.optim as optim 
#net.parameters() means all the adjustable parts of the neural network, learning rate is amount of change (we don't want model to swerve based on one train case)
optimizer = optim.Adam(net.parameters(),lr=0.001)

#get datasets using torchvision.datasets transforms are application applied to data (transforms conversion to tensors)
train = torchvision.datasets.MNIST("", train=True, download=True,transform=transforms.Compose([transforms.ToTensor()]))
test = torchvision.datasets.MNIST("", train=False, download=True,transform=transforms.Compose([transforms.ToTensor()]))
#store in data loader, batch size is how many samples is passed through the model at once (in GPU memory), best batch size is between 8-64
#shuffling avoids feeding too much of one kind of image and leads to more generalization 
trainset = torch.utils.data.DataLoader(train,batch_size=10,shuffle=True)
testset = torch.utils.data.DataLoader(test,batch_size=10,shuffle=True)

#full pass through data is epoch
EPOCHS = 3
for epoch in range(EPOCHS):
  #data is a batch of data in the training set
  for data in trainset:
    #split into features and labels
    features, labels = data
    #print(features, labels)
    #reset the gradient for next passes to avoid convoluting the results of multiple backpropogations  
    net.zero_grad()
    #pass data into network (make sure input shape matches)
    output = net(features.view(-1,28*28))
    #compute error (output,expected)
    loss = F.nll_loss(output,labels)
    print("loss is the ", loss)
    #backpropogate loss through trainiable parameters of model
    loss.backward()
    #adjust neural network
    optimizer.step()