学习视频来自:https://www.bilibili.com/video/BV1Lx411j7ws?p=12
placeholder():占位符,在神经网络构建图Graph的时候在模型中占位,此时并没有把要输入的数据传入模型中,它只是分配必要的内存。建立session,在会话中,运行模型的时候通过fee_dict()函数向占位符中传入数据。
函数形式:
tf.placeholder(dtype,shape=None,name=None)
参数:
1.dtype:数据类型,常为tf.float32/64等数值类型
2.shape:数据形状。默认为None,就是一维值。
3.name:名称
eg:
import tensorflow as tf
#确保sess.run()可正常执行
tf.compat.v1.disable_eager_execution()
x1=tf.compat.v1.placeholder(tf.float32)
x2=tf.compat.v1.placeholder(tf.float32)
output=tf.multiply(x1,x2)#乘法运算
with tf.compat.v1.Session() as sess:
print( sess.run(output,feed_dict={x1:2,x2:3}))