首先要了解constant的元素
constant(
value,
dtype=None,
shape=None,
name='Const',
verify_shape=False
)
参数说明:
value:这个必须填写,可以是一个值也可以是一个列表
对于一个值
num = tf.constant(1)
如果想查看num的值,需要创建一个会话,由会话去启动。会话的创建在使用之后需要释放资源,可以用with代码块来自动完成关闭动作。
num = tf.constant(1)
with tf.Session() as sess:
print(sess.run(num))
对于一个列表
list = tf.constant([2, 3, 4])
with tf.Session() as sess:
print(sess.run(list)) # [2 3 4]
dtype:选填。如果不填默认类型为value的类型,传入参数为(float32,float64....)
list = tf.constant([2, 3, 4], dtype=tf.float32)
with tf.Session() as sess:
print(sess.run(list)) # [2. 3. 4