我要做的是从现有的训练模型中获取一些权重和偏差,然后在我的自定义操作(模型或图形)中使用它们。
我可以使用以下方法还原模型:
# Create context
with tf.Graph().as_default(), tf.Session() as sess:
# Create model
with tf.variable_scope('train'):
train_model = MyModel(some_args)
然后获取张量:
latest_ckpt = tf.train.latest_checkpoint(path)
if latest_ckpt:
saver.restore(sess, latest_ckpt)
weight = tf.get_default_graph().get_tensor_by_name("example:0")
我的问题是,如果要weight在另一个上下文(模型或图形)中使用它,如何安全地将其值复制到新图形中,例如:
with self.test_session(use_gpu=True, graph=ops.Graph()) as sess:
with vs.variable_scope("test", initializer=initializer):
# How can I make it possible?
w = tf.get_variable('name', initializer=weight)
欢迎任何帮助,非常感谢。
感谢@Sorin的启发,我找到了一种简单干净的方法:
z = graph.get_tensor_by_name('prefix/NN/W1:0')
with tf.Session(graph=graph) as sess:
z_value = sess.run(z)
with tf.Graph().as_default() as new_graph, tf.Session(graph=new_graph) as sess:
w = tf.get_variable('w', initializer=z_value)
白板的微信
随时随地看视频慕课网APP
相关分类