TensorFlow训练-使用简单的一层估算器来匹配结果

时间:2018-09-26 21:04:07

标签: python tensorflow tensorflow-estimator

我是TensorFlow的新手,但在分析方面经验丰富。我正尝试在TensorFlow中重现使用传统神经网络训练器获得的性能,该训练器仅由单个隐藏层组成,并使用带有动量更新和均方误差目标函数的反向prop进行训练。

不幸的是,到目前为止,我的所有尝试都以失败告终,使用TensorFlow模型产生的范围从使用罐头tf.estimator.DNNClassifier时大约“仅”比我的自家训练师低10-15%,甚至很多更糟糕的是,我自己尝试构建网络。

有人可以在下面的代码中识别出我在调用该工具时所犯的错误吗?使用命令行输入,我可以在“ canned_dnn”和“自定义”设置之间切换

def get_classifier( model_name, cur_model_dir, feature_columns ):
    if model_name == 'canned_dnn':
        return tf.estimator.DNNClassifier( feature_columns=feature_columns,
                                           model_dir=cur_model_dir,
                                           hidden_units=[18],
                                           activation_fn=tf.nn.relu )
    elif model_name == 'custom':
        return tf.estimator.Estimator( model_fn=my_model,
                                       model_dir=cur_model_dir,
                                       params={
                                           'feature_columns': feature_columns,
                                           'n_classes': 2
                                       })
    else:
        raise RuntimeError()

def my_model(features, labels, mode, params):

    # input layer                                                                                                                                                                                                                                                           
    net = tf.feature_column.input_layer( features, params['feature_columns'] )

    # hidden layer 1                                                                                                                                                                                                                                                        
    net = tf.layers.dense(net, units=18, activation=tf.nn.relu, use_bias=True, bias_initializer=tf.zeros_initializer(), name='LEARN_1')

    # output layer computes logits                                                                                                                                                                                                                                          
    logits = tf.layers.dense(net, params['n_classes'], activation=tf.nn.sigmoid, name='OUTPUT')

    # compute predictions                                                                                                                                                                                                                                                   
    predicted_classes = tf.argmax(logits, 1)
    if mode == tf.estimator.ModeKeys.PREDICT:
        predictions = {
            'class_ids': predicted_classes[:, tf.newaxis],
            'probabilities': tf.nn.softmax(logits),
            'logits': logits,
        }
        return tf.estimator.EstimatorSpec(mode, predictions=predictions)

    # mean squared error                                                                                                                                                                                                                                                    
    predicted_classes = tf.argmax(logits, 1)
    loss = tf.losses.mean_squared_error(labels=tf.one_hot(labels, 2), predictions=logits)

    # metrics                                                                                                                                                                                                                                                               
    # loss report                                                                                                                                                                                                                                                           
    tf.summary.scalar('loss', loss )

    # accuracy                                                                                                                                                                                                                                                              
    accuracy = tf.metrics.accuracy(labels=labels,
                                   predictions=predicted_classes,
                                   name='acc_op')

    metrics = {'accuracy': accuracy}
    tf.summary.scalar('accuracy', accuracy[1])

    # EVALUATION MODE                                                                                                                                                                                                                                                       
    if mode == tf.estimator.ModeKeys.EVAL:
        return tf.estimator.EstimatorSpec(
            mode, loss=loss, eval_metric_ops=metrics)

    # TRAINING MODE                                                                                                                                                                                                                                                         
    assert mode == tf.estimator.ModeKeys.TRAIN
    with tf.name_scope( 'OPTIMIZE' ):
        optimizer=tf.train.MomentumOptimizer(learning_rate=.02,momentum=.5)
        train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
        return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)

0 个答案:

没有答案