简体   繁体   English

tf.variable和tf.constant

[英]tf.Variable and tf.constant

I was reading the CNN model for text classification, code link , and I was wondering, in line 70, the code: 我正在阅读CNN模型以进行文本分类和代码链接 ,而我在第70行想知道代码:

b = tf.Variable(tf.constant(0.1, shape=[num_classes]), name="b")

Why it can be defined as Variable and constant at same time? 为什么可以同时将其定义为变量和常量? is this equal to: 这等于:

b = tf.Variable(0.1, shape=[num_classes], name="b")

Yes, both are same. 是的,两者都一样。 Tensorflow implicitly copies tf.constant value into tf.Variable value. Tensorflow隐式将tf.constant值复制到tf.Variable值。 Operations a.op,b.op and c.op explain everything 操作a.op,b.op和c.op解释了所有内容

  import tensorflow as tf

    with tf.Session() as sess:
        a=tf.constant(0.1);
        b = tf.Variable(tf.constant(0.1), name="b");
        c = tf.Variable(0.1, name="b");
        sess.run(tf.global_variables_initializer());
        print(a.dtype);
        print(b.dtype);
        print(c.dtype);
        print("**********************")
        print(a.op);
        print(b.op);
        print(c.op);

Output: 输出:

<dtype: 'float32'>
<dtype: 'float32_ref'>
<dtype: 'float32_ref'>
**********************

name: "Const_40"
op: "Const"
attr {
  key: "dtype"
  value {
    type: DT_FLOAT
  }
}
attr {
  key: "value"
  value {
    tensor {
      dtype: DT_FLOAT
      tensor_shape {
      }
      float_val: 0.10000000149
    }
  }
}

name: "b_38"
op: "VariableV2"
attr {
  key: "container"
  value {
    s: ""
  }
}
attr {
  key: "dtype"
  value {
    type: DT_FLOAT
  }
}
attr {
  key: "shape"
  value {
    shape {
    }
  }
}
attr {
  key: "shared_name"
  value {
    s: ""
  }
}

name: "b_39"
op: "VariableV2"
attr {
  key: "container"
  value {
    s: ""
  }
}
attr {
  key: "dtype"
  value {
    type: DT_FLOAT
  }
}
attr {
  key: "shape"
  value {
    shape {
    }
  }
}
attr {
  key: "shared_name"
  value {
    s: ""
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM