TensorFlow 不同图中定义和使用变量

代码参考《TensorFlow:实战Google深度学习框架》,本地手打,调试后复制出来,和原文会有差别。


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
g1=tf.Graph()
with g1.as_default():
v=tf.get_variable("v",shape=[1],initializer=tf.zeros_initializer())
g2=tf.Graph()
with g2.as_default():
v=tf.get_variable("v",shape=[1],initializer=tf.ones_initializer())
with tf.Session(graph=g1) as sess:
tf.initialize_all_variables().run()
with tf.variable_scope("",reuse=True):
print (sess.run(tf.get_variable("v")))
with tf.Session(graph=g2) as sess:
tf.initialize_all_variables().run()
with tf.variable_scope("",reuse=True):
print (sess.run(tf.get_variable("v")))

Be the first to comment

Leave a Reply

Your email address will not be published.