本文对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=)