关于tensorflow的placeholder

使用方法

官方的解释:(我这里为了清晰直接在代码上进行注释)

>>> import tensorflow as tf
>>> help(tf.placeholder)
#它的功能主要在于制定一个数据,并且在其中填充占位符,指定数据类型之后更加方便内存空间的分配
Help on function placeholder in module tensorflow.python.ops.array_ops:
#可以指定的参数类型:数据类型,数组形状,名称
placeholder(dtype, shape=None, name=None)
    #先定义一个张量,后续再对数据进行填充
    Inserts a placeholder for a tensor that will be always fed.
    #重点:它的值需要用feed_dict功能的参数指定,并且用Session.run(),Tensor.eval(),operation.run()等来运行和赋值
    **Important**: This tensor will produce an error if evaluated. Its value must
    be fed using the `feed_dict` optional argument to `Session.run()`,
    `Tensor.eval()`, or `Operation.run()`.
    #实例
    For example:
    
    ```python
    #首先给定占位符(tensorflow的图节点,x,y都是节点)
    x = tf.placeholder(float, shape=(1024, 1024))
    y = tf.matmul(x, x)
    
    with tf.Session() as sess:
      #由于没有用feed_dict来进行赋值,因此无法进行运算,会报错
      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:
      #数据类型(numpy数组中的数据都是同一类型的)
      dtype: The type of elements in the tensor to be fed.
      #指定形状(利用一个元组的形式,如(10,10))
      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).
    #返回值
    Returns:
      #返回一个张量的句柄,可以对其进行赋值,但是不能直接进行运算
      A `Tensor` that may be used as a handle for feeding a value, but not
      evaluated directly.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值