例如,我有以下代码:
def model_fn(features, labels, mode, params):
"""Model function for Estimator."""
first_hidden_layer = tf.layers.dense(features["x"], 10, activation=tf.nn.relu)
second_hidden_layer = tf.layers.dense(first_hidden_layer, 10, activation=tf.nn.relu)
output_layer = tf.layers.dense(second_hidden_layer, 1)
predictions = tf.reshape(output_layer, [-1])
loss = tf.losses.mean_squared_error(labels, predictions)
optimizer = tf.train.GradientDescentOptimizer(
learning_rate=params["learning_rate"])
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,export_outputs=export_outputs)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": np.array(training_set.data)},
y=np.array(training_set.target),
num_epochs=None,
shuffle=True)
nn.train(input_fn=train_input_fn, steps=100)
如何获得" second_hidden_layer"的输出值,而不是张量而是实际值?我试图使用此代码但失败了。
export_outputs = {"en_out": tf.estimator.export.RegressionOutput( second_hidden_layer)}
答案 0 :(得分:0)
tf.estimator.EstimatorSpec有另一个名为"预测"的参数。使用此dict可以直接调用Estimator返回预测。
predictions = {
"en_out" : second_hidden_layer
}
并将其添加到EstimatorSpec中,如
return tf.estimator.EstimatorSpec(
mode=mode,
loss=loss,
train_op=train_op,predictions=predictions)
参数" export_outputs"用于例如for tensorflow serving。