Tensorflow 模型恢复(恢复训练似乎从头开始)
Tensorflow model restoration (resume training seems starting from scratch)
我在保存模型后恢复训练时遇到问题。
问题是我的损失从 6 减少到 3。这时我保存模型。
当我恢复它并继续训练时,损失从 6 重新开始。
看来恢复真的不行。
我不明白,因为打印重量时,它们似乎已正确加载。
我使用 ADAM 优化器。提前致谢。
这里:
batch_size = self.batch_size
num_classes = self.num_classes
n_hidden = 50 #700
n_layers = 1 #3
truncated_backprop = self.seq_len
dropout = 0.3
learning_rate = 0.001
epochs = 200
with tf.name_scope('input'):
x = tf.placeholder(tf.float32, [batch_size, truncated_backprop], name='x')
y = tf.placeholder(tf.int32, [batch_size, truncated_backprop], name='y')
with tf.name_scope('weights'):
W = tf.Variable(np.random.rand(n_hidden, num_classes), dtype=tf.float32)
b = tf.Variable(np.random.rand(1, num_classes), dtype=tf.float32)
inputs_series = tf.split(x, truncated_backprop, 1)
labels_series = tf.unstack(y, axis=1)
with tf.name_scope('LSTM'):
cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, state_is_tuple=True)
cell = tf.contrib.rnn.DropoutWrapper(cell, output_keep_prob=dropout)
cell = tf.contrib.rnn.MultiRNNCell([cell] * n_layers)
states_series, current_state = tf.contrib.rnn.static_rnn(cell, inputs_series, \
dtype=tf.float32)
logits_series = [tf.matmul(state, W) + b for state in states_series]
prediction_series = [tf.nn.softmax(logits) for logits in logits_series]
losses = [tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels) \
for logits, labels, in zip(logits_series, labels_series)]
total_loss = tf.reduce_mean(losses)
train_step = tf.train.AdamOptimizer(learning_rate).minimize(total_loss)
tf.summary.scalar('total_loss', total_loss)
summary_op = tf.summary.merge_all()
loss_list = []
writer = tf.summary.FileWriter('tf_logs', graph=tf.get_default_graph())
all_saver = tf.train.Saver()
with tf.Session() as sess:
#sess.run(tf.global_variables_initializer())
tf.reset_default_graph()
saver = tf.train.import_meta_graph('./models/tf_models/rnn_model.meta')
saver.restore(sess, './models/tf_models/rnn_model')
for epoch_idx in range(epochs):
xx, yy = next(self.get_batch)
batch_count = len(self.D.chars) // batch_size // truncated_backprop
for batch_idx in range(batch_count):
batchX, batchY = next(self.get_batch)
summ, _total_loss, _train_step, _current_state, _prediction_series = sess.run(\
[summary_op, total_loss, train_step, current_state, prediction_series],
feed_dict = {
x : batchX,
y : batchY
})
loss_list.append(_total_loss)
writer.add_summary(summ, epoch_idx * batch_count + batch_idx)
if batch_idx % 5 == 0:
print('Step', batch_idx, 'Batch_loss', _total_loss)
if batch_idx % 50 == 0:
all_saver.save(sess, 'models/tf_models/rnn_model')
if epoch_idx % 5 == 0:
print('Epoch', epoch_idx, 'Last_loss', loss_list[-1])
我有同样的问题,在我的例子中,模型被正确恢复,但损失一次又一次地开始非常高,问题是我的批量恢复不是随机的。我有三个 classes,A、B 和 C。我的数据以这种方式输入,然后是 A,然后是 B,然后是 C。我不知道这是否是你的问题,但你必须确保每一批你给你的模型有你所有的 classes 在里面,所以在你的情况下,批次必须有 batch_size/num_classes 输入每个 class。我更改了它,一切都运行良好:)
检查您是否正确喂食模型。
我的问题是标签中的代码错误,它们在两个 运行 之间变化。
所以现在可以了。
感谢您的帮助
我在保存模型后恢复训练时遇到问题。 问题是我的损失从 6 减少到 3。这时我保存模型。 当我恢复它并继续训练时,损失从 6 重新开始。 看来恢复真的不行。 我不明白,因为打印重量时,它们似乎已正确加载。 我使用 ADAM 优化器。提前致谢。 这里:
batch_size = self.batch_size
num_classes = self.num_classes
n_hidden = 50 #700
n_layers = 1 #3
truncated_backprop = self.seq_len
dropout = 0.3
learning_rate = 0.001
epochs = 200
with tf.name_scope('input'):
x = tf.placeholder(tf.float32, [batch_size, truncated_backprop], name='x')
y = tf.placeholder(tf.int32, [batch_size, truncated_backprop], name='y')
with tf.name_scope('weights'):
W = tf.Variable(np.random.rand(n_hidden, num_classes), dtype=tf.float32)
b = tf.Variable(np.random.rand(1, num_classes), dtype=tf.float32)
inputs_series = tf.split(x, truncated_backprop, 1)
labels_series = tf.unstack(y, axis=1)
with tf.name_scope('LSTM'):
cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, state_is_tuple=True)
cell = tf.contrib.rnn.DropoutWrapper(cell, output_keep_prob=dropout)
cell = tf.contrib.rnn.MultiRNNCell([cell] * n_layers)
states_series, current_state = tf.contrib.rnn.static_rnn(cell, inputs_series, \
dtype=tf.float32)
logits_series = [tf.matmul(state, W) + b for state in states_series]
prediction_series = [tf.nn.softmax(logits) for logits in logits_series]
losses = [tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels) \
for logits, labels, in zip(logits_series, labels_series)]
total_loss = tf.reduce_mean(losses)
train_step = tf.train.AdamOptimizer(learning_rate).minimize(total_loss)
tf.summary.scalar('total_loss', total_loss)
summary_op = tf.summary.merge_all()
loss_list = []
writer = tf.summary.FileWriter('tf_logs', graph=tf.get_default_graph())
all_saver = tf.train.Saver()
with tf.Session() as sess:
#sess.run(tf.global_variables_initializer())
tf.reset_default_graph()
saver = tf.train.import_meta_graph('./models/tf_models/rnn_model.meta')
saver.restore(sess, './models/tf_models/rnn_model')
for epoch_idx in range(epochs):
xx, yy = next(self.get_batch)
batch_count = len(self.D.chars) // batch_size // truncated_backprop
for batch_idx in range(batch_count):
batchX, batchY = next(self.get_batch)
summ, _total_loss, _train_step, _current_state, _prediction_series = sess.run(\
[summary_op, total_loss, train_step, current_state, prediction_series],
feed_dict = {
x : batchX,
y : batchY
})
loss_list.append(_total_loss)
writer.add_summary(summ, epoch_idx * batch_count + batch_idx)
if batch_idx % 5 == 0:
print('Step', batch_idx, 'Batch_loss', _total_loss)
if batch_idx % 50 == 0:
all_saver.save(sess, 'models/tf_models/rnn_model')
if epoch_idx % 5 == 0:
print('Epoch', epoch_idx, 'Last_loss', loss_list[-1])
我有同样的问题,在我的例子中,模型被正确恢复,但损失一次又一次地开始非常高,问题是我的批量恢复不是随机的。我有三个 classes,A、B 和 C。我的数据以这种方式输入,然后是 A,然后是 B,然后是 C。我不知道这是否是你的问题,但你必须确保每一批你给你的模型有你所有的 classes 在里面,所以在你的情况下,批次必须有 batch_size/num_classes 输入每个 class。我更改了它,一切都运行良好:)
检查您是否正确喂食模型。
我的问题是标签中的代码错误,它们在两个 运行 之间变化。 所以现在可以了。 感谢您的帮助