在张量流中,训练modelA(including LSTMcells)
之后,如何使用modelA的权重和偏差来初始化modelB(including LSTMcells)
的权重和偏差。
这是model structures."()"
是num_units
。
modelA:
LSTM(20)-LSTM(3)※1
modelB:
LSTM(2)-LSTM(20)-LSTM(3)
([LSTM(2)]←added in modelB)
([LSTM(20)-LSTM(3)]←same in modelA※2)
我想使用※1的参数初始化※2的参数,然后重新训练模型B。
我的测试代码在这里。
def build_model_A():
X = tf.placeholder(tf.float32, [None,None,1], name='X')
lstm_2 = tf.nn.rnn_cell.BasicLSTMCell(20, name='blstmc2')#
lstm_3 = tf.nn.rnn_cell.BasicLSTMCell(3, name='blstmc3')#
lstm_out_2,lstm_out_2_state = tf.nn.dynamic_rnn(lstm_2,X,dtype=tf.float32,time_major=False)#
lstm_out_3,lstm_out_3_state = tf.nn.dynamic_rnn(lstm_3,lstm_out_2,dtype=tf.float32,time_major=False)
##loss##
p_loss = tf.square(tf.subtract(lstm_out_3, 0.5))
# Optimization
pred_loss = tf.reduce_sum(p_loss, name='pred_loss')
pred_train_op = tf.train.AdamOptimizer().minimize(pred_loss, name='pred_train_op')
return
def build_model_B():
X = tf.placeholder(tf.float32, [None,None,1], name='X')
lstm_1 = tf.nn.rnn_cell.BasicLSTMCell(2, name='blstmc1')#
lstm_2 = tf.nn.rnn_cell.BasicLSTMCell(20, name='blstmc2')#
lstm_3 = tf.nn.rnn_cell.BasicLSTMCell(3, name='blstmc3')#
lstm_out_1,lstm_out_1_state = tf.nn.dynamic_rnn(lstm_1,X,dtype=tf.float32,time_major=False)#
lstm_out_2,lstm_out_2_state = tf.nn.dynamic_rnn(lstm_2,lstm_out_1,dtype=tf.float32,time_major=False)
lstm_out_3,lstm_out_3_state = tf.nn.dynamic_rnn(lstm_3,lstm_out_2,dtype=tf.float32,time_major=False)
##loss##
p_loss = tf.square(tf.subtract(lstm_out_3, 0.5))
# Optimization
pred_loss = tf.reduce_sum(p_loss, name='pred_loss')
pred_train_op = tf.train.AdamOptimizer().minimize(pred_loss, name='pred_train_op')
return
def train_and_evaluate(sess,num_batches=None,input_data=None):
pred_loss = sess.graph.get_tensor_by_name('pred_loss:0')
sess.run(tf.global_variables_initializer())##
saver = tf.train.Saver()###for model save
for i in range(num_batches):
train_optimizer = "pred_train_op"
_, pa = sess.run([train_optimizer, pred_loss],
feed_dict={'X:0': input_data})
if i%1000==0:
[pa_A] = sess.run([pred_loss], feed_dict={'X:0': input_data})
print("**Train_acc("+str(i)+")**")
print("accuracy:"+str(pa_A))
if i==10000:
saver.save(sess, "model/test_sess_modelA/trained")
def train_fnc():
tf.reset_default_graph()
build_model_A()
sess = tf.InteractiveSession()
##make noise inputdata X_a##
x_sa = np.array([[1],[3],[5],[7],[9]])
X_a = []
for i in range(10):
X_a.append(x_sa)
X_a = np.array(X_a)
train_batch_num = 10000
train_and_evaluate(sess,num_batches=train_batch_num,input_data=X_a)
return
def test_func():
tf.reset_default_graph()
build_model_B()
saver = tf.train.Saver()
sess = tf.Session()
saver.restore(sess, "model/test_sess_modelA/trained")
def main_fnc():
train_fnc()
test_fnc()
我尝试使用“ tf.train.Saver()”,但是在恢复模型B中的参数时,出现错误“ NotFoundError(请参阅上面的回溯):关键rnn / blstmc1 / bias在检查点中找不到”。>
而且,我还尝试使用“ saver = tf.train.Saver({'blstmc2':lstm_2,'blstmc3':lstm_3})“,但是我遇到了错误(无法将BasicLSTMCell转换为张量)。 / p>