TensorFlow常用激活函數

threshold

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
 
def threshold(x):
    cond = tf.less(x, tf.zeros(x.shape, dtype=x.dtype))
    res = tf.where(cond, tf.zeros(x.shape), tf.ones(x.shape))
    return res

h = np.linspace(-1,1,50)
out = threshold(h)

with tf.Session() as sess:
    y = sess.run(out)
    plt.xlabel('Activity of Neuron')
    plt.ylabel('Output of Neuron')
    plt.title('Threshold Activation Function')
    plt.plot(h, y)
    plt.show()

在這裏插入圖片描述

Sigmoid

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
 
h = np.linspace(-10,10,50)
out = tf.sigmoid(h)

with tf.Session() as sess:
    y = sess.run(out)
    plt.xlabel('Activity of Neuron')
    plt.ylabel('Output of Neuron')
    plt.title('Sigmoid Activation Function')
    plt.plot(h, y)
    plt.show()

在這裏插入圖片描述

Tanh

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
 
h = np.linspace(-10,10,50)
out = tf.tanh(h)

with tf.Session() as sess:
    y = sess.run(out)
    plt.xlabel('Activity of Neuron')
    plt.ylabel('Output of Neuron')
    plt.title('Tanh Activation Function')
    plt.plot(h, y)
    plt.show()

在這裏插入圖片描述

RELU

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
 
h = np.linspace(-10,10,50)
out = tf.nn.relu(h)

with tf.Session() as sess:
    y = sess.run(out)
    plt.xlabel('Activity of Neuron')
    plt.ylabel('Output of Neuron')
    plt.title('RELU Activation Function')
    plt.plot(h, y)
    plt.show()

在這裏插入圖片描述

Softmax

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
 
h = np.linspace(-5,5,50)
out = tf.nn.softmax(h)

with tf.Session() as sess:
    y = sess.run(out)
    plt.xlabel('Activity of Neuron')
    plt.ylabel('Output of Neuron')
    plt.title('Softmax Activation Function')
    plt.plot(h, y)
    plt.show()

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章