Skip to main content
 首页 » 编程设计

python之为什么 tensorflow 常量是 feedable true

2024年10月01日28dyllove98

我正在学习tensorflow

import tensorflow as tf 
print(tf.VERSION) 
 
a = tf.placeholder(tf.float32, shape=[3]) 
b = tf.constant([2, 2, 2], tf.float32) 
c = a + b 
with tf.Session() as sess: 
    print(tf.get_default_graph().is_feedable(b)) 
    print(sess.run(c, feed_dict={a: [3, 2, 3]})) 

输出结果如下

1.0.1 
True 
[ 5.  4.  5.] 

我不明白为什么 tensorflow 说常量是可馈送的。占位符是 feedable true 但为什么是常量?

请您参考如下方法:

因为在TF中你也可以feed values in constants and variables :

While you can replace any Tensor with feed data, including variables and constants, the best practice is to use a tf.placeholder node

自己检查一下:

import tensorflow as tf 
 
a = tf.constant([2, 2, 2], tf.float32) 
with tf.Session() as sess: 
    print(sess.run(a, feed_dict={a: [3, 2, 3]})) 

常量 a 改变了它的值。