TensorFlow学习_Activation_function

感觉今天进度有点快啊: )

 1 # -*- coding:UTF8 -*-
 2 
 3 import tensorflow as tf
 4 import numpy as np
 5 import matplotlib.pyplot as plt
 6 
 7 #创建输入数据
 8 x = np.linspace(-10,10,100)
 9 
10 #Activation_function的实现
11 def sigmoid(inputs):
12     for x in inputs:
13         y = 1 / (1 + np.exp(-x))
14         return y
15 
16 def relu(inputs):
17     for x in inputs:
18         if x > 0:
19             y = x
20         else:
21             y = 0
22         return y
23 
24 def tanh(inputs):
25     for x in inputs:
26         y = (np.exp(x) - np.exp(-x)) / float(np.exp(x) + np.exp(-x))
27         return y
28 
29 def softplus(inputs):
30     for x in inputs:
31         y = np.log(1 + np.exp(x))
32         return y
33 
34 #经过tensorflow的激活函数处理的各个Y值
35 y_sigmoid = tf.nn.sigmoid(x)
36 y_relu = tf.nn.relu(x)
37 y_tanh = tf.nn.tanh(x)
38 y_softplus = tf.nn.softplus(x)
39 
40 #创建会话
41 sess = tf.Session()
42 
43 #运行
44 y_sigmoid, y_relu, y_tanh, y_softplus = sess.run([y_sigmoid, y_relu, y_tanh, y_softplus])
45 
46 plt.subplot(221)
47 plt.plot(x, y_sigmoid)
48 plt.title('sigmoid')
49 
50 plt.subplot(222)
51 plt.plot(x, y_relu)
52 plt.title('relu')
53 
54 plt.subplot(223)
55 plt.plot(x, y_tanh)
56 plt.title('tanh')
57 
58 plt.subplot(224)
59 plt.plot(x, y_softplus)
60 plt.title('softplus')
61 
62 plt.show()
63 
64 #关闭会话
65 sess.close()
66 
67 #sess.close()放在最后
68 #之前放在创建窗口下面,直接导致无法打开会话窗口

函数定义那一块,我是直接按照公式写的,不知道是否存在什么问题!

欢迎大家来指正!

原文地址:https://www.cnblogs.com/AlexHaiY/p/9325776.html