tf.placeholder函数说明

函数形式:
tf.placeholder(
    dtype,
    shape=None,
    name=None
)
参数:
dtype:数据类型。常用的是tf.float32,tf.float64等数值类型
shape:数据形状。默认是None,就是一维值,也可以是多维(比如[2,3], [None, 3]表示列是3,行不定)
name:名称
为什么要用placeholder?
       Tensorflow的设计理念称之为计算流图,在编写程序时,首先构筑整个系统的graph,代码并不会直接生效,这一点和python的其他数值计算库(如Numpy等)不同,graph为静态的,类似于docker中的镜像。然后,在实际的运行时,启动一个session,程序才会真正的运行。这样做的好处就是:避免反复地切换底层程序实际运行的上下文,tensorflow帮你优化整个系统的代码。我们知道,很多python程序的底层为C语言或者其他语言,执行一行脚本,就要切换一次,是有成本的,tensorflow通过计算流图的方式,帮你优化整个session需要执行的代码,还是很有优势的。

所以placeholder()函数是在神经网络构建graph的时候在模型中的占位,此时并没有把要输入的数据传入模型,它只会分配必要的内存。等建立session,在会话中,运行模型的时候通过feed_dict()函数向占位符喂入数据。

代码示例:
import tensorflow as tf
import numpy as np

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)

output = tf.multiply(input1, input2)

with tf.Session() as sess:
print sess.run(output, feed_dict = {input1:[3.], input2: [4.]})

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}))

参考资料:https://www.jianshu.com/p/ec261a65e3c9
---------------------
作者:清晨的光明
来源:CSDN
原文:https://blog.csdn.net/kdongyi/article/details/82343712
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/jfdwd/p/11184131.html

时间: 2024-08-05 06:32:15

tf.placeholder函数说明的相关文章

TensorFlow 辨异 —— tf.placeholder 与 tf.Variable

二者的主要区别在于: tf.Variable:主要在于一些可训练变量(trainable variables),比如模型的权重(weights,W)或者偏执值(bias): 声明时,必须提供初始值: 名称的真实含义,在于变量,也即在真实训练时,其值是会改变的,自然事先需要指定初始值:  weights = tf.Variable( tf.truncated_normal([IMAGE_PIXELS, hidden1_units], stddev=1./math.sqrt(float(IMAGE_

tf.placeholder使用说明

tf.placeholder(dtype, shape=None, name=None) placeholder,占位符,在tensorflow中类似于函数参数,运行时必须传入值. dtype:数据类型.常用的是tf.float32,tf.float64等数值类型. shape:数据形状.默认是None,就是一维值,也可以是多维,比如[2,3], [None, 3]表示列是3,行不定. name:名称. 代码片段-1(计算3*4=12) [python] view plain copy #!/u

tf.placeholder

tf.placeholder tf.placeholder(    dtype,    shape=None,    name=None) Inserts a placeholder for a tensor that will be always fed. Important: This tensor will produce an error if evaluated. Its value must be fed using the feed_dict optional argument t

TensorFlow如何通过tf.device函数来指定运行每一个操作的设备?

TensorFlow程序可以通过tf.device函数来指定运行每一个操作的设备. 这个设备可以是本地的CPU或者GPU,也可以是某一台远程的服务器. TensorFlow会给每一个可用的设备一个名称,tf.device函数可以通过设备的名称,来指定执行运算的设备.比如CPU在TensorFlow中的名称为/cpu:0. 在默认情况下,即使机器有多个CPU,TensorFlow也不会区分它们,所有的CPU都使用/cpu:0作为名称. –而一台机器上不同GPU的名称是不同的,第n个GPU在Tens

tf.slice函数解析

tf.slice函数解析 觉得有用的话,欢迎一起讨论相互学习~Follow Me tf.slice(input_, begin, size, name = None) 解释 :这个函数的作用是从输入数据input中提取出一块切片,切片的尺寸是size,切片的开始位置是begin. 切片的尺寸size表示输出tensor的数据维度,其中size[i]表示在第i维度上面的元素个数. 开始位置begin表示切片相对于输入数据input_的每一个偏移量,比如数据input_是 [[[1, 1, 1],

tf.transpose函数的用法讲解

tf.transpose函数中文意思是转置,对于低维度的转置问题,很简单,不想讨论,直接转置就好(大家看下面文档,一看就懂). tf.transpose(a, perm=None, name='transpose') Transposes a. Permutes the dimensions according to perm. The returned tensor's dimension i will correspond to the input dimension perm[i]. If

tensorflow中 tf.reduce_mean函数

tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值. 参考:https://blog.csdn.net/dcrmg/article/details/79797826 原文地址:https://www.cnblogs.com/yibeimingyue/p/11517034.html

tf,cond()函数

tensorflow里面有太多的函数功能,完全像是新学一门语言,所以打算采用,遇到不会的函数就记录下来,作为自己的函数手册.这篇要讲的就是tf,cond(). tf,cond(A,B,C) 就像是条件语句:if A: B else:C,举个例子: z = tf.multiply(a, b) result = tf.cond(x < y, lambda: tf.add(x, z), lambda: tf.square(y)) 解读一下这个官方的例子,首先是给z赋值了一个简单的乘法函数,然后调用tf

TensorFlow 之tf.placeholder()

tf.placeholder( dtype, shape=None, name=None ) # In[] #dtype:被填充的张量(Tensor)的元素类型. #shape:被填充张量(Tensor)的形状(可选参数).如果没有指定张量(Tensor)打形状,你可 #以填充该张量(Tensor)为任意形状. #name:为该操作提供一个名字(可选参数). import tensorflow as tf # 定义placeholder input1 = tf.placeholder(tf.fl