17 - 激活函数及其梯度
1个月前 • 101次点击 • 来自 TensorFlow
收录专题: TensorFlow入门笔记
tf.sigmoid
sigmoid函数也叫Logistic函数,用于隐层神经元输出,取值范围为(0,1),它可以将一个实数映射到(0,1)的区间,可以用来做二分类。
import tensorflow as tf
a = tf.linspace(-10., 10., 10)
with tf.GradientTape() as tape:
tape.watch(a)
y = tf.sigmoid(a)
grads = tape.gradient(y, [a])
print("a:", a)
print("y:", y)
print("grads: ", grads)
数据结果
a: tf.Tensor(
[-10. -7.7777777 -5.5555553 -3.333333 -1.1111107 1.1111116
3.333334 5.5555563 7.7777786 10. ], shape=(10,), dtype=float32)
y: tf.Tensor(
[4.5397868e-05 4.1876672e-04 3.8510328e-03 3.4445208e-02 2.4766390e-01
7.5233626e-01 9.6555483e-01 9.9614894e-01 9.9958128e-01 9.9995458e-01], shape=(10,), dtype=float32)
grads: [<tf.Tensor: shape=(10,), dtype=float32, numpy=
array([4.5395806e-05, 4.1859134e-04, 3.8362022e-03, 3.3258736e-02,
1.8632649e-01, 1.8632641e-01, 3.3258699e-02, 3.8362255e-03,
4.1854731e-04, 4.5416677e-05], dtype=float32)>]
tf.tanh
tanh是双曲函数中的一个,tanh为双曲正切。在数学中,双曲正切“tanh”是由双曲正弦和双曲余弦这两种基本双曲函数推导而来。
tanh(x) = 2sigmoid(2x) - 1
此激活函数常用于RNN
import tensorflow as tf
a = tf.linspace(-10., 10., 10)
tf.tanh(a)
tf.nn.relu
ReLU - Rectified Linear Unit 线性整流函数
import tensorflow as tf
a = tf.linspace(-10., 10., 10)
tf.nn.relu(a)
tf.nn.leaky_relu(a)
标签