Tensorflow自定义估算器-训练准确性和损失保持恒定

时间:2018-10-15 04:32:55

标签: tensorflow google-cloud-ml

我开发了一个简单的tensorflow自定义估计器,用于使用cloud-ml训练动态RNN。在测试模型时,我可以看到训练/评估准确性和损失保持不变。我尝试学习速度,增加数据大小,使用了几种不同的优化器以及尝试用于定义模型函数的替代选项。但是没有任何效果。尽管我不确定,但由于训练期间的准确性和损失完全没有变化,因此在我看来该模型完全没有训练。在我看来,虽然我找不到原因,但训练作业并未执行。

赞赏有人是否可以指出我在估算器函数中是否缺少任何内容。以下是我为估算器编写的代码:

谢谢

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)

0 个答案:

没有答案