zoukankan      html  css  js  c++  java
  • 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

     
     
  • 相关阅读:
    关于firstChild,firstElementChild和children
    trim(),正则表达式中文匹配
    Shell之基本用法
    Samba服务部署
    Linux基础(3)
    linux基础(2)
    Linux基础(1)
    网络基础及网络协议
    操作系统简介
    为何要学习计算机基础
  • 原文地址:https://www.cnblogs.com/nowornever-L/p/6908775.html
Copyright © 2011-2022 走看看