[英]How to reuse the weights in Tensorflow?
我將多次調用以下的tensorflow代碼。 權重將被重用還是每次都會創建新圖?
def lstm(encoder_cell, encoder_inputs_embedded, encoder_inputs_length):
with tf.variable_scope('lstm') as scope_bilstm:
((encoder_fw_outputs,
encoder_bw_outputs),
(encoder_fw_state,
encoder_bw_state)) = (
tf.nn.bidirectional_dynamic_rnn(cell_fw=encoder_cell,
cell_bw=encoder_cell,
inputs=encoder_inputs_embedded,
sequence_length=encoder_inputs_length,
time_major=False,
dtype=tf.float32)
)
encoder_outputs = tf.concat((encoder_fw_outputs, encoder_bw_outputs), 2)
return encoder_outputs
因為我知道它可能不會被重用,所以我通過在tf.variable_scope()
添加一個額外的reuse=True
來嘗試以下代碼。
def lstm(encoder_cell, encoder_inputs_embedded, encoder_inputs_length):
with tf.variable_scope('lstm', reuse=True) as scope_bilstm:
((encoder_fw_outputs,
encoder_bw_outputs),
(encoder_fw_state,
encoder_bw_state)) = (
tf.nn.bidirectional_dynamic_rnn(cell_fw=encoder_cell,
cell_bw=encoder_cell,
inputs=encoder_inputs_embedded,
sequence_length=encoder_inputs_length,
time_major=False,
dtype=tf.float32)
)
encoder_outputs = tf.concat((encoder_fw_outputs, encoder_bw_outputs), 2)
return encoder_outputs
但是我遇到了以下錯誤,
ValueError:變量inner / bidirectional_rnn / fw / lstm_cell / weights不存在,或者不是使用tf.get_variable()創建的。 您是說要在VarScope中設置“ reuse = None”?
我該如何解決這個錯誤? 我真的很感激!
那scope_bilstm.reuse_variables()
呢? 我不知道在程序中該行插入的位置。
它可能是有用的,看看這個在load_with_skip
功能
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.