Serialization 重用tensorflow训练图的一部分
因此,我训练了一个带有几层的tensorflow模型,大致如下:Serialization 重用tensorflow训练图的一部分,serialization,tensorflow,Serialization,Tensorflow,因此,我训练了一个带有几层的tensorflow模型,大致如下: with tf.variable_scope('model1') as scope: inputs = tf.placeholder(tf.int32, [None, num_time_steps]) embeddings = tf.get_variable('embeddings', (vocab_size, embedding_size)) lstm = tf.nn.rnn_cell.LSTMCell
with tf.variable_scope('model1') as scope:
inputs = tf.placeholder(tf.int32, [None, num_time_steps])
embeddings = tf.get_variable('embeddings', (vocab_size, embedding_size))
lstm = tf.nn.rnn_cell.LSTMCell(lstm_units)
embedded = tf.nn.embedding_lookup(embeddings, inputs)
_, state = tf.nn.dynamic_rnn(lstm, embedded, dtype=tf.float32, scope=scope)
# more stuff on the state
现在,我想在另一个模型中重用嵌入矩阵和lstm权重,除了这两个组件之外,它与这个模型非常不同
据我所知,如果我用tf.Saver
对象加载它们,它将查找
变量的名称完全相同,但我在两个图中使用了不同的variable\u scope
s
在中,建议创建一个图,其中LSTM被训练为另一个的超集,但鉴于两个模型的差异,我认为在我的情况下不可能。无论如何,如果一个图形做独立的事情,我认为让它依赖于另一个图形不是一个好主意
我考虑过在序列化图中更改LSTM权重和嵌入的变量范围。我的意思是,在它最初读到的model1/Weights:0
或其他东西的地方,它将是另一个范围/Weights:0
。这是否可能和可行
当然,如果有更好的解决方案,也很受欢迎。我发现可以使用字典将序列化文件中的变量名(没有尾随的
:0
)映射到要在图形中还原的变量对象来初始化该保护程序。例如:
varmap = {'model1/some_scope/weights': variable_in_model2,
'model1/another_scope/weights': another_variable_in_model2}
saver = tf.train.Saver(varmap)
saver.restore(sess, path_to_saved_file)