Tensorflow常用的函数:tf.cast

时间:2024-01-14 19:36:50

1.tf.cast(x,dtype,name)

此函数的目的是为了将x数据,准换为dtype所表示的类型,例如tf.float32,tf.bool,tf.uint8等

example:  import tensorflow as tf

    x = tf.Variable([True,True,False,False])

    y = tf.cast(x,dtype = tf.float32)

    sess = tf.Session()

    init = tf.global_variables_initializer()

    sess.run(init)

    print(sess.run(y))

然后我们得到的结果就是[1,1,0,0],显然我们将布尔型的数据转换为float类型,如果将输入的变量定义为[1,1,0,0],那么输出的值就是布尔类型True/False