TensorFlow2.0:常用数据范围压缩函数

**

一 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)>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值