- 设置随机种子
def seed_reproducer(seed=42):
"""Reproducer for pytorch experiment.
Parameters
----------
seed: int, optional (default = 2019)
Radnom seed.
Example
-------
seed_reproducer(seed=2019).
"""
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.enabled = True
- labelsmooth with CrossEntropyLoss
class LabelSmoothLoss(nn.Module):
def __init__(self, smoothing=0.0):
super(LabelSmoothLoss, self).__init__()
self.smoothing = smoothing
def forward(self, input, target):
log_prob = F.log_softmax(input, dim=-1)
weight = input.new_ones(input.size()) * \
self.smoothing / (input.size(-1) - 1.)
weight.scatter_(-1, target.unsqueeze(-1), (1. - self.smoothing))
loss = (-weight * log_prob).sum(dim=-1).mean()
return loss
- 梯度裁剪
def clip_gradient(optimizer, grad_clip=1):
"""
Clips gradients computed during backpropagation to avoid explosion of gradients.
:param optimizer: optimizer with the gradients to be clipped
:param grad_clip: clip value
"""
for group in optimizer.param_groups:
for param in group["params"]:
if param.grad is not None:
param.grad.data.clamp_(-grad_clip, grad_clip)
- 导出onnx
def test():
model = SiameseNetworkOutput()
pthfile = os.path.join(Config.save_dir,'3e47_loss0.23540_acc0.96000.pth')
model.load_state_dict(torch.load(pthfile, map_location='cpu'), strict=True)
#data type nchw
dummy_input1 = torch.randn(1, 3, 100, 100)
input_names = [ "input1"] #自己命名
output_names = [ "output1" ]
# torch.onnx.export(model, (dummy_input1, dummy_input2, dummy_input3), "C3AE.onnx", verbose=True, input_names=input_names, output_names=output_names)
torch.onnx.export(model, dummy_input1, "output/mymodel.onnx", verbose=True, input_names=input_names, output_names=output_names)
- 多维数组按指定维度索引列表取值:numpy多维数组索引
-
Pytorch 入门示例:
Note:This is part of this repo Data-Science-Notes.