I used pytorch 1.1.0 ,torchvision 0.3.0 and cudatoolkit 10.0.When I typed this “optimizer = torch.optim.SGD(Model.parameters(), lr=learning_rate)”,it appeared name ‘Model’ is not defined.
How did you define the Model
instance?
Note that Model
is only a variable you’ve used to create the model, e.g. via:
Model = ResNet()
optimizer = torch.optim.SGD(Model.parameters(), lr=1e-3)
I type this code:
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
from torch.autograd import Variable
input_size=1
output_size=1
learning_rate=0.001
xtrain=np.array([[2.3],[4.4],[3.7],[6.1],[7.3],[2.1],[5.6],[7.7],[8.7],[4.1],[6.7],[6.1],[7.5],[2.1],[7.2],[5.6],[5.7],[7.7],[3.1]],dtype=np.float32)
ytrain=np.array([[3.7],[4.76],[4.],[7.1],[8.6],[3.5],[5.4],[7.6],[7.9],[5.3],[7.3],[7.5],[8.5],[3.2],[8.7],[6.4],[6.6],[7.9],[5.3]],dtype=np.float32)
plt.figure()
plt.scatter(xtrain,ytrain)
plt.xlabel(‘xtrain’)
plt.ylabel(‘ytrain’)
plt.show()
class LinearRegression(nn.Module):
def init(self,input_size,output_size):
super(LInearRegression,self).init()
self.linear=nn.Linear(input_size,output_size)
def forward(self, x):
out = self.linear(x)
return out
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
You have to create an instance of LinearRegression
:
model = LinearRegression(input_size, output_size)
before using the model in some way.