-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearback.py
More file actions
30 lines (29 loc) · 949 Bytes
/
Copy pathlinearback.py
File metadata and controls
30 lines (29 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import torch
import time
x_data=torch.Tensor([[1.0],[2.0],[3.0]])
y_data=torch.Tensor([[2.0],[4.0],[6.0]])
class LinearModel(torch.nn.Module):
def __init__(self):
super(LinearModel,self).__init__() #调用父类构造函数,just do it
self.linear=torch.nn.Linear(1,1) #构造线性模型,输入和输出维度
def forward(self,x):
y_pred=self.linear(x)
return y_pred
model=LinearModel()
criterion=torch.nn.MSELoss(reduction='sum')
optimizer=torch.optim.SGD(model.parameters(),lr=0.01)
timestamp1 = time.time()
for epoch in range(1000):
y_pred=model(x_data)
loss=criterion(y_pred,y_data)
print(epoch,loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('w=',model.linear.weight.item())
print('b=',model.linear.bias.item())
timestamp2 = time.time()
print(timestamp2-timestamp1)
x_test=torch.Tensor([[4.0]])
y_test=model(x_test)
print('y_pred',y_test.item())