tf.placeholder
x = placeholder(dtype, shape=None, name=None)
可以理解为先占位,后面用数据feed,
注意: Placeholders are not compatible with eager execution.
所以在调用时,要加上下面这一句,才可以成功运行
tf.compat.v1.disable_eager_execution()
优于版本更新,用 tf.compat.v1. 来兼容tensorflow版本1的方法。
示例:
import tensorflow as tf
import numpy as np
tf.compat.v1.disable_eager_execution()
a = tf.compat.v1.placeholder(dtype=tf.int32) # 定义一个占位符Tensor
b = tf.compat.v1.placeholder(dtype=tf.int32)
c = a + b
a_ = input("a = ") # 从终端读入一个整数并放入变量a_
b_ = input("b = ")
sess = tf.compat.v1.Session()
c_ = sess.run(c, feed_dict={a: a_, b: b_}) # feed_dict参数传入为了计算c所需要的张量的值
print("a + b = %d" % c_)