如何使用预训练的张量流模型在Keras中指定可训练变量?

时间:2018-09-29 08:12:23

标签: python tensorflow keras

如果我只有keras图层,我知道可以使用layer.trainable = False。但是,我也有一个预训练的张量流模型,并且我不想更改参数。我要实现的目标如下:

g = tf.Graph()
with g.as_default()

    ## this is the pretrain tensorflow model, and I don't want to change the parameters
    with tf.variable_scope('pretrain'):
        saver = tf.train.import_meta_graph(xxxx)
        saver.restore(sess, tf.train.latest_checkpoint(xxxx))
    tf_input = g.get_tensor_by_name('input')
    tf_output = g.get_tensor_by_name('output')

    ## this is my keras model that I want to optimize
    with tf.variable_scope('train'):
        keras_output = Dense(xxx)(tf_output)

## how to specify the trainable variable in Keras?
## I don't want to use sess.run(opt) because I want to save it as a keras model, 
## which is easier to use in inference stage.
opt = tf.train.AdamOptimizer().minimize(var_list='train')
model = Model(tf_input, keras_output)
model.compile(xxx, optimizer = opt)

当我有预训练模型时,如何指定可训练变量?非常感谢!

0 个答案:

没有答案