tf.placeholder()用法

本文详细介绍了TensorFlow中Placeholder的功能和使用方法,解释了如何在计算图中预留位置供外部输入数据,并通过实例演示了其在矩阵乘法运算中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

感谢:https://tensorflow.google.cn/api_docs/python/tf/placeholder

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

Inserts a placeholder for a tensor that will be always fed.

placeholder是TensorFlow中的占位符,暂时存储变量。TensorFlow如果要从外部传入数据,需要用tf.placeholder()

Important: This tensor will produce an error if evaluated. Its value must be fed using the feed_dictoptional argument to Session.run()Tensor.eval(), or Operation.run()

需要注意的是:placeholder()和sess.run(***,feed_dict={input1 : ***,input2 : ***})绑定了,必须要用这种形式传输数据。

For example:

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.

Args:

  • dtype: The type of elements in the tensor to be fed.
  • shape: The shape of the tensor to be fed (optional). If the shape is not specified, you can feed a tensor of any shape.
  • name: A name for the operation (optional).

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值