import tensorflow as tf a = tf.constant([1.0,2.0],name="a") b = tf.constant([2.0,3.0],name="b") result = a + b sess = tf.Session() W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations. >>> sess.run(result) array([ 3., 5.], dtype=float32)
>>> type(result) <class 'tensorflow.python.framework.ops.Tensor'>
import tensorflow as tf a = tf.constant([1.0, 2.0], name="a") b = tf.constant([2.0, 3.0], name="b") result = a + b
print(a.graph is tf.get_default_graph()) # 输出为True # 通过a.graph可以查看张量所属的计算图,如果没有指定,则为当前默认的计算图。所以输出为True。 # tf.get_default_graph()表示获取当前默认的计算图
import tensorflow as tf
g1 = tf.Graph() with g1.as_default(): # 设置v=0 v = tf.get_variable("v", initializer=tf.zeros(shape=[1])) # 这个在1.0版本之前写成tf.zeros_initializer(shape=[1])下同 g2 = tf.Graph() with g2.as_default(): # 设置v=1 v = tf.get_variable("v", initializer=tf.ones(shape=[1]))
# 在计算图g1中读取变量"v"的取值 with tf.Session(graph=g1) as sess: tf.global_variables_initializer().run() # 这里在1.0版本之前是tf.initalizer_all_variables().run()下同 with tf.variable_scope("", reuse=True): # 在计算图g1中,变量"v"的取值应该为0 print(sess.run(tf.get_variable("v")))
# 在计算图g2中读取变量"v"的取值 with tf.Session(graph=g2) as sess: tf.global_variables_initializer().run() with tf.variable_scope("", reuse=True): # 在计算图g2中,变量"v"的取值应该为1 print(sess.run(tf.get_variable("v")))
# 运行结果 W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations. [ 0.] [ 1.]
sess = tf.Session() with sess.as_default(): print(result.eval())
# 输出 W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations. W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations. [ 3. 5.]
# 这2个命令都可以输出一样的结果 print(sess.run(result)) print(result.eval(session=sess))