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)
|
1
| x = torch.zeros(5,3,dtype=torch.long)
|
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')
|
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/
|
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地址)
|