Tensorflow实战-计算图

import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

g1 = tf.Graph()
with g1.as_default():
    #在计算图g1中定义变量“v”,并设置初始值为0.
    v = tf.get_variable(
        "v",initializer=tf.zeros_initializer()(shape=[1]) #维度(shape)信息
    )

g2 = tf.Graph()
with g2.as_default():
    #在计算图g2中定义变量“v”,并设置初始值为1.
    v = tf.get_variable(
        "v",initializer=tf.ones_initializer()(shape=[1])
    )

#在计算图g1中读取变量“v”的取值。
with tf.Session(graph=g1) as sess:
    tf.global_variables_initializer().run()
    with tf.variable_scope("",reuse=True): #生成一个命名空间
        #在计算图g1中,变量“v”的取值应该为0,所以下面这行会输出[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,所以下面这行会输出[1.]。
        print (sess.run(tf.get_variable("v")))

猜你喜欢

转载自blog.csdn.net/s11092114/article/details/83870108
今日推荐