Tensorflow CustomEstimator DNNRegressor label_dimensions
Tensorflow CustomEstimator DNNRegressor label_dimensions
我目前对 tensorflow 还很陌生,我正在尝试根据粒子在过去时间步长中的位置来预测粒子的位置。
我的特征是这样的:
X_0 X_1 X_2 X_3 X_4 Y_0 Y_1 Y_2 Y_3 Y_4
19 650.0 651.0 652.0 653.0 654.0 128.3 135.9 143.5 151.1 158.7
16 647.0 648.0 649.0 650.0 651.0 105.5 113.1 120.7 128.3 135.9
...
我的标签看起来像这样:
LabelX LabelY
19 655.0 166.3
16 652.0 143.5
...
如您所见,我的标签是二维的。我的第一次尝试使用 tensorflows 预制估计器 tf.estimator.DNNRegressor
并在创建过程中给它参数 label_dimension=2
效果很好。
现在我想对自定义估算器执行相同的操作。
遗憾的是,tensorflow 网站上的教程都使用分类器而不是回归器,我在网上找到的唯一示例是 this,但它们只使用一维输出。
我已经尝试了很多,但没有取得任何进展。
我相当确定我必须更改第 41 行
output_layer = tf.layers.dense(inputs=top, units=1)
但如果这样做,我将无法使文件的其余部分正常运行。
我成功了。
你可以找到我的代码here。
import tensorflow as tf
def myCustomEstimator(features, labels, mode, params):
"""Modell funktion für Custom Estimator (DNN Regression)"""
# Input Layer
top = tf.feature_column.input_layer(features, params["feature_columns"])
# basierend auf hidden Units wird die Netztopologie aufgebaut
for units in params.get("hidden_units", [20]):
top = tf.layers.dense(inputs=top, units=units, activation=tf.nn.relu)
if "dropout" in params.keys() and params["dropout"] != 0:
top = tf.layers.dropout(inputs=top, rate=params["dropout"], training=mode == tf.estimator.ModeKeys.TRAIN)
# lineares output layer mit 2 Neuronen für die 2 Koordinaten
output_layer = tf.layers.dense(inputs=top, units=2)
if mode == tf.estimator.ModeKeys.PREDICT:
# In `PREDICT` mode we only need to return predictions.
return tf.estimator.EstimatorSpec(
mode=mode, predictions={"predictions": output_layer})
average_loss = tf.losses.mean_squared_error(tf.cast(labels, tf.float32), output_layer)
tf.summary.scalar("average_loss", average_loss)
MSE = tf.metrics.mean_squared_error(tf.cast(labels, tf.float32), output_layer)
tf.summary.scalar('error', MSE[1])
# Pre-made estimators use the total_loss instead of the average,
# so report total_loss for compatibility.
batch_size = tf.shape(labels)[0]
total_loss = tf.to_float(batch_size) * average_loss
if mode == tf.estimator.ModeKeys.TRAIN:
optimizer = params.get("optimizer", tf.train.AdamOptimizer)
optimizer = optimizer(params.get("learning_rate", None))
train_op = optimizer.minimize(
loss=average_loss, global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(
mode=mode, loss=total_loss, train_op=train_op)
# In evaluation mode we will calculate evaluation metrics.
assert mode == tf.estimator.ModeKeys.EVAL
# Calculate root mean squared error
rmse = tf.metrics.root_mean_squared_error(tf.cast(labels, tf.float32), output_layer)
# Add the rmse to the collection of evaluation metrics.
eval_metrics = {"rmse": rmse, "average_loss": MSE}
return tf.estimator.EstimatorSpec(
mode=mode,
# Report sum of error for compatibility with pre-made estimators
loss=total_loss,
eval_metric_ops=eval_metrics)
我目前对 tensorflow 还很陌生,我正在尝试根据粒子在过去时间步长中的位置来预测粒子的位置。
我的特征是这样的:
X_0 X_1 X_2 X_3 X_4 Y_0 Y_1 Y_2 Y_3 Y_4
19 650.0 651.0 652.0 653.0 654.0 128.3 135.9 143.5 151.1 158.7
16 647.0 648.0 649.0 650.0 651.0 105.5 113.1 120.7 128.3 135.9
...
我的标签看起来像这样:
LabelX LabelY
19 655.0 166.3
16 652.0 143.5
...
如您所见,我的标签是二维的。我的第一次尝试使用 tensorflows 预制估计器 tf.estimator.DNNRegressor
并在创建过程中给它参数 label_dimension=2
效果很好。
现在我想对自定义估算器执行相同的操作。 遗憾的是,tensorflow 网站上的教程都使用分类器而不是回归器,我在网上找到的唯一示例是 this,但它们只使用一维输出。
我已经尝试了很多,但没有取得任何进展。 我相当确定我必须更改第 41 行
output_layer = tf.layers.dense(inputs=top, units=1)
但如果这样做,我将无法使文件的其余部分正常运行。
我成功了。
你可以找到我的代码here。
import tensorflow as tf
def myCustomEstimator(features, labels, mode, params):
"""Modell funktion für Custom Estimator (DNN Regression)"""
# Input Layer
top = tf.feature_column.input_layer(features, params["feature_columns"])
# basierend auf hidden Units wird die Netztopologie aufgebaut
for units in params.get("hidden_units", [20]):
top = tf.layers.dense(inputs=top, units=units, activation=tf.nn.relu)
if "dropout" in params.keys() and params["dropout"] != 0:
top = tf.layers.dropout(inputs=top, rate=params["dropout"], training=mode == tf.estimator.ModeKeys.TRAIN)
# lineares output layer mit 2 Neuronen für die 2 Koordinaten
output_layer = tf.layers.dense(inputs=top, units=2)
if mode == tf.estimator.ModeKeys.PREDICT:
# In `PREDICT` mode we only need to return predictions.
return tf.estimator.EstimatorSpec(
mode=mode, predictions={"predictions": output_layer})
average_loss = tf.losses.mean_squared_error(tf.cast(labels, tf.float32), output_layer)
tf.summary.scalar("average_loss", average_loss)
MSE = tf.metrics.mean_squared_error(tf.cast(labels, tf.float32), output_layer)
tf.summary.scalar('error', MSE[1])
# Pre-made estimators use the total_loss instead of the average,
# so report total_loss for compatibility.
batch_size = tf.shape(labels)[0]
total_loss = tf.to_float(batch_size) * average_loss
if mode == tf.estimator.ModeKeys.TRAIN:
optimizer = params.get("optimizer", tf.train.AdamOptimizer)
optimizer = optimizer(params.get("learning_rate", None))
train_op = optimizer.minimize(
loss=average_loss, global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(
mode=mode, loss=total_loss, train_op=train_op)
# In evaluation mode we will calculate evaluation metrics.
assert mode == tf.estimator.ModeKeys.EVAL
# Calculate root mean squared error
rmse = tf.metrics.root_mean_squared_error(tf.cast(labels, tf.float32), output_layer)
# Add the rmse to the collection of evaluation metrics.
eval_metrics = {"rmse": rmse, "average_loss": MSE}
return tf.estimator.EstimatorSpec(
mode=mode,
# Report sum of error for compatibility with pre-made estimators
loss=total_loss,
eval_metric_ops=eval_metrics)