tf.equal()、tf.cast()和tf.reduce_mean 实现卷积神经网络求精度

equal(x, y, name=None)
逐元素的比较x和y是否相等,所以要求x和y的维度相等,并且要求x和y的数据类型相同,否则报错。返回的是跟x、y维度相同的True/False矩阵

>>> import tensorflow as tf
>>> sess=tf.Session()
>>> sess.run(tf.equal([1,2,3,4],[1,3,4,5]))
array([ True, False, False, False])

tf.cast( x,dtype,name=None)
实现数据类型的转换,将x的数据类型转换为dtype格式,比如将整型转为浮点型

>>> import tensorflow as tf
>>> sess=tf.Session()
>>> sess.run(tf.cast([1,2,3,4],dtype=tf.float32))
array([1., 2., 3., 4.], dtype=float32)

将布尔型转换为浮点型

>>> sess.run(tf.cast([False,True,True],dtype=tf.float32))
array([0., 1., 1.], dtype=float32)

tf.reduce_mean(input_tensor,axis=None,keep_dims=False,name=None,reduction_indices=None)

参数:
input_tenor: 输入的参数
axis: 在该维度上对input_tensor求平均,所以output中该维度被消去,若不指定维度,则对所有维度上求平均
keep_dims: 是否要保留原来的形状。如果为True,则axis的那一维变成1
reduction_indices:在以前版本中用来指定轴,已弃用

有一点需要注意的是,output和input的数据类型相同,所以如果想得到浮点类型的数据,输入一定要为浮点型,不能为整型

代码:

if __name__ == '__main__':    
    sess = tf.Session()

    a = [[[1, 2, 3, 4], [4, 5, 6, 7]], [[3, 4, 5, 6], [0, 9, 8, 7]],
         [[2, 3, 4, 5], [6, 7, 8, 5]]]  # shape=[3,2,4]
    b = tf.reduce_mean(a, axis=0, keep_dims=False)  # shape=(2,4)
    c = tf.reduce_mean(a, axis=1, keep_dims=False)  # shape=(3,4)
    d = tf.reduce_mean(a, axis=2, keep_dims=False)  # shape=(2,3)
    e = tf.reduce_mean(a, keep_dims=False)  # shape=一个数
    print('axis=0 and keep_dims=False:', b.get_shape(), '\naxis=1 and keep_dims=False:', c.get_shape(),
          '\naxis=2 and keep_dims=False:', d.get_shape(), '\naxis=None and keep_dims=False:', e.get_shape())
    print('axis=0 and keep_dims=False的值\n', sess.run(b), '\naxis=None and keep_dims=False的值\n', sess.run(e))
    b = tf.reduce_mean(a, axis=0, keep_dims=True)  # shape=(1,2,4)
    c = tf.reduce_mean(a, axis=1, keep_dims=True)  # shape=(3,1,4)
    d = tf.reduce_mean(a, axis=2, keep_dims=True)  # shape=(2,3,1)
    e = tf.reduce_mean(a, keep_dims=True)  # shape=(1,1,1)
    print('axis=0 and keep_dims=True:', b.get_shape(), '\naxis=1 and keep_dims=True:', c.get_shape(),
          '\naxis=2 and keep_dims=True:', d.get_shape(), '\naxis=None and keep_dims=True:', e.get_shape())
    print('axis=0 and keep_dims=True的值\n', sess.run(b), '\naxis=None and keep_dims=True的值\n', sess.run(e))

输出为:

axis=0 and keep_dims=False: (2, 4) 
axis=1 and keep_dims=False: (3, 4) 
axis=2 and keep_dims=False: (3, 2) 
axis=None and keep_dims=False: ()
axis=0 and keep_dims=False的值
 [[2 3 4 5]
 [3 7 7 6]] 
axis=None and keep_dims=False的值
 4
axis=0 and keep_dims=True: (1, 2, 4) 
axis=1 and keep_dims=True: (3, 1, 4) 
axis=2 and keep_dims=True: (3, 2, 1) 
axis=None and keep_dims=True: (1, 1, 1)
axis=0 and keep_dims=True的值
 [[[2 3 4 5]
  [3 7 7 6]]] 
axis=None and keep_dims=True的值
 [[[4]]]

在实现神经网络模型求精度的时候,对于每一个batch的数据,
首先用true_or_false=tf.equal(output_y,lables)
其次用0_or_1=tf.cast(true_or_false,tf.float32) 这个地方一定要把它转换成浮点型的,不然下面求平均的时候只能得到0、1值
最后用tf.reduce_mean(0_or_1)求精度

acc_true_or_false = tf.equal(self.input_y, self.output_y)
self.accuracy = tf.reduce_mean(tf.cast(acc_true_or_false, tf.float32))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值