tf.placeholder相似函数中的形参

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

此函数能够理解为形参,用于定义过程,在执行的时候再赋具体的值


参数:

    dtype:数据类型。经常使用的是tf.float32,tf.float64等数值类型
    shape:数据形状。默认是None,就是一维值,也能够是多维,好比[2,3], [None, 3]表示列是3,行不定
    name:名称。html

#coding: utf-8

import tensorflow as tf
import numpy as np

x = tf.placeholder(tf.float32, shape=(1024, 1024))  
y = tf.matmul(x, x)  
         
with tf.Session() as sess:  
      #print(sess.run(y))  # ERROR: 此处x尚未赋值.  
         
      rand_array = np.random.rand(1024, 1024)  
      print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.  

 

官方的话在feed里:TensorFlow 还提供了 feed 机制, 该机制 能够临时替代图中的任意操做中的 tensor 能够对图中任何操做提交补丁, 直接插入一个 tensor.python

feed 使用一个 tensor 值临时替换一个操做的输出结果. 你能够提供 feed 数据做为 run() 调用的参数. feed 只在调用它的方法内有效, 方法结束, feed 就会消失. 最多见的用例是将某些特殊的操做指定为 "feed" 操做, 标记的方法是使用 tf.placeholder() 为这些操做建立占位符.dom

input1 = tf.placeholder(tf.types.float32)
input2 = tf.placeholder(tf.types.float32)
output = tf.mul(input1, input2)

with tf.Session() as sess: print sess.run([output], feed_dict={input1:[7.], input2:[2.]}) # 输出: # [array([ 14.], dtype=float32)]参考:http://www.tensorfly.cn/tfdoc/get_started/basic_usage.html 
相关文章
相关标签/搜索