【发布时间】:2018-02-15 09:46:47
【问题描述】:
我正在研究纹理分类,并且基于以前的工作,我正在尝试将 AlexNET 的最后一层修改为具有 20 个类,并且仅针对我的多类分类问题训练该层。 我在 NVIDIA GTX 1080 上使用 Tensorflow-GPU,在 Ubuntu 16.04 上使用 Python3.6。 我正在使用梯度下降优化器和类 Estimator 来构建它。我还使用两个 dropout 层进行正则化。因此,我的超参数是学习率、batch_size 和 weight_decay。我尝试使用 50,100,200 的 batch_size,0.005 和 0.0005 的 weight_decays,以及 1e-3,1e-4 和 1e-5 的学习率。上述值的所有训练损失曲线都遵循相似的趋势。
我的训练损失曲线不是单调递减,而是似乎在振荡。我为学习率=1e-5、权重衰减=0.0005 和batch_size=200 提供了张量板可视化。
请协助了解出了什么问题以及我可以如何纠正它。 The Tensorboard Visualization for the case I specified
# Create the Estimator
classifier = tf.estimator.Estimator(model_fn=cnn_model)
# Set up logging for predictions
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=10)
# Train the model
train_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": train_data},y=train_labels,batch_size=batch_size,num_epochs=None,shuffle=True)
classifier.train(input_fn=train_input_fn, steps=200000, hooks=[logging_hook])
# Evaluate the model and print results
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": eval_data},
y=eval_labels,
num_epochs=1,
shuffle=False)
eval_results = classifier.evaluate(input_fn=eval_input_fn)
print(eval_results)
#Sections of the cnn_model
#Output Config
predictions = { "classes": tf.argmax(input=logits, axis=1),# Generate predictions (for PREDICT and EVAL mode)
"probabilities": tf.nn.softmax(logits, name="softmax_tensor")} # Add `softmax_tensor` to the graph. It is used for PREDICT and by the `logging_hook`.
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)
# Calculate Loss (for both TRAIN and EVAL modes)
onehot_labels = tf.one_hot(indices=tf.cast(labels,tf.int32),depth=20)
loss = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=logits)
#Training Config
if mode == tf.estimator.ModeKeys.TRAIN:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
tf.summary.scalar('training_loss',loss)
summary_hook = tf.train.SummarySaverHook(save_steps=10,output_dir='outputs',summary_op=tf.summary.merge_all())
train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op,training_hooks=[summary_hook])
# Evaluation Metric- Accuracy
eval_metric_ops = {"accuracy": tf.metrics.accuracy(labels=labels, predictions=predictions["classes"])}
print(time.time()-t)
tf.summary.scalar('eval_loss',loss)
ac=tf.metrics.accuracy(labels=labels,predictions=predictions["classes"])
tf.summary.scalar('eval_accuracy',ac)
evaluation_hook= tf.train.SummarySaverHook(save_steps=10,output_dir='outputseval',summary_op=tf.summary.merge_all())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops,evaluation_hooks=[evaluation_hook])
【问题讨论】:
-
这个问题应该去:datascience.stackexchange.com
-
@RSSharma:您找到解决问题的方法了吗?我面临着类似的情况,我的训练损失也看起来像一个周期函数。谢谢
标签: python tensorflow machine-learning