1、绝大部分时候都会忽略graph的使用,如下代码所示,学会怎样tf.get_default_graph()是重要的,此外注意变量定义时的初始化必须加 initializer
2、此外,要知道
writer2 = tf.summary.FileWriter(Summary_log, g2)
writer2.close()
这两条语句加在哪里也是极为重要的
3、注意命名及命名空间的使用
import tensorflow as tf
Summary_log = './path'
g1 = tf.Graph()
with g1.as_default():
with tf.name_scope("MAT_MUL"):
a = tf.constant(1.0, tf.float32, [1, 2], name="tensor_a")
b = tf.constant(2.0, tf.float32, [2, 1], name="tensor_b")
result = tf.matmul(a, b, name="mat_mul")
g2 = tf.Graph()
with g2.as_default():
a = tf.get_variable("v_1", [2, 2], tf.float32, initializer=tf.ones_initializer)
b = tf.get_variable("v_2", [2, 2], tf.float32, initializer=tf.ones_initializer)
with tf.variable_scope("ADD"):
result2 = tf.add_n([a, b], name="add")
# writer1 = tf.summary.FileWriter(Summary_log, g1)
# writer1.close()
writer2 = tf.summary.FileWriter(Summary_log, g2)
writer2.close()
# with tf.Session(graph=g1) as sess:
# print(sess.run(result))
with tf.Session(graph=g2) as sess:
tf.global_variables_initializer().run()
print(sess.run(result2))
原文地址:https://www.cnblogs.com/liuboblog/p/11615571.html
时间: 2024-11-09 09:56:10