我们定义Q=3a3−b2, 则可以计算出 ∂Q∂a=9a2 且 ∂Q∂b=−2b, 然后我们将a,b的值带入就可以计算出对应的梯度值。
import torch
x = torch.tensor(3, dtype=torch.float32, requires_grad=True)
y = torch.tensor(4, dtype=torch.float32, requires_grad=True)
b = torch.tensor(5, dtype=torch.float32, requires_grad=True)
z = x*y + b
"Z = xy"
print(z)
z.backward()
print(z.requires_grad, x.grad, y.grad, b.grad)
#调用线性包
from matplotlib import pyplot as plt
import torch
data_x = [1, 2, 3]#创建数据
data_y = [2, 4, 6]
loss_list = list()#损失的列表 创建list函数
a_list = list()
alpha = 0.01 #调线性图像系数
#预测值运算
def forward(x):
return a * x
def lossFunction(x, y):
y_pred = forward(x)#预测值
loss = (y_pred - y) ** 2 #损失值,将预测值减去真实值得平方
return loss
if __name__ == '__main__':
a = torch.Tensor([7.0])# 数据节点
a.requires_grad = True #requires_grad必须为True,才能求偏导
for epoch in range(1000):#梯度
# for a in np.arange(0, 4, 0.1):
sum_loss = 0
for i in range(3):
sum_loss += lossFunction(data_x[i], data_y[i])#获取data_x,data_y列表数据
l = lossFunction(data_x[i],data_y[i])
l.backward()
a.data = a.data - alpha*a.grad #梯度运算
a.grad = None
a_list.append(a.data)
# a = gradient(a, data_x[i], data_y[i])
loss_list.append(sum_loss / 3)#取平均数
#输出数据,图像显示
print(a_list)
plt.subplot(211)
plt.plot(a_list)
plt.subplot(212)
plt.plot(loss_list)
plt.show()