tensorflow中tf.cast()用法

tf.cast(x,  dtype,  name=None)

将x的数据格式转化成dtype数据类型.例如,原来x的数据格式是bool.
参数
x:输入
dtype:转换目标类型
name:名称
返回:Tensor

import tensorflow as tf
a = tf.Variable([1.0, 1.3, 2.1, 3.41, 4.51])
b = tf.cast(a, dtype=tf.int8)
c = tf.cast(a > 3, dtype=tf.bool)
d = tf.cast(a > 3, dtype=tf.int8)
e = tf.cast(a < 2, dtype=tf.float32)

sess = tf.Session()
sess.run(tf.initialize_all_variables())
print(sess.run(b))
print(sess.run(c))
print(sess.run(d))
print(sess.run(e))

输出:

[1 1 2 3 4]
[False False False  True  True]
[0 0 0 1 1]
[1. 1. 0. 0. 0.]

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值