(在pytorch包中)Tensor数据成员:data(存放数据w,也是Tensor变量,但是取data不会构建计算图)和grad(存放梯度loss对w的导,调用bacward之后grad也是个Tensor,每次引用结束要zero)
backward会释放计算图,每一次运行神经网络时计算图可能是不同的,所以没进行一次反向传播就释放计算图
exercise:文章来源:https://www.toymoban.com/news/detail-563895.html
# -*- coding: utf-8 -*-
# @Time : 2023-07-11 23:01
# @Author : yuer
# @FileName: exercise04.py
# @Software: PyCharm
import torch
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
# w是一个包含data和grad的张量
w = torch.Tensor([1.0])
w.requires_grad = True
def forward(x):
return x * w
# 数乘,将x强制类型转换为Tensor
# data和grad都是Tensor,但是取tensor中的data是不会构建计算图的。
def loss(x, y):
y_pred = forward(x)
return (y_pred - y) ** 2
print("predict (before training)", 4, forward(4).item())
for epoch in range(100):
for x, y in zip(x_data, y_data):
l = loss(x, y)
# 是包含w的计算,也是Tensor变量
l.backward()
# l.backward()会把计算图中所有需要梯度(grad)的地方都会求出来,然后把梯度都存在对应的待求的参数中,最终计算图被释放。
# 调用该方法后w.grad由None更新为Tensor类型,并释放计算图
print('\tgrad:', x, y, w.grad.item())
# item是标量,用于输出此数据
w.data = w.data - 0.01 * w.grad.data
# w中data与grad都为Tensor变量
w.grad.data.zero_()
# 每次反向传播(grad中)的数据要清零,否则梯度值是每次计算相加的总额
print('progress:', epoch, l.item())
print("predict (after training)", 4, forward(4).item())
homework:文章来源地址https://www.toymoban.com/news/detail-563895.html
# -*- coding: utf-8 -*-
# @Time : 2023-07-12 10:50
# @Author : yuer
# @FileName: homework04.py
# @Software: PyCharm
import torch
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
w1 = torch.Tensor([1.0])
w1.requires_grad = True
w2 = torch.Tensor([1.0])
w2.requires_grad = True
b = torch.Tensor([1.0])
b.requires_grad = True
def forward(x):
return w1 * x * x + w2 * x + b
def loss(x, y):
y_pred = forward(x)
return (y_pred - y) ** 2
for epoch in range(100):
for x, y in zip(x_data, y_data):
l = loss(x, y)
l.backward()
print('\tgrad:', x, y, w1.grad.item(), w2.grad.item(), b.grad.item())
w1.data = w1.data - 0.01 * w1.grad.data
w2.data = w2.data - 0.01 * w2.grad.data
b.data = b.data - 0.01 * b.grad.data
w1.grad.data.zero_()
w2.grad.data.zero_()
b.grad.data.zero_()
print('progress:', epoch, l.item())
print("predict (after training)", 4, forward(4).item())
到了这里,关于深度学习之反向传播的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!