[TensorFlow学习笔记] tf.placeholder

tf.placeholder是一种占位符,类似于一种声明但没有初始化,在使用时需要给它赋值

tf.placeholder(
    dtype,
    shape=None,
    name=None
)

变量声明:

  • dtype:数据类型,如tf.float32
  • shape(可选):张量的维度,如[4,5]为4x5的矩阵;[None,5]为行未指定,列为5的矩阵
  • name(可选):张量的名称

e.g.


x = tf.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)

with tf.Session() as sess:
  print(sess.run(y))  # ERROR: will fail because x was not fed.

  rand_array = np.random.rand(1024, 1024)
  print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.

你可能感兴趣的:(TensorFlow)