**
一 tf.nn.relu( )函数
**
tf.nn.relu( )激活函数可以将小于0的数据变成0,大于0的数据保持不变.
In [2]: a = tf.constant([-1,-2,0,1,2])
In [3]: a
Out[3]: <tf.Tensor: id=0, shape=(5,), dtype=int32, numpy=array([-1, -2, 0, 1, 2], dtype=int32)>
In [4]: tf.nn.relu(a)
Out[4]: <tf.Tensor: id=2, shape=(5,), dtype=int32, numpy=array([0, 0, 0, 1, 2], dtype=int32)>
**
二 tf.sigmoid( )函数
**
tf.sogmid( )函数可以将数据转换范围到[0,1]
In [12]: a = tf.cast(tf.constant([-2,-1,0,1,2]),dtype=tf.float32)
In [13]: a
Out[13]: <tf.Tensor: id=10, shape=(5,), dtype=float32, numpy=array([-2., -1., 0., 1., 2.], dtype=float32)>
In [14]: tf.sigmoid(a)
Out[14]:
<tf.Tensor: id=12, shape=(5,), dtype=float32, numpy=
array([0.11920292, 0.26894143, 0.5 , 0.7310586 , 0.880797 ],
dtype=float32)>
**
三 tf.tanh( )函数
**
tf.tanh( )函数可以将数据转换范围到[-1,1]
In [12]: a = tf.cast(tf.constant([-2,-1,0,1,2]),dtype=tf.float32)
In [13]: a
Out[13]: <tf.Tensor: id=10, shape=(5,), dtype=float32, numpy=array([-2., -1., 0., 1., 2.], dtype=float32)>
In [14]: tf.sigmoid(a)
Out[14]:
<tf.Tensor: id=12, shape=(5,), dtype=float32, numpy=
array([0.11920292, 0.26894143, 0.5 , 0.7310586 , 0.880797 ],
dtype=float32)>
In [15]: tf.tanh(a)
Out[15]:
<tf.Tensor: id=14, shape=(5,), dtype=float32, numpy=
array([-0.9640276, -0.7615942, 0. , 0.7615942, 0.9640276],
dtype=float32)>
**
四 tf.nn.softmax( )函数
**
tf.nn.softmax( )函数将输出的数据转换为概率,使得相加概率总和为1,适用于分类.
In [12]: a = tf.cast(tf.constant([-2,-1,0,1,2]),dtype=tf.float32)
In [13]: a
Out[13]: <tf.Tensor: id=10, shape=(5,), dtype=float32, numpy=array([-2., -1., 0., 1., 2.], dtype=float32)>
In [14]: tf.sigmoid(a)
Out[14]:
<tf.Tensor: id=12, shape=(5,), dtype=float32, numpy=
array([0.11920292, 0.26894143, 0.5 , 0.7310586 , 0.880797 ],
dtype=float32)>
In [15]: tf.tanh(a)
Out[15]:
<tf.Tensor: id=14, shape=(5,), dtype=float32, numpy=
array([-0.9640276, -0.7615942, 0. , 0.7615942, 0.9640276],
dtype=float32)>
In [17]: tf.nn.softmax(a)
Out[17]:
<tf.Tensor: id=16, shape=(5,), dtype=float32, numpy=
array([0.01165623, 0.03168492, 0.08612854, 0.23412165, 0.6364086 ],
dtype=float32)>