深度学习入门之第三章 神经网络 激活函数的设计

前言: 在本章中主要介绍神经网络(GANs)中两个重要的部分,激活函数和神经网络的输出, 

 

激活函数的作用: 在generator 和 discriminator中, 使用激活函数可以将每一层卷积或者MLP操作得到的数据进行非线性转化,使用激活函数有利于方向传播。

第一类激活函数: sigmoid函数   H(x) = 1/{1+e(-x)}

import numpy as np
import tensorflow as tf

# 直接定义sigmoid激活函数
def sigmoid(x):
    return 1/(1+ np.exp(-x))

sample = np.array([-1, 1, -2, 2])
my_result = sigmoid(sample)
print("the result of sigmoid difined by myself is: ", my_result)


sample = tf.convert_to_tensor(sample,dtype=tf.float64)
tf_result = tf.nn.sigmoid(sample)
sess = tf.Session()
print("the result of sigmoid difined by tensorflow is: ", sess.run(tf_result))

'''
输出结果是 发现结果一样 
the result of sigmoid difined by myself is:  [0.26894142 0.73105858 0.11920292 0.88079708]
the result of sigmoid difined by tensorflow is:  [0.26894142 0.73105858 0.11920292 0.88079708]
'''

 

第二类激活函数:  阶跃函数 特点就是大于0的输出1,小于0输出0 真实的深度学习中一般不会使用阶跃函数, 

import numpy as np

def step_funtion_float(x):
    if x > 0:
        return 1
    else:
        return 0

sample_f = 6
my_result = step_funtion_float(sample_f)
print("the result of step_funtion_float difined by myself is: ", my_result)
sample = np.array([-1, 1, -2, 2])
# 注意下面两句的意思就是找到numpy中大于0的为真, 并且将其转化成int型 
y = sample > 0
y = y.astype(np.int)
print("the result of step_funtion_numpy difined by myself is: ", y)

 

第三类激活函数: ReLU函数 特点大于0输出原始数据 小于0输出0 注意在真实的深度学习之中, 一般自己重写这个函数 并且将阈值设置到0.2.

import numpy as np
import tensorflow as tf

def relu(x):
    return np.maximum(0, x)
sample = np.array([-1, 1, -2, 2])
result = relu(sample)
print("the result of sigmoid difined by myself is:",result)


sample = tf.convert_to_tensor(sample,dtype=tf.float64)
tf_result = tf.nn.relu(sample)
sess = tf.Session()
print("the result of sigmoid difined by tensorflow is: ", sess.run(tf_result))

'''
数据输出结果是:
the result of sigmoid difined by myself is: [0 1 0 2]
the result of sigmoid difined by tensorflow is:  [0. 1. 0. 2.]
'''

激活函数之中多介绍一个tanh函数: 其能够将数据转化到(-1, 1)之间, 主要记住其一般用在生成器的最后一层就可以了。

如果想学习函数具体封装的可以参考这个链接, ctrl + f输入 tanh即可。

import numpy as np
import tensorflow as tf
sample = np.array([-1, 1, -2, 2])
sample = tf.convert_to_tensor(sample,dtype=tf.float64)
tf_result = tf.nn.tanh(sample)
sess = tf.Session()
print("the result of sigmoid difined by tensorflow is: ", sess.run(tf_result))

下一章将介绍神经网络输出层的设计方案:

。。。。。。。。。。。

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值