路径表示方法
# 绝对路径
root_dir = 'F:\dataset\dnn\\'
# 相对路径
output_dir = './output\\'
torch.FloatTensor
class COVID19Dataset(Dataset):
def __init__(self, x, y=None):
if y is None:
self.y = y
else:
# 读取csv的数据转化为tensor,dtype=float,否则整型不能计算梯度
self.y = torch.FloatTensor(y)
self.x = torch.FloatTensor(x)
自定义数据集
import torch
from torch.utils.data import Dataset
# 继承Dataset,Dataloader取数据的时候调用getitem方法
class COVID19Dataset(Dataset):
def __init__(self,x,y):
def __getitem__(self,idx):
def __len__(self):
定义模型结构
import torch.nn as nn
# 直接调用model(x)是调用model的__call__方法
# 而继承nn.Module之后,改为调用forward方法
class My_Model(nn.Module):
def __init__(self, input_dim):
super(My_Model, self).__init__()
def forward(self, x):
torch.squeeze
# 去除降维为一的维度
# input_dim - 16 - 8 - 1 , 得到(Batch_zie,1)维度的向量,但实际上我们需要1行Batch_size个向量
# 把二维降维成了一维
# (B, 1) -> (B)
x = x.squeeze(1)
加载模型
net = model(input).to(device)
net.load_state_dict(torch.load('xxx.ckpt'))
损失函数
# 结果是多个向量形式
criterion = nn.MSELoss(reduction='none')
# 缺省值,默认是总损失的平均值
criterion = nn.MSELoss(reduction='mean')
# 得到总损失
criterion = nn.MSELoss(reduction='sum')
优化器
optimizer = torch.optim.Adam(params = model.parameters(),
lr= 1e-5,
weight_decay= 1e-5)
训练过程
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
# 记录训练曲线
writer = SummaryWriter()
for 迭代轮次:
# 启动BN和DropOut
net.train()
# 可视化训练进度
train_pbar = tqdm(train_loader, position=0, leave=True)
for x,y in train_pbar:
# 梯度置0,只希望比较本次与上次的梯度变化
optimizer.zero_grad()
# 数据加载至GPU
x, y = x.to(device), y.to(device)
# 加载模型
pred = model(x)
# 计算损失,(预测值,真实值)
loss = criterion(pred, y)
# 反向传播
loss.backward()
# 更新参数
optimizer.step()
step += 1
# 从计算图中截取loss的值,剔除梯度信息
loss_record.append(loss.detach().item())
train_pbar.set_description(f'Epoch [{epoch +1}/{n_epochs}]')
train_pbar.set_postfix({'loss': loss.detach().item()})
mean_train_loss = sum(loss_record ) /len(loss_record)
writer.add_scalar('Loss/train', mean_train_loss, step)
验证/测试过程
net.eval()
for x, y in valid_loader:
x, y = x.to(device), y.to(device)
with torch.no_grad():
pred = model(x)
loss = criterion(pred, y)
随机数种子代码
def same_seed(seed):
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
保存模型
torch.save(model.state_dict(), config['save_path'])
标签分割
# 所有行的最后一列(label)
y_train = train_data[:, -1]
# 所有行除去最后一列(label)的所有列
raw_x_train = train_data[:, :-1]