4
我想弄清楚如何導入學習保存模型作爲更大模型的一部分。Tensorflow:跨圖傳輸變量
具體而言,我有一個RNN和一些嵌入矩陣(模型A),這些嵌入矩陣已經在存儲在.ckpt
文件中的大型數據集上進行了訓練。我還有另一個模型(模型B),它使用這個完全相同的RNN和嵌入模型作爲子模型,並使用其他操作來處理此RNN的輸出並將其用於分類。
這兩個模型的圖形定義是不同的,但我想將模型B初始化爲模型A的已保存版本。如何完成這項工作?
我到目前爲止的嘗試是嘗試加載模型A作爲不同會話和圖形下的另一個模型,然後將模型B中的相關矩陣分配給來自A的那些矩陣,但這不起作用。
這裏的代碼的相關部分:
sup = supervised() # spins up a class with an interactive session inside and sets up the graph
g = tf.Graph()
with g.as_default():
unsup = unsupervised('unsup.ckpt') # loads in model A from file
# w/ another session (not interactive)
# get matrix from unsup and assign to sup
sup._word_embeddings.assign(unsup.session.run(unsup._word_embeddings))
# do the same for the RNN
sup._gate_matrix.assign(unsup.session.run(unsup._gate_matrix))
sup._gate_bias.assign(unsup.session.run(unsup._gate_bias))
sup._cand_matrix.assign(unsup.session.run(unsup._cand_matrix))
sup._cand_bias.assign(unsup.session.run(unsup._cand_bias))
注意,這兩種模式有實例化類在使用tf.get_variable()
從rnn_cell.linear
爲RNN分配矩陣明確的變量。
在此先感謝!
嗨,你能分享你的解決方案嗎? 謝謝 – Sentient07
對不起,我沒有手上的代碼了。基本上所有這些賦值操作都是'g'上定義的符號操作符,因此要運行它們,您需要在'session.run'中運行'g'。希望這可以幫助。 – Taaam
我想要做的是恢復兩個不同模型(我認爲是兩個不同的圖)的權重,然後我想要第一個輸出作爲第二個圖的輸入 – Sentient07