我要做的是從現(xiàn)有的訓練模型中獲取一些權(quán)重和偏差,然后在我的自定義操作(模型或圖形)中使用它們。我可以使用以下方法還原模型:# Create contextwith 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的啟發(fā),我找到了一種簡單干凈的方法: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)
添加回答
舉報
0/150
提交
取消