小白干GAN系列1——ReLU函数

  因为之前没有学过任何有关机器学习的知识,直接看GAN的代码有点难,所以准备开这个小白系列,记录下自己遇到的各种小知识点,如果理解错了,请见笑,纯小白。

  ReLU是torch.nn里面的一个函数,格式为:nn.ReLU(inplace=True)

  参数inplace=True:当inplace为True,将会改变输入的数据,否则不会改变原输入,只会产生新的输出。

  举个例子:

import torch
import torch.nn as nn
 
input = torch.randn(5)
print('输入处理前:\n', input, input.size())
print('*'*70)
 
print("Default. inplace=False:")
output_F = nn.ReLU(inplace=False)(input)
print('输入:\n', input, input.size())
print('输出:\n', output_F, output_F.size())
 
print('*'*70)
 
print("inplace=True:")
output_T = nn.ReLU(inplace=True)(input)
print('输入:\n', input, input.size())
print('输出:\n', output_T, output_T.size())
运行结果:
 
输入处理前:
 tensor([-2.2577,  0.1088, -0.9960,  0.4097, -0.3411]) torch.Size([5])
**********************************************************************
Default. inplace=False:
输入:
 tensor([-2.2577,  0.1088, -0.9960,  0.4097, -0.3411]) torch.Size([5])
输出:
 tensor([0.0000, 0.1088, 0.0000, 0.4097, 0.0000]) torch.Size([5])
**********************************************************************
inplace=True:
输入:
 tensor([0.0000, 0.1088, 0.0000, 0.4097, 0.0000]) torch.Size([5])
输出:
 tensor([0.0000, 0.1088, 0.0000, 0.4097, 0.0000]) torch.Size([5])

  一开始并不能理解为什么输入跟输出的负值都变为了0,查阅后知道了下面这个函数,relu是非线性激活函数。

image-20201103164206849

posted @ 2020-11-03 16:46  崔可爱的胖肉肉  阅读(380)  评论(0编辑  收藏  举报