![](/img/trans.png)
[英]Does torch.manual_seed include the operation of torch.cuda.manual_seed_all?
[英]The result is different when I apply torch.manual_seed before loading cuda() after loading the model
我試圖確保我的代碼是可重現的(總是得到相同的結果)
所以我在我的代碼之前應用了以下設置。
os.environ['PYTHONHASHSEED'] = str(args.seed)
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed) # if you are using multi-GPU.
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
通過這些設置,我總是在相同的環境和 GPU 下獲得相同的結果。
但是,當我在加載模型后應用 torch.manual_seed() 時。
torch.manual_seed(args.seed)
model = Net()
Net.cuda()
torch.manual_seed(args.seed)
model = Net()
torch.manual_seed(args.seed)
Net.cuda()
以上兩個結果是不同的。 我該如何理解這種情況?
加載模型后種子是否重新初始化?
Net.cuda()
對隨機數生成器沒有影響。 在cuda()
它只是為所有模型參數調用cuda()
。 所以基本上它是多次調用Tensor.cuda()
。
我們可以通過執行以下操作來測試:
torch.random.manual_seed(42)
x = torch.rand(1)
x.cuda()
y = torch.rand(1)
y.cuda()
print(x, y)
# the above prints the same as below
torch.random.manual_seed(42)
print(torch.rand(1), torch.rand(1))
所以這意味着Net()
正在使用數字生成器來初始化層內的隨機權重。
torch.manual_seed(args.seed)
model = Net()
print(torch.rand(1))
# the below will print a different result
torch.manual_seed(args.seed)
model = Net()
torch.manual_seed(args.seed)
print(torch.rand(1))
我建議縮小 Python 源代碼中管理隨機數的范圍。 因此,模型外部的全局代碼塊不負責內部值的生成方式。
簡單地說,將種子作為參數傳遞給模型的__init__
。
model = Net(args.seed)
print(torch.rand(1))
這將迫使開發人員在使用模型時始終提供一致性種子,如果種子並非總是必要的,您可以將參數設為可選。
我會避免一直使用相同的種子,因為您將學習使用最適合該種子的參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.