torch

"工具"

Posted by zwt on October 28, 2020

预备知识

设置随机种子

1
2
3
4
5
6
7
8
9
10
11
import torch
import numpy as np
import random
def setup_seed(seed):
     torch.manual_seed(seed)
     torch.cuda.manual_seed_all(seed)
     np.random.seed(seed)
     random.seed(seed)
     torch.backends.cudnn.deterministic = True
# 设置随机数种子
setup_seed(20)

tensor

创建未初始化的Tensor:5*3

1
x = torch.empty(5,3)

创建随机初始化Tensor

1
x  = torch.rand(,3)

创建全为0的Tensor

1
x = torch.zeros(5,3,dtype=torch.long)

返回的tensor默认具有相同的dtype和device

1
x.new_ones(5,3,dtype=torch.float64)

指定新的数据类型

1
torch.randn_like(x, dtype=torch.float)

获取形状

1
2
torch.size()
torch.shape

加法,可以指定输出

1
2
result = torch.empty(5,3)
torch.add(y,y,out=result)

模型保存:

1
2
3
4
5
6
7
8
9
10
11
只保存参数:
# 保存
torch.save(model.state_dict(), '\parameter.pkl')
# 加载
model = TheModelClass(...)
model.load_state_dict(torch.load('\parameter.pkl'))
保存完整模型:
# 保存
torch.save(model, '\model.pkl')
# 加载
model = torch.load('\model.pkl')

sentence2teansformer

转onnx

1
2
3
pip install transformers[onnx]
python -m transformers.onnx --model=./models/my-128dim-model onnx/
python -m transformers.onnx --model=./output/training_multi-task-learning2 --atol=2e-5 onnx/v1/

tensorboard

1
2
3
4
5
6
7
8
9
10
11
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter('./runs')
writer.add_scalar('LOSS/Train_loss', float(avg_train_loss),(epoch + 1))
writer.add_scalar('LOSS/Valid_loss', float(avg_valid_loss), (epoch + 1))
writer.add_scalar('ACC/Train_lacc', float(avg_train_acc), (epoch + 1))
writer.add_scalar('ACC/Valid_lacc', float(avg_valid_acc), (epoch + 1))

cd runs/
tensorboard --logdir ./(writer地址)
或:
tensorboard --logdir=./(writer地址)

参考

  1. 深度学习之PyTorch实战-基础学习及搭建环境