import numpy as np
import matplotlib.pyplot as plt
import torch
#part1 prepare dataset
x_data = torch.Tensor([[1.0],[2.0],[3.0]])
y_data = torch.Tensor([[2.0],[4.0],[6.0]])#part2 design model using classclassLinearModel(torch.nn.Module):def__init__(self):super(LinearModel, self).__init__()#官方写法
self.linear = torch.nn.Linear(1,1)#线性模型defforward(self,x):#重写callable forward
y_pred = self.linear(x)return y_pred
#part3 construct loss and optimizer#实例化,创建线性模型
model = LinearModel()
criterion = torch.nn.MSELoss(size_average=False)#loss 不需要算均值
optimizer = torch.optim.SGD(model.parameters(),lr=0.01)#SGD 随机梯度下降优化器; model.parameters:输入权重; lr 学习率#part4 training cyclefor epoch inrange(100):
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())
x_test = torch.Tensor([[4.0]])
y_test = model(x_test)print('y_pred = ',y_test.data.item())
#out:016.28174781799316417.29366445541381823.2917718887329131.509597420692443840.715589106082916350.361493527889251760.2032432705163955770.1321862041950225880.0999545529484748890.08501528948545456100.07778217643499374110.07398831844329834120.07173360139131546130.07017242908477783140.06892779469490051150.0678321123123169160.06681052595376968170.06582929939031601180.06487414985895157190.06393765658140182200.063016876578331210.06211043894290924220.061217449605464935230.060337554663419724240.059470437467098236250.05861560255289078260.057773198932409286270.05694276839494705280.05612459406256676290.05531793832778931300.054522860795259476310.05373939126729965320.05296697840094566330.05220579355955124340.05145558714866638350.05071603134274483360.04998715966939926370.049268800765275955380.048560768365859985390.047862716019153595400.04717487469315529410.046496935188770294420.04582870006561279430.04517001658678055440.04452100023627281450.043881095945835114460.043250422924757004470.04262881353497505480.042016178369522095490.041412435472011566
D:\Anaconda3\envs\pcd\lib\site-packages\torch\nn\_reduction.py:43: UserWarning: size_average andreduce args will be deprecated, please use reduction='sum' instead.
warnings.warn(warning.format(ret))500.0408172532916069510.040230605751276016520.03965239226818085530.03908245638012886540.03852083534002304550.03796733543276787560.03742161765694618570.0368838757276535580.03635374456644058590.035831205546855927600.03531632944941521610.03480881080031395620.034308500587940216630.03381550312042236640.03332945331931114650.032850489020347595660.03237827494740486670.03191307559609413680.0314544141292572690.031002411618828773700.030556699261069298710.03011762723326683720.02968478389084339730.029258109629154205740.028837714344263077750.028423231095075607760.028014766052365303770.02761211432516575780.027215277776122093790.02682417817413807800.02643866091966629810.026058759540319443820.025684215128421783830.02531503140926361840.024951264262199402850.024592645466327667860.024239204823970795870.02389087900519371880.023547515273094177890.0232091061770916900.022875573486089706910.02254680171608925920.022222815081477165930.0219033882021904940.02158866822719574950.021278396248817444960.020972535014152527970.020671162754297256980.02037406899034977990.020081277936697006
w =1.905661940574646
b=0.21445274353027344
y_pred =7.837100505828857
Process finished with exit code 0