nn.ReLU() 函数的inplace参数

本文对pytorch 中的nn.ReLU() 函数的inplace参数进行了研究,研究发现:
第一,inplace 默认为False;
第二,inplace 取值不影响loss 的反向传播,计算时可直接予以忽略。

import torch
import torch.nn as nn


#inplace为True,将会改变输入的数据 ,否则不会改变原输入,只会产生新的输出
np.random.seed(1)
W=np.random.randn(7,3)
W=torch.Tensor(W)
W.requires_grad=True
m = nn.ReLU(inplace=False)

input = np.random.randn(1,7)
input= torch.Tensor(input)
mid=torch.mm(input,W)
output = m(mid)
loss=torch.sum(output)

loss.backward()

print("输入处理前图片:")
print(input)
print("ReLU输出:")
print(output)
print("输出的尺度:")
print(output.size())
print("输入处理后图片:")
print(input)

print("梯度求解:")
print(W.grad)

print("中间值:")
print(mid)

输入处理前图片:
tensor([[ 1.1447, 0.9016, 0.5025, 0.9009, -0.6837, -0.1229, -0.9358]])
ReLU输出:
tensor([[1.8603, 0.7530, 0.0000]], grad_fn=)
输出的尺度:
torch.Size([1, 3])
输入处理后图片:
tensor([[ 1.1447, 0.9016, 0.5025, 0.9009, -0.6837, -0.1229, -0.9358]])
梯度求解:
tensor([[ 1.1447, 1.1447, 0.0000],
[ 0.9016, 0.9016, 0.0000],
[ 0.5025, 0.5025, 0.0000],
[ 0.9009, 0.9009, 0.0000],
[-0.6837, -0.6837, -0.0000],
[-0.1229, -0.1229, -0.0000],
[-0.9358, -0.9358, -0.0000]])
中间值:
tensor([[ 1.8603, 0.7530, -4.0126]], grad_fn=)

import torch
import torch.nn as nn


#inplace为True,将会改变输入的数据 ,否则不会改变原输入,只会产生新的输出
np.random.seed(1)
W=np.random.randn(7,3)
W=torch.Tensor(W)
W.requires_grad=True
m = nn.ReLU(inplace=True)

input = np.random.randn(1,7)
input= torch.Tensor(input)
mid=torch.mm(input,W)
output = m(mid)
loss=torch.sum(output)

loss.backward()

print("输入处理前图片:")
print(input)
print("ReLU输出:")
print(output)
print("输出的尺度:")
print(output.size())
print("输入处理后图片:")
print(input)

print("梯度求解:")
print(W.grad)

print("中间值:")
print(mid)

输入处理前图片:
tensor([[ 1.1447, 0.9016, 0.5025, 0.9009, -0.6837, -0.1229, -0.9358]])
ReLU输出:
tensor([[1.8603, 0.7530, 0.0000]], grad_fn=)
输出的尺度:
torch.Size([1, 3])
输入处理后图片:
tensor([[ 1.1447, 0.9016, 0.5025, 0.9009, -0.6837, -0.1229, -0.9358]])
梯度求解:
tensor([[ 1.1447, 1.1447, 0.0000],
[ 0.9016, 0.9016, 0.0000],
[ 0.5025, 0.5025, 0.0000],
[ 0.9009, 0.9009, 0.0000],
[-0.6837, -0.6837, -0.0000],
[-0.1229, -0.1229, -0.0000],
[-0.9358, -0.9358, -0.0000]])
中间值:
tensor([[1.8603, 0.7530, 0.0000]], grad_fn=)

import torch
import torch.nn as nn


#inplace为True,将会改变输入的数据 ,否则不会改变原输入,只会产生新的输出
np.random.seed(1)
W=np.random.randn(7,3)
W=torch.Tensor(W)
W.requires_grad=True
m = nn.ReLU()

input = np.random.randn(1,7)
input= torch.Tensor(input)
mid=torch.mm(input,W)
output = m(mid)
loss=torch.sum(output)

loss.backward()

print("输入处理前图片:")
print(input)
print("ReLU输出:")
print(output)
print("输出的尺度:")
print(output.size())
print("输入处理后图片:")
print(input)

print("梯度求解:")
print(W.grad)

print("中间值:")
print(mid)

输入处理前图片:
tensor([[ 1.1447, 0.9016, 0.5025, 0.9009, -0.6837, -0.1229, -0.9358]])
ReLU输出:
tensor([[1.8603, 0.7530, 0.0000]], grad_fn=)
输出的尺度:
torch.Size([1, 3])
输入处理后图片:
tensor([[ 1.1447, 0.9016, 0.5025, 0.9009, -0.6837, -0.1229, -0.9358]])
梯度求解:
tensor([[ 1.1447, 1.1447, 0.0000],
[ 0.9016, 0.9016, 0.0000],
[ 0.5025, 0.5025, 0.0000],
[ 0.9009, 0.9009, 0.0000],
[-0.6837, -0.6837, -0.0000],
[-0.1229, -0.1229, -0.0000],
[-0.9358, -0.9358, -0.0000]])
中间值:
tensor([[ 1.8603, 0.7530, -4.0126]], grad_fn=)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值