简介
在Tensorflow中,可以通过convert_to_tensor去指定使用np所生成的数据;但是如果一开始生成的就是tf的tensor数据类型,再使用convert_to_tensor去转换数据类型会报错。
代码
转换成功
import tensorflow as tf
import numpy as np
a = np.arange(0, 5)
b = tf.convert_to_tensor(a, dtype = tf.int64)
a = [3, 4]
b = tf.convert_to_tensor(a, dtype = tf.int64)
a = (5, 6)
b = tf.convert_to_tensor(a, dtype = tf.float64)
print("a: ", a)
print("b: ", b)
转换失败
import tensorflow as tf
import numpy as np
a = tf.Variable(tf.constant(5))
b = tf.convert_to_tensor(a, dtype = tf.int64)
print("a: ", a)
print("b: ", b)