2016-04-15 104 views

回答

0

您也可以使用Session.run()方法。

In [1]: import tensorflow as tf 

In [2]: sess = tf.InteractiveSession() 

In [3]: x = tf.constant(4) 

In [4]: sess.run(x) 
Out[4]: 4 
1

常量值被硬连接到图中,因此您可以通过检查图定义来查看它。

IE

tf.reset_default_graph() 
tf.constant(42) 
print tf.get_default_graph().as_graph_def() 

这给你

node { 
    name: "Const" 
    op: "Const" 
    attr { 
    key: "dtype" 
    value { 
     type: DT_INT32 
    } 
    } 
    attr { 
    key: "value" 
    value { 
     tensor { 
     dtype: DT_INT32 
     tensor_shape { 
     } 
     int_val: 42 
     } 
    } 
    } 
} 
versions { 
    producer: 9 
} 

这意味着你能得到的恒定值出作为

tf.get_default_graph().as_graph_def().node[0].attr["value"].tensor.int_val[0]