注意pytorch的原地操作

常见的原地操作:nn.ReLu(inplace=True)

python 复制代码
a = torch.randn(2)	# tensor([-0.3690,  0.0626])
b = a.clone() # tensor([-0.3690,  0.0626])
c = a	# tensor([-0.3690,  0.0626])
relu = nn.ReLu(inplace=True)

情况1

python 复制代码
out = relu(a) # tensor([0.0000, 0.0626])
a	# tensor([0.0000, 0.0626])
b	# tensor([-0.3690,  0.0626])
c	# tensor([0.0000, 0.0626])

如果没有clone, c则会变化

情况2

python 复制代码
out1 = a + relu(a)	# tensor([-0.3690, 0.1252])
out2 = relu(a) + a	# tensor([0, 0.1252])

两个完全不同结果

相关推荐
BBB努力学习程序设计1 天前
从文本中精准提取手机号并脱敏:Python 正则 + 文件流的实战进阶
python
BBB努力学习程序设计1 天前
Python文件操作完全指南:读写文件与数据处理
python·pycharm
vv_Ⅸ1 天前
打卡day47
python
zhongtianhulian1 天前
陶瓷行业大会资讯:掌握行业动态,洞察未来趋势
大数据·人工智能·python
小鸡吃米…1 天前
Python的人工智能-入门指南
python
汗流浃背了吧,老弟!1 天前
把 Bert 回炉重铸——基于Bert的生成式任务训练
人工智能·深度学习·bert
ZiLing1 天前
为什么 AI Agent 需要执行层熔断器?——一次 LangChain 事故复盘
人工智能
haiyu_y1 天前
Day 49 随机函数与广播机制
人工智能·pytorch·深度学习
喜欢吃豆1 天前
深度解析DeepSeek大语言模型架构演进——从多头注意力机制到 DeepSeek 核心技术体系 (DeepSeek-MoE, MTP, MLA)
人工智能·语言模型·架构·大模型·deepseek
Elwin Wong1 天前
关于熵的一些概念及其计算
人工智能·大模型·llm