ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

pytorch中 += 操作被视为是原地操作

2021-02-01 19:59:27  阅读:302  来源: 互联网

标签:tensor requires torch 原地 pytorch True 操作 grad seed


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有