PyTorchによるrandnでの乱数固定

乱数を発生させる前にtorch.manual_seedを置く

# 直前に置く
torch.manual_seed(42)
nn.Parameter(torch.randn(1, 50 + 1, 768))
>> Parameter containing:
tensor([[[ 1.9269,  1.4873,  0.9007,  ..., -1.6034, -0.4298,  0.5762],
         [ 0.3444, -3.1016, -1.4587,  ...,  0.2200,  0.3249,  1.3190],
         [-0.8497, -0.6987, -0.2052,  ..., -0.0298,  1.2715,  1.0849],
         ...,
         [-0.6430, -0.1612, -1.5933,  ..., -0.3602, -1.0423,  0.0702],
         [ 0.0594,  0.7516, -0.2885,  ...,  0.9389, -0.7241, -0.4666],
         [ 0.5249, -0.0187,  0.5637,  ...,  1.8743, -0.9375,  1.6950]]],
       requires_grad=True)

# 2回以上実行するときも直前に置くことで同じ値の乱数が発生
torch.manual_seed(42)
nn.Parameter(torch.randn(1, 50 + 1, 768))
>> Parameter containing:
tensor([[[ 1.9269,  1.4873,  0.9007,  ..., -1.6034, -0.4298,  0.5762],
         [ 0.3444, -3.1016, -1.4587,  ...,  0.2200,  0.3249,  1.3190],
         [-0.8497, -0.6987, -0.2052,  ..., -0.0298,  1.2715,  1.0849],
         ...,
         [-0.6430, -0.1612, -1.5933,  ..., -0.3602, -1.0423,  0.0702],
         [ 0.0594,  0.7516, -0.2885,  ...,  0.9389, -0.7241, -0.4666],
         [ 0.5249, -0.0187,  0.5637,  ...,  1.8743, -0.9375,  1.6950]]],
       requires_grad=True)

# 直前に無い場合は別の乱数になるので注意
nn.Parameter(torch.randn(1, 50 + 1, 768))
>> Parameter containing:
tensor([[[-0.6672, -1.7791,  1.9137,  ...,  0.6422, -0.4615,  0.8588],
         [-1.4310,  0.7049,  0.2331,  ..., -0.3112,  0.1357, -0.3704],
         [ 0.2174, -0.3104, -0.2272,  ..., -0.5745,  0.4291, -1.0543],
         ...,
         [-0.0828, -0.2698, -0.6782,  ...,  0.4300,  0.8262,  0.0927],
         [ 1.1733,  0.6565, -0.5008,  ..., -0.3682,  0.0783, -0.6028],
         [-0.4723,  0.5906,  1.0406,  ..., -2.0031,  0.2319,  1.0350]]],
       requires_grad=True)

Python,PyTorch

Posted by vastee