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_PIXELS)), name=‘weights‘)
      )
      biases = tf.Variable(tf.zeros([hidden1_units]), name=‘biases‘)
  • tf.placeholder:用于得到传递进来的真实的训练样本:
    • 不必指定初始值,可在运行时,通过 Session.run 的函数的 feed_dict 参数指定;
    • 这也是其命名的原因所在,仅仅作为一种占位符;
    images_placeholder = tf.placeholder(tf.float32, shape=[batch_size, IMAGE_PIXELS])
    labels_placeholder = tf.placeholder(tf.int32, shape=[batch_size])

如下则是二者真实的使用场景:

for step in range(FLAGS.max_steps):
    feed_dict = {
        images_placeholder = images_feed,
        labels_placeholder = labels_feed
    }
    _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)

当执行这些操作时,tf.Variable 的值将会改变,也即被修改,这也是其名称的来源(variable,变量)。

What’s the difference between tf.placeholder and tf.Variable

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lanchunhui/article/details/61712830

原文地址:https://www.cnblogs.com/Ph-one/p/9078821.html

时间: 2024-08-29 15:49:12

TensorFlow 辨异 —— tf.placeholder 与 tf.Variable的相关文章

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

tensorflow中使用tf.variable_scope和tf.get_variable的ValueError

ValueError: Variable conv1/weights1 already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at: 在使用tensorflow 中的tf.variable_scope和tf.get_variable搭建网络时,重复运行程序会报以上的ValueError错误,这是因为第二次运行时,内存中已经存在名字相同的层或者参数,发生了冲突,所以会提示

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

tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope关系

1. tf.Variable与tf.get_variable tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的. 当然,变量也可以通过tf.Varivale来创建.当tf.get_variable用于变量创建时,和tf.Variable的功能基本等价

【tf.keras】tf.keras使用tensorflow中定义的optimizer

我的 tensorflow+keras 版本: print(tf.VERSION) # '1.10.0' print(tf.keras.__version__) # '2.1.6-tf' tf.keras 没有实现 AdamW,即 Adam with Weight decay.论文<DECOUPLED WEIGHT DECAY REGULARIZATION>提出,在使用 Adam 时,weight decay 不等于 L2 regularization.具体可以参见 当前训练神经网络最快的方式

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,代码并不会直接生效,这

TensorFlow 学习(二)—— tf Graph tf Session 与 tf Session run

session: with tf.Session() as sess:/ tf.InteractiveSession() 初始化: tf.global_variables_initializer() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) 1 2 0. tf.Graph 命名空间与 operation name(oper.name 获取操作名): c_0 = tf.constant(0, nam

TF:利用TF的train.Saver将训练好的variables(W、b)保存到指定的index、meda文件

import tensorflow as tf import numpy as np W = tf.Variable([[2,1,8],[1,2,5]], dtype=tf.float32, name='weights') b = tf.Variable([[1,2,5]], dtype=tf.float32, name='biases') init= tf.global_variables_initializer() saver = tf.train.Saver() with tf.Sessi