反向传播算法
- 数学公式
- 算法代码
- 结果
- 算法中一些函数的区别
数学公式
算法代码
这里用反向传播算法,计算 y = w * x模型
import numpy as np
import matplotlib.pyplot as ply
#反向传播算法,需要使用pytorch框架,
#这里导入pytorch框架,用torch
import torch
#用反向传播算法计算 y = w * x模型
x_data = [1.0, 2.0, 3.0, 4.0]
y_data = [2.0, 4.0, 6.0, 8.0]
w = torch.Tensor(1) # 设置初始值
w.requires_grad = True #计算梯度,默认是不计算的
def forward(x):
return w*x
def loss(x, y):
y_pred = forward(x)
return (y_pred - y)**2
print('Predict (befortraining)',4,forward(4))
#注意:在pytorch中,只有浮点类型的数才有梯度,因此在定义张量时一定要将类型指定为float型
#100轮
for epoch in range(100):
l = loss(1, 2) # 为了在for循环之前定义l,以便之后的输出,无实际意义
for x, y in zip(x_data, y_data):
l = loss(x, y)
l.backward() #计算梯度的函数(作用单独查下)
print('\tgrad:', x, y, w.grad.item())
w.data = w.data - 0.01 * w.grad.data # w.grad是一个张量,所以要取data
w.grad.data.zero_() #释放之前的梯度
print('Epoch:', epoch, l.item())
print('Predict(after training)', 4, forward(4).item())
结果
“C:\Program Files\Python38\python.exe” C:\Users\惊艳了时光\Desktop\code\机器学习\反向传播算法.py
Predict (befortraining) 4 tensor([0.], grad_fn=)
grad: 1.0 2.0 -4.0
Epoch: 0 4.0
grad: 2.0 4.0 -15.680000305175781
Epoch: 0 15.366400718688965
grad: 3.0 6.0 -32.457603454589844
Epoch: 0 29.263774871826172
grad: 4.0 8.0 -47.31596755981445
Epoch: 0 34.98126220703125
grad: 1.0 2.0 -2.0109286308288574
Epoch: 1 1.0109584331512451
grad: 2.0 4.0 -7.882840156555176
Epoch: 1 3.883697986602783
grad: 3.0 6.0 -16.31747817993164
Epoch: 1 7.396113872528076
grad: 4.0 8.0 -23.78725814819336
Epoch: 1 8.841151237487793
grad: 1.0 2.0 -1.0109584331512451
Epoch: 2 0.25550922751426697
grad: 2.0 4.0 -3.9629573822021484
Epoch: 2 0.981564462184906
grad: 3.0 6.0 -8.203322410583496
Epoch: 2 1.8692916631698608
grad: 4.0 8.0 -11.9586181640625
Epoch: 2 2.234508514404297
grad: 1.0 2.0 -0.5082411766052246
Epoch: 3 0.06457727402448654
grad: 2.0 4.0 -1.9923057556152344
Epoch: 3 0.24808013439178467
grad: 3.0 6.0 -4.124073028564453
Epoch: 3 0.4724438488483429
grad: 4.0 8.0 -6.011981964111328
Epoch: 3 0.5647488832473755
grad: 1.0 2.0 -0.2555091381072998
Epoch: 4 0.016321230679750443
grad: 2.0 4.0 -1.0015954971313477
Epoch: 4 0.06269959360361099
grad: 3.0 6.0 -2.07330322265625
Epoch: 4 0.11940517276525497
grad: 4.0 8.0 -3.0224151611328125
Epoch: 4 0.14273427426815033
grad: 1.0 2.0 -0.12845253944396973
Epoch: 5 0.0041250139474868774
grad: 2.0 4.0 -0.5035343170166016
Epoch: 5 0.015846675261855125
grad: 3.0 6.0 -1.0423164367675781
Epoch: 5 0.030178431421518326
grad: 4.0 8.0 -1.5194625854492188
Epoch: 5 0.036074478179216385
grad: 1.0 2.0 -0.06457710266113281
Epoch: 6 0.001042550546117127
grad: 2.0 4.0 -0.2531423568725586
Epoch: 6 0.004005065653473139
grad: 3.0 6.0 -0.5240049362182617
Epoch: 6 0.007627254817634821
grad: 4.0 8.0 -0.7638816833496094
Epoch: 6 0.009117425419390202
grad: 1.0 2.0 -0.03246498107910156
Epoch: 7 0.0002634937409311533
grad: 2.0 4.0 -0.12726306915283203
Epoch: 7 0.0010122430976480246
grad: 3.0 6.0 -0.26343441009521484
Epoch: 7 0.0019277135143056512
grad: 4.0 8.0 -0.3840293884277344
Epoch: 7 0.0023043525870889425
grad: 1.0 2.0 -0.016321182250976562
Epoch: 8 6.659524660790339e-05
grad: 2.0 4.0 -0.0639791488647461
Epoch: 8 0.0002558332053013146
grad: 3.0 6.0 -0.13243675231933594
Epoch: 8 0.00048720816266722977
grad: 4.0 8.0 -0.19306182861328125
Epoch: 8 0.0005823886021971703
grad: 1.0 2.0 -0.008205175399780273
Epoch: 9 1.6831225366331637e-05
grad: 2.0 4.0 -0.032164573669433594
Epoch: 9 6.465998740168288e-05
grad: 3.0 6.0 -0.06658172607421875
Epoch: 9 0.00012314239575061947
grad: 4.0 8.0 -0.0970611572265625
Epoch: 9 0.00014720106264576316
grad: 1.0 2.0 -0.004125118255615234
Epoch: 10 4.2541500988591e-06
grad: 2.0 4.0 -0.016170501708984375
Epoch: 10 1.634281943552196e-05
grad: 3.0 6.0 -0.033473968505859375
Epoch: 10 3.112518243142404e-05
grad: 4.0 8.0 -0.048797607421875
Epoch: 10 3.7206351407803595e-05
grad: 1.0 2.0 -0.002074003219604492
Epoch: 11 1.0753723245215951e-06
grad: 2.0 4.0 -0.008130073547363281
Epoch: 11 4.131130936002592e-06
grad: 3.0 6.0 -0.016828536987304688
Epoch: 11 7.866657142585609e-06
grad: 4.0 8.0 -0.024532318115234375
Epoch: 11 9.403665899299085e-06
grad: 1.0 2.0 -0.0010426044464111328
Epoch: 12 2.7175599370821146e-07
grad: 2.0 4.0 -0.0040874481201171875
Epoch: 12 1.0442020084155956e-06
grad: 3.0 6.0 -0.008460044860839844
Epoch: 12 1.988121084650629e-06
grad: 4.0 8.0 -0.012332916259765625
Epoch: 12 2.3765753667248646e-06
grad: 1.0 2.0 -0.0005240440368652344
Epoch: 13 6.865553814350278e-08
grad: 2.0 4.0 -0.0020542144775390625
Epoch: 13 2.637373199831927e-07
grad: 3.0 6.0 -0.0042514801025390625
Epoch: 13 5.020856406190433e-07
grad: 4.0 8.0 -0.006198883056640625
Epoch: 13 6.004086117172847e-07
grad: 1.0 2.0 -0.00026345252990722656
Epoch: 14 1.7351808878629527e-08
grad: 2.0 4.0 -0.0010328292846679688
Epoch: 14 6.667102070423425e-08
grad: 3.0 6.0 -0.0021371841430664062
Epoch: 14 1.2687655726040248e-07
grad: 4.0 8.0 -0.003116607666015625
Epoch: 14 1.5176942724792752e-07
grad: 1.0 2.0 -0.00013256072998046875
Epoch: 15 4.393086783238687e-09
grad: 2.0 4.0 -0.0005197525024414062
Epoch: 15 1.68839164871315e-08
grad: 3.0 6.0 -0.00107574462890625
Epoch: 15 3.2145180739462376e-08
grad: 4.0 8.0 -0.001567840576171875
Epoch: 15 3.84081886295462e-08
grad: 1.0 2.0 -6.651878356933594e-05
Epoch: 16 1.1061871418860392e-09
grad: 2.0 4.0 -0.00026035308837890625
Epoch: 16 4.2364831642771605e-09
grad: 3.0 6.0 -0.000537872314453125
Epoch: 16 8.036295184865594e-09
grad: 4.0 8.0 -0.00078582763671875
Epoch: 16 9.648829291108996e-09
grad: 1.0 2.0 -3.337860107421875e-05
Epoch: 17 2.7853275241795927e-10
grad: 2.0 4.0 -0.00013065338134765625
Epoch: 17 1.0668941285985056e-09
grad: 3.0 6.0 -0.0002689361572265625
Epoch: 17 2.0090737962163985e-09
grad: 4.0 8.0 -0.000392913818359375
Epoch: 17 2.412207322777249e-09
grad: 1.0 2.0 -1.6689300537109375e-05
Epoch: 18 6.963318810448982e-11
grad: 2.0 4.0 -6.580352783203125e-05
Epoch: 18 2.7063151719630696e-10
grad: 3.0 6.0 -0.00013446807861328125
Epoch: 18 5.022684490540996e-10
grad: 4.0 8.0 -0.0001983642578125
Epoch: 18 6.148184183984995e-10
grad: 1.0 2.0 -8.344650268554688e-06
Epoch: 19 1.7408297026122455e-11
grad: 2.0 4.0 -3.24249267578125e-05
Epoch: 19 6.571099220309407e-11
grad: 3.0 6.0 -6.580352783203125e-05
Epoch: 19 1.2028067430946976e-10
grad: 4.0 8.0 -9.5367431640625e-05
Epoch: 19 1.4210854715202004e-10
grad: 1.0 2.0 -4.0531158447265625e-06
Epoch: 20 4.106937012693379e-12
grad: 2.0 4.0 -1.621246337890625e-05
Epoch: 20 1.6427748050773516e-11
grad: 3.0 6.0 -3.4332275390625e-05
Epoch: 20 3.2741809263825417e-11
grad: 4.0 8.0 -4.9591064453125e-05
Epoch: 20 3.842615114990622e-11
grad: 1.0 2.0 -2.1457672119140625e-06
Epoch: 21 1.1510792319313623e-12
grad: 2.0 4.0 -8.58306884765625e-06
Epoch: 21 4.604316927725449e-12
grad: 3.0 6.0 -1.71661376953125e-05
Epoch: 21 8.185452315956354e-12
grad: 4.0 8.0 -2.6702880859375e-05
Epoch: 21 1.1141310096718371e-11
grad: 1.0 2.0 -1.1920928955078125e-06
Epoch: 22 3.552713678800501e-13
grad: 2.0 4.0 -4.76837158203125e-06
Epoch: 22 1.4210854715202004e-12
grad: 3.0 6.0 -1.1444091796875e-05
Epoch: 22 3.637978807091713e-12
grad: 4.0 8.0 -1.52587890625e-05
Epoch: 22 3.637978807091713e-12
grad: 1.0 2.0 -7.152557373046875e-07
Epoch: 23 1.2789769243681803e-13
grad: 2.0 4.0 -2.86102294921875e-06
Epoch: 23 5.115907697472721e-13
grad: 3.0 6.0 -5.7220458984375e-06
Epoch: 23 9.094947017729282e-13
grad: 4.0 8.0 -1.1444091796875e-05
Epoch: 23 2.0463630789890885e-12
grad: 1.0 2.0 -4.76837158203125e-07
Epoch: 24 5.684341886080802e-14
grad: 2.0 4.0 -1.9073486328125e-06
Epoch: 24 2.2737367544323206e-13
grad: 3.0 6.0 -5.7220458984375e-06
Epoch: 24 9.094947017729282e-13
grad: 4.0 8.0 -7.62939453125e-06
Epoch: 24 9.094947017729282e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 25 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 25 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 25 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 25 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 26 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 26 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 26 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 26 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 27 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 27 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 27 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 27 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 28 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 28 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 28 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 28 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 29 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 29 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 29 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 29 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 30 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 30 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 30 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 30 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 31 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 31 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 31 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 31 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 32 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 32 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 32 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 32 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 33 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 33 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 33 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 33 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 34 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 34 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 34 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 34 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 35 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 35 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 35 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 35 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 36 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 36 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 36 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 36 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 37 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 37 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 37 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 37 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 38 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 38 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 38 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 38 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 39 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 39 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 39 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 39 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 40 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 40 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 40 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 40 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 41 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 41 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 41 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 41 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 42 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 42 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 42 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 42 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 43 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 43 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 43 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 43 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 44 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 44 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 44 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 44 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 45 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 45 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 45 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 45 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 46 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 46 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 46 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 46 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 47 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 47 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 47 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 47 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 48 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 48 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 48 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 48 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 49 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 49 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 49 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 49 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 50 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 50 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 50 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 50 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 51 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 51 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 51 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 51 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 52 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 52 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 52 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 52 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 53 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 53 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 53 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 53 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 54 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 54 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 54 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 54 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 55 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 55 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 55 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 55 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 56 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 56 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 56 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 56 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 57 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 57 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 57 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 57 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 58 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 58 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 58 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 58 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 59 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 59 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 59 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 59 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 60 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 60 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 60 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 60 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 61 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 61 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 61 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 61 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 62 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 62 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 62 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 62 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 63 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 63 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 63 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 63 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 64 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 64 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 64 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 64 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 65 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 65 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 65 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 65 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 66 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 66 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 66 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 66 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 67 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 67 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 67 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 67 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 68 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 68 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 68 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 68 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 69 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 69 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 69 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 69 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 70 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 70 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 70 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 70 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 71 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 71 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 71 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 71 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 72 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 72 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 72 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 72 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 73 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 73 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 73 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 73 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 74 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 74 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 74 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 74 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 75 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 75 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 75 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 75 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 76 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 76 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 76 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 76 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 77 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 77 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 77 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 77 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 78 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 78 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 78 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 78 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 79 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 79 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 79 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 79 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 80 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 80 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 80 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 80 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 81 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 81 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 81 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 81 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 82 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 82 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 82 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 82 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 83 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 83 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 83 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 83 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 84 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 84 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 84 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 84 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 85 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 85 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 85 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 85 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 86 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 86 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 86 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 86 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 87 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 87 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 87 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 87 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 88 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 88 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 88 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 88 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 89 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 89 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 89 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 89 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 90 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 90 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 90 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 90 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 91 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 91 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 91 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 91 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 92 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 92 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 92 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 92 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 93 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 93 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 93 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 93 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 94 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 94 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 94 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 94 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 95 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 95 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 95 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 95 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 96 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 96 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 96 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 96 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 97 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 97 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 97 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 97 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 98 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 98 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 98 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 98 2.2737367544323206e-13
grad: 1.0 2.0 -2.384185791015625e-07
Epoch: 99 1.4210854715202004e-14
grad: 2.0 4.0 -9.5367431640625e-07
Epoch: 99 5.684341886080802e-14
grad: 3.0 6.0 -2.86102294921875e-06
Epoch: 99 2.2737367544323206e-13
grad: 4.0 8.0 -3.814697265625e-06
Epoch: 99 2.2737367544323206e-13
Predict(after training) 4 7.999999523162842
算法中一些函数的区别
1.w.data 表示张量w的值,其本身也是张量,输出格式tensor[数]。
2.w.grad 表示张量w的梯度,其本身w.grad是张量 用时(标量计算时)需要取w.grad.data,表示张量w.grad的值,输出格式tensor[数],(梯度输出时)需要取w.grad.item(),表示返回的是一个具体的数值,输出格式 数
3.w.grad.item() l.item() 表示返回的是一个具体的数值,输出格式 数