(2_)Pytorch随机数种子、opt.parser

opt.parser

1、action = ‘store_true’

(2_)Pytorch随机数种子、opt.parser_第1张图片

2、可选参数和必选参数

args 分为可选参数和必选参数。-- 指定可选参数,不加 – 指定的是必选参数(等于“位置参数”)。

相应的参数为必须手动指定。此时即使通过default设置默认参数,也还是会报错!!

parser.add_argument('data', metavar='DIR', help='path to dataset')

随机数种子

torch.manual_seed():为CPU设置随机数种子

torch.cuda.manual_seed():为GPU设置随机数种子

torch.cuda.manual_seed_all():为所有的GPU设置随机数种子

random.seed():为random模块的随机数种子

说明

  1. torch.manual_seed() 一般和 torch.rand()、torch.randn() 等函数搭配使用。
  2. 通过指定seed值,可以令每次生成的随机数相同,从而方便复现实验结果
  3. 设置随机种子后,是每次运行 py 文件的输出结果都一样,而不是每次随机函数生成的结果一样。
import torch
torch.manual_seed(0)
print(torch.rand(1, 2))	# 返回一个张量,包含了从区间[0, 1)的均匀分布中抽取的一组随机数
print(torch.randn(1, 2)) # 返回一个张量,包含了均值0,方差1的正态分布中抽取的一组随机数
# torch.manual_seed(0)
print(torch.rand(1, 2))
print(torch.randn(1, 2))

原文链接:

.detach() .data

Pytorch中.detach()与.data的用法

在这里插入图片描述

你可能感兴趣的:(pytorch基础知识积累,pytorch,深度学习)