pytorch中 += 操作被视为是原地操作
作者:互联网
Microsoft Windows [版本 10.0.18363.1316]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\chenxuqi>conda activate ssd4pytorch1_2_0
(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>>
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x0000011406BFD330>
>>>
>>> a = torch.randn(3,4,requires_grad=True)
>>> a
tensor([[ 0.2824, -0.3715, 0.9088, -1.7601],
[-0.1806, 2.0937, 1.0406, -1.7651],
[ 1.1216, 0.8440, 0.1783, 0.6859]], requires_grad=True)
>>> a = a+2
>>> a
tensor([[2.2824, 1.6285, 2.9088, 0.2399],
[1.8194, 4.0937, 3.0406, 0.2349],
[3.1216, 2.8440, 2.1783, 2.6859]], grad_fn=<AddBackward0>)
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x0000011406BFD330>
>>>
>>> a = torch.randn(3,4,requires_grad=True)
>>> a
tensor([[ 0.2824, -0.3715, 0.9088, -1.7601],
[-0.1806, 2.0937, 1.0406, -1.7651],
[ 1.1216, 0.8440, 0.1783, 0.6859]], requires_grad=True)
>>> a += 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: a leaf Variable that requires grad has been used in an in-place operation.
>>>
>>>
>>> ^Z
(ssd4pytorch1_2_0) C:\Users\chenxuqi>conda activate pytorch_1.7.1_cu102
(pytorch_1.7.1_cu102) C:\Users\chenxuqi>python
Python 3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x0000024EBCC67870>
>>>
>>> a = torch.randn(3,4,requires_grad=True)
>>> a
tensor([[ 0.2824, -0.3715, 0.9088, -1.7601],
[-0.1806, 2.0937, 1.0406, -1.7651],
[ 1.1216, 0.8440, 0.1783, 0.6859]], requires_grad=True)
>>>
>>> a = a + 2
>>> a
tensor([[2.2824, 1.6285, 2.9088, 0.2399],
[1.8194, 4.0937, 3.0406, 0.2349],
[3.1216, 2.8440, 2.1783, 2.6859]], grad_fn=<AddBackward0>)
>>> a += 2
>>> a
tensor([[4.2824, 3.6285, 4.9088, 2.2399],
[3.8194, 6.0937, 5.0406, 2.2349],
[5.1216, 4.8440, 4.1783, 4.6859]], grad_fn=<AddBackward0>)
>>>
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x0000024EBCC67870>
>>>
>>> a = torch.randn(3,4,requires_grad=True)
>>> a
tensor([[ 0.2824, -0.3715, 0.9088, -1.7601],
[-0.1806, 2.0937, 1.0406, -1.7651],
[ 1.1216, 0.8440, 0.1783, 0.6859]], requires_grad=True)
>>>
>>> a += 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: a leaf Variable that requires grad is being used in an in-place operation.
>>>
>>>
>>>
标签:tensor,requires,torch,原地,pytorch,True,操作,grad,seed 来源: https://blog.csdn.net/m0_46653437/article/details/113528037