赞赏有人是否可以指出我在估算器函数中是否缺少任何内容。以下是我为估算器编写的代码:
谢谢
import tensorflow as tf
from util import OUTPUT_FEATURE
def metric_fn(probabilities, labels):
"""
"""
pred_model = tf.cast(tf.argmax(probabilities, 1), tf.int32)
actual_model = tf.cast(tf.argmax(labels, 1), tf.int32)
metrics = {}
metrics['accuracy'] = tf.metrics.accuracy(pred_model, actual_model)
# metrics['auroc'] = tf.metrics.auc(labels, probabilities)
return metrics
def RNN_estimator(hyper_parms, config):
"""
"""
num_classes = len(hyper_parms.label_dict)
hidden_units = hyper_parms.hidden_units
learning_rate = hyper_parms.learning_rate
lables = hyper_parms.label_dict
def _RNNModel(features):
"""
"""
input_layer = features[OUTPUT_FEATURE]
# Create hidden RNN layers given the input layer
hidden_layers = [tf.nn.rnn_cell.LSTMCell(size) for size in hidden_units]
multi_rnn_cell = tf.nn.rnn_cell.MultiRNNCell(hidden_layers)
outputs, state = tf.nn.dynamic_rnn(cell=multi_rnn_cell,
inputs=input_layer,
dtype=tf.float32)
logits = tf.layers.dense(outputs[:,-1], num_classes)
return logits
def _train_op_fn(loss):
"""
"""
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
return train_op
def _model_fn(features, labels, mode):
"""
"""
logits = _RNNModel(features)
probabilities = tf.nn.softmax(logits)
predict_output = tf.cast(tf.argmax(probabilities, 1), tf.int32)
predict_output = tf.gather(lables, predict_output)
if (mode == tf.estimator.ModeKeys.TRAIN
or mode == tf.estimator.ModeKeys.EVAL):
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(
labels=labels, logits=logits))
evalmetrics = metric_fn(probabilities, labels)
if mode == tf.estimator.ModeKeys.TRAIN:
train_op = _train_op_fn(loss)
else:
train_op = None
else:
train_op = None
loss=None
evalmetrics=None
if (mode == tf.estimator.ModeKeys.PREDICT
or mode == tf.estimator.ModeKeys.EVAL):
predictions = {'class': predict_output,
'score': tf.reduce_max(probabilities, axis=1)
}
export_outputs = {'prediction': tf.estimator.export.PredictOutput(predictions)
}
else:
predictions = None
export_outputs = None
return tf.estimator.EstimatorSpec(mode=mode,
predictions=predictions,
loss=loss,
train_op=train_op,
export_outputs=export_outputs,
eval_metric_ops=evalmetrics)
return tf.estimator.Estimator(model_fn=_model_fn,
config=config,
warm_start_from=None)