使用 tensorflow 的估计器 API 的 RNN 每个时期的权重矩阵和成本
weight matrices and cost in each epoch of RNN using estimator API of tensorflow
我使用 Estimator API 来训练 RNN 模型,我想绘制 cost/epoch 图形并获得最佳模型权重矩阵。在 Estimator API 中可以吗?
这是代码:
classifier.train(input_fn=lambda: input_fn_train(train_x, label_train, batch_size),steps=train_steps)
eval_result = classifier.evaluate(input_fn=lambda: input_fn_eval(test_x, label_test, batch_size))
有可能。您需要做的是配置您的 Estimator 以生成有助于您决定要保留哪些权重的相关信息。这可以通过检查点来完成。这是您模型的 'saves'。
传递给 Estimator config=
一些配置会很有用。
这是一个带有自定义 Estimator 的示例:
def model_fn(features, labels, mode, params):
#Some code is here that gives you the output of your model from where
#you get your predictions.
if mode == tf.estimator.ModeKeys.TRAIN or tf.estimator.ModeKeys.EVAL:
#Some more code is here
loss = #your loss function here
tf.summary.scalar('loss', loss)
if mode == tf.estimator.ModeKeys.TRAIN:
#More code here that train your model
if mode == tf.estimator.ModeKeys.EVAL:
#Again more code that you use to get some evaluation metrics
if mode == tf.estimator.ModeKeys.PREDICT:
#Code...
return tf.estimator.EstimatorSpec(mode=mode,
predictions=predictions,
loss=loss,
train_op=train_op,
eval_metric_ops=eval_metric_ops)
configuration = tf.estimator.RunConfig(save_summary_steps=10,
keep_checkpoint_max=30,
save_checkpoints_steps=10,
log_step_count_steps=10)
custom_estimator = tf.estimator.Estimator(model_fn=model_fn,
model_dir='model_dir',
config=configuration)
custom_estimator.train(input_fn=input_fn_train, steps=10000)
save_summary_steps
:其实你可以这样想,你走了多少步之后
估算器会更新您的摘要。这可能很有用,因此您可以绘制损失
每 10 步。
save_checkpoints_steps
:您的估算器将在当前状态下保存多少步。
您可以在 model_dir
中找到这些检查点。
如果您使用的是固定 Estimator,我认为摘要是预定义的,但损失函数已经存在,因此您只需配置打印摘要的频率以及保存模型状态的频率。
我使用 Estimator API 来训练 RNN 模型,我想绘制 cost/epoch 图形并获得最佳模型权重矩阵。在 Estimator API 中可以吗? 这是代码:
classifier.train(input_fn=lambda: input_fn_train(train_x, label_train, batch_size),steps=train_steps)
eval_result = classifier.evaluate(input_fn=lambda: input_fn_eval(test_x, label_test, batch_size))
有可能。您需要做的是配置您的 Estimator 以生成有助于您决定要保留哪些权重的相关信息。这可以通过检查点来完成。这是您模型的 'saves'。
传递给 Estimator config=
一些配置会很有用。
这是一个带有自定义 Estimator 的示例:
def model_fn(features, labels, mode, params):
#Some code is here that gives you the output of your model from where
#you get your predictions.
if mode == tf.estimator.ModeKeys.TRAIN or tf.estimator.ModeKeys.EVAL:
#Some more code is here
loss = #your loss function here
tf.summary.scalar('loss', loss)
if mode == tf.estimator.ModeKeys.TRAIN:
#More code here that train your model
if mode == tf.estimator.ModeKeys.EVAL:
#Again more code that you use to get some evaluation metrics
if mode == tf.estimator.ModeKeys.PREDICT:
#Code...
return tf.estimator.EstimatorSpec(mode=mode,
predictions=predictions,
loss=loss,
train_op=train_op,
eval_metric_ops=eval_metric_ops)
configuration = tf.estimator.RunConfig(save_summary_steps=10,
keep_checkpoint_max=30,
save_checkpoints_steps=10,
log_step_count_steps=10)
custom_estimator = tf.estimator.Estimator(model_fn=model_fn,
model_dir='model_dir',
config=configuration)
custom_estimator.train(input_fn=input_fn_train, steps=10000)
save_summary_steps
:其实你可以这样想,你走了多少步之后
估算器会更新您的摘要。这可能很有用,因此您可以绘制损失
每 10 步。
save_checkpoints_steps
:您的估算器将在当前状态下保存多少步。
您可以在 model_dir
中找到这些检查点。
如果您使用的是固定 Estimator,我认为摘要是预定义的,但损失函数已经存在,因此您只需配置打印摘要的频率以及保存模型状态的频率。