placeholer是Tnesorflow的占位符节点,由placeholder方法创建,也是一种常量。由用户在调用run的防擦时才传递,可以看理解为一种形参。在使用时需要用户用字典传递常数值。
创建
X= tf.placeholder(dtype=tf.float32,shape = [144,100],name = 'X')
参数:
dtype:数据类型
shape:数据形状,选填,不填则随传入数据形状转自行变动
name:常量名,选填,默认不重复
import tensorflow as tf
import numpy.random as random
node1 = tf.placeholder(tf.float32)
node2 = tf.placeholder(tf.float32)
op = tf.multiply(node1,node2)
sess = tf.Session()
const1 = tf.constant(random.rand(4,5))
const2 = tf.constant(random.rnad(4,5))
#可以传入初始化的常量
print(session.run(op,{node1:session.run(const1),node2: session.run(const2)}))
#可以直接传入张量,其实同初始化后的常量一致
print (session.run(op, {node1:random(2,3),node2:random.rand(2,3)}))
与tf.Variable的区别
tf.Variable主要用于一些可训练变量,如模型的权重(weights)或者偏置(bias)
必须进行初始化
tf.placeholder:用于得到传递进来的真实训练样本
不必指定初始值,可在运行时,通过Session.run的函数feed_dict参数指定
可以认为为将来的输入数据占据空间