在最新版本1.0.3,上 遇到d2l.torch库里面缺失train_ch3函数,下面是个人写的替代补充函数可以完全平替。
所有函数都放在util.py文件中文章来源:https://www.toymoban.com/news/detail-743737.html
import torch.nn
from d2l import torch as d2l
from IPython import display
class Accumulator:
"""
在n个变量上累加
"""
def __init__(self, n):
self.data = [0.0] * n # 创建一个长度为 n 的列表,初始化所有元素为0.0。
def add(self, *args): # 累加
self.data = [a + float(b) for a, b in zip(self.data, args)]
def reset(self): # 重置累加器的状态,将所有元素重置为0.0
self.data = [0.0] * len(self.data)
def __getitem__(self, idx): # 获取所有数据
return self.data[idx]
def accuracy(y_hat, y):
"""
计算正确的数量
:param y_hat:
:param y:
:return:
"""
if len(y_hat.shape) > 1 and y_hat.shape[1] > 1:
y_hat = y_hat.argmax(axis=1) # 在每行中找到最大值的索引,以确定每个样本的预测类别
cmp = y_hat.type(y.dtype) == y
return float(cmp.type(y.dtype).sum())
def evaluate_accuracy(net, data_iter):
"""
计算指定数据集的精度
:param net:
:param data_iter:
:return:
"""
if isinstance(net, torch.nn.Module):
net.eval() # 通常会关闭一些在训练时启用的行为
metric = Accumulator(2)
with torch.no_grad():
for X, y in data_iter:
metric.add(accuracy(net(X), y), y.numel())
return metric[0] / metric[1]
class Animator:
"""
在动画中绘制数据
"""
def __init__(self, xlabel=None, ylabel=None, legend=None, xlim=None,
ylim=None, xscale='linear', yscale='linear',
fmts=('-', 'm--', 'g-', 'r:'), nrows=1, ncols=1,
figsize=(3.5, 2.5)):
# 增量的绘制多条线
if legend is None:
legend = []
d2l.use_svg_display()
self.fig, self.axes = d2l.plt.subplots(nrows, ncols, figsize=figsize)
if nrows * ncols == 1:
self.axes = [self.axes, ]
# 使用lambda函数捕获参数
self.config_axes = lambda: d2l.set_axes(
self.axes[0], xlabel, ylabel, xlim, ylim, xscale, yscale, legend
)
self.X, self.Y, self.fmts = None, None, fmts
def add(self, x, y):
"""
向图表中添加多个数据点
:param x:
:param y:
:return:
"""
if not hasattr(y, "__len__"):
y = [y]
n = len(y)
if not hasattr(x, "__len__"):
x = [x] * n
if not self.X:
self.X = [[] for _ in range(n)]
if not self.Y:
self.Y = [[] for _ in range(n)]
for i, (a, b) in enumerate(zip(x, y)):
if a is not None and b is not None:
self.X[i].append(a)
self.Y[i].append(b)
self.axes[0].cla()
for x, y, fmt in zip(self.X, self.Y, self.fmts):
self.axes[0].plot(x, y, fmt)
self.config_axes()
display.display(self.fig)
display.clear_output(wait=True)
def train_epoch_ch3(net, train_iter, loss, updater):
"""
训练模型一轮
:param net:是要训练的神经网络模型
:param train_iter:是训练数据的数据迭代器,用于遍历训练数据集
:param loss:是用于计算损失的损失函数
:param updater:是用于更新模型参数的优化器
:return:
"""
if isinstance(net, torch.nn.Module): # 用于检查一个对象是否属于指定的类(或类的子类)或数据类型。
net.train()
# 训练损失总和, 训练准确总和, 样本数
metric = Accumulator(3)
for X, y in train_iter: # 计算梯度并更新参数
y_hat = net(X)
l = loss(y_hat, y)
if isinstance(updater, torch.optim.Optimizer): # 用于检查一个对象是否属于指定的类(或类的子类)或数据类型。
# 使用pytorch内置的优化器和损失函数
updater.zero_grad()
l.mean().backward() # 方法用于计算损失的平均值
updater.step()
else:
# 使用定制(自定义)的优化器和损失函数
l.sum().backward()
updater(X.shape())
metric.add(float(l.sum()), accuracy(y_hat, y), y.numel())
# 返回训练损失和训练精度
return metric[0] / metric[2], metric[1] / metric[2]
def train_ch3(net, train_iter, test_iter, loss, num_epochs, updater):
"""
训练模型()
:param net:
:param train_iter:
:param test_iter:
:param loss:
:param num_epochs:
:param updater:
:return:
"""
animator = Animator(xlabel='epoch', xlim=[1, num_epochs], ylim=[0.3, 0.9],
legend=['train loss', 'train acc', 'test acc'])
for epoch in range(num_epochs):
trans_metrics = train_epoch_ch3(net, train_iter, loss, updater)
test_acc = evaluate_accuracy(net, test_iter)
animator.add(epoch + 1, trans_metrics + (test_acc,))
train_loss, train_acc = trans_metrics
print(trans_metrics)
def predict_ch3(net, test_iter, n=6):
"""
进行预测
:param net:
:param test_iter:
:param n:
:return:
"""
global X, y
for X, y in test_iter:
break
trues = d2l.get_fashion_mnist_labels(y)
preds = d2l.get_fashion_mnist_labels(net(X).argmax(axis=1))
titles = [true + "\n" + pred for true, pred in zip(trues, preds)]
d2l.show_images(
X[0:n].reshape((n, 28, 28)), 1, n, titles=titles[0:n]
)
d2l.plt.show()
直接调用即可
找个位置放就行
文章来源地址https://www.toymoban.com/news/detail-743737.html
到了这里,关于补充d2l.torch库里面缺失train_ch3函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!