将相同的权重加载到新图中的几个变量

时间:2019-04-08 12:40:21

标签: python tensorflow

我想将预训练模型中的相同变量加载到新模型中的多个变量

mydate = "11/11/2019"
new Date("11/11/2019").toISOString()

和后记

v1 = tf.get_variable("v1", shape=[3], initializer = tf.zeros_initializer)
inc_v1 = v1.assign(v1+1)
init_op = tf.global_variables_initializer()
saver = tf.train.Saver(v1)

with tf.Session() as sess:
  sess.run(init_op)
  sess.run(v1+1)
  save_path = saver.save(sess, "/tmp/model.ckpt")

也就是说,我希望两个变量都可以从先前模型的v1变量中初始化。
下面的示例崩溃了,因为它说图形是不同的。

2 个答案:

答案 0 :(得分:1)

Evaluate the assigned value of the variable from the original graph and then initialize new variables from new graph with this value:

import tensorflow as tf

with tf.Graph().as_default():
    # the variable from the original graph
    v0 = tf.Variable(tf.random_normal([2, 2]))

with tf.Session(graph=v0.graph) as sess:
    sess.run(v0.initializer)
    init_val = v0.eval() # <-- evaluate the assigned value
    print('original graph:')
    print(init_val)
# original graph:
# [[-1.7466899   1.1560178 ]
#  [-0.46535382  1.7059366 ]]

# variables from new graph
with tf.Graph().as_default():
    v1 = tf.Variable(init_val) # <-- variable from new graph
    v2 = tf.Variable(init_val) # <-- variable from new graph

with tf.Session(graph=v1.graph) as sess:
    sess.run([v.initializer for v in [v1, v2]])
    print('new graph:')
    print(v1.eval())
    print(v2.eval())
# new graph:
# [[-1.7466899   1.1560178 ]
#  [-0.46535382  1.7059366 ]]
# [[-1.7466899   1.1560178 ]
#  [-0.46535382  1.7059366 ]]

答案 1 :(得分:0)

Here's another method, iterating the variables from the previous graph:

def load_pretrained(sess):
    checkpoint_path = 'pretrainedmodel.ckpt'

    vars_to_load = [var for var in tf.get_collection(tf.GraphKeys.VARIABLES) if
                    ("some_scope" in var.op.name)]

    assign_ops = []
    reader = tf.contrib.framework.load_checkpoint(checkpoint_path)

    for var in vars_to_load:
        for name,shape in tf.contrib.framework.list_variables(checkpoint_path):
            if(var.op.name ~some regex comperison~ name):
                assign_ops.append(tf.assign(var,reader.get_tensor(name)))
                break


    sess.run(assign_ops)