0
点赞
收藏
分享

微信扫一扫

tf.placeholder类似函数中的形参

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

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


参数:

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

#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.

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

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

举报

相关推荐

0 条评论