深度学习步骤
搭建模型的workspace
Dataset
dataset中的getitem会自动把数据转换成tensor类型
config
在计算机领域中,config通常指的是配置文件(configuration file),也称作配置项或配置信息。配置文件是一种文本文件,用于存储应用程序或系统的各种配置信息,例如网络配置、数据库配置、日志配置、安全配置等等。
包括:Data, Model Structure, Experiment
二分类问题
输出两个节点 表示分类一和分类二的概率
onehot编码
from torch.nn import functional as F
data_y = F.one_hot(data_y) # one hot encode
""" shape: [n_item, 2]
tensor([[1, 0],
[1, 0],
[0, 1],
...,
[1, 0],
[1, 0],
[1, 0]])
"""
nn.CrossentropyLoss()
criterion = nn.CrossEntropyLoss()
不需要进行onehot操作
感知机
包括:线性层+激活函数 —>输出
其中nn.Linear()就是线性层,不包括激活函数
class BinaryClassificationModel(nn.Module):
def __init__(self, in_feature):
super(BinaryClassificationModel, self).__init__()
""" single perceptron
self.layer_1 = nn.Linear(in_features=in_feature, out_features=2, bias=True)
"""
""" multi perceptron """
self.layer_1 = nn.Linear(in_features=in_feature, out_features=128, bias=True)
self.layer_2 = nn.Linear(in_features=128, out_features=512, bias=True)
# 。。。
self.layer_final = nn.Linear(in_features=512, out_features=2, bias=True)
def forward(self, x):
layer_1_output = F.sigmoid(self.layer_1(x))
layer_2_output = F.sigmoid(self.layer_2(layer_1_output))
output = F.sigmoid(self.layer_final(layer_2_output))
return output
torch.nn
nn.ModuleList()
损失函数
nn.BCEloss() 二分类损失函数
epoch和step的区别
在深度学习模型的训练过程中,通常会使用epoch和step来衡量训练的进度。
epoch: 指的是模型在整个训练集上训练的次数,也就是将整个训练集数据都用来训练一遍的次数。例如,一个epoch等于将训练数据集中的所有数据都传入模型中进行了一次前向计算和反向传播计算。一般来说,训练epoch数越多,模型的表现会越好,但是也会增加过拟合的风险。
step: 指的是每次迭代更新模型参数的次数。一个step等于使用一个batch的数据进行一次前向计算和反向传播计算,然后更新模型参数的一次迭代。例如,一个batch大小为32,训练集数据总数为10000,则一个epoch的step数为10000/32=312.5,实际通常会向下取整,即为312。
在训练过程中,通常会设置epoch数和step数的上限,当达到上限时训练结束。同时,还可以根据验证集的表现来进行早期停止等策略,以防止过拟合的出现。
tensorboardX的使用
使用cd一步步进入到log文件夹,再使用tensorboard --logdir=./
进入到 http://localhost:6006/ 中
数据集的划分
正确的初始化参数
激活函数
尽量使用mish激活函数
优化器
SGD 所有参数共用一个学习率,vt表示动量。
Ada 每个参数都有自己的学习率,随着步数的增加,学习率会慢慢下降。
check point 的设置
def save_checkpoint(model_, epoch_, optm, checkpoint_path):
save_dict = {
'epoch': epoch_,
'model_state_dict': model_.state_dict(),
'optimizer_state_dict': optm.state_dict()
}
torch.save(save_dict, checkpoint_path)
if not step % HP.save_step: # model save
model_path = 'model_%d_%d.pth' % (epoch, step)
save_checkpoint(model, epoch, opt, os.path.join('model_save', model_path))
Q: .pth是什么文件
A: ".pth"是PyTorch中的模型文件扩展名。它包含了模型的所有参数、权重和偏差等信息。在PyTorch中,我们可以通过保存模型的state_dict(包含模型的参数)或者直接保存整个模型来生成.pth文件,以便在将来使用模型时加载它们。
Q: 用什么方法保存
**A:**可以使用PyTorch中的torch.save()函数将模型的参数保存到.pth文件中。该函数接受两个参数:第一个参数是要保存的Python字典对象,第二个参数是要保存的文件路径。在保存模型参数时,通常会将模型的状态字典(state_dict)、优化器的状态字典(如果使用了优化器)以及当前的epoch数保存在一起,以便在恢复模型时可以同时加载这些信息。
Normalization
希望每一层的输出都是独立同分布的