【问题标题】:Keras sequential model to Tensorflow EstimatorSpec accuracy decreasesKeras 顺序模型到 Tensorflow EstimatorSpec 的准确性降低
【发布时间】:2018-10-23 11:33:34
【问题描述】:

从 Keras (keras_model_fn) 转换为 TF model_fn 以在 Sagemaker 中使用时遇到一些问题。

模型如下所示:

Keras

def keras_model_fn(hyperparameters):
    model = tf.keras.Sequential()
    # increase input_dim (cur 2500) as amount of words go up
    model.add(tf.keras.layers.InputLayer(input_shape=[8], name='main_input'))
    model.add(tf.keras.layers.Embedding(2500, 128, input_length=8))
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(NUM_CLASSES, activation='softmax'))
    model.compile(
        optimizer='adam',
        loss='categorical_crossentropy',
        metrics=['acc']
    )
    return model

张量流

def model_fn(features, labels, mode, params):
    input_layer = tf.keras.layers.InputLayer(
        input_shape=(8,))(features[INPUT_TENSOR_NAME])
    embedding_layer = tf.keras.layers.Embedding(
        2500, 
        128, 
        input_length=8)(input_layer)
    flattened = tf.keras.layers.Flatten()(embedding_layer)
    predictions = tf.keras.layers.Dense(
        NUM_CLASSES, 
        activation='softmax')(flattened)

    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(
            mode=mode,
            predictions={"output": predictions})

    loss = tf.losses.softmax_cross_entropy(labels, predictions)
    train_op = tf.contrib.layers.optimize_loss(
        loss=loss,
        global_step=tf.train.get_global_step(),
        learning_rate=0.001,
        optimizer="Adam")

    predictions_dict = {"output": predictions}
    eval_metric_ops = {
        "accuracy": tf.metrics.accuracy(
            tf.cast(labels,tf.int32), predictions)
    }
    return tf.estimator.EstimatorSpec(
        mode=mode,
        loss=loss,
        train_op=train_op,
        eval_metric_ops=eval_metric_ops
)

训练和评估数据是相同的。输入一组填充文本序列(长度为 8)。预期输出为 1/5 个标签。

损失

我假设问题在于损失函数。我不太清楚 Sequential 模型在幕后做什么,而我的 tensorflow 模型在做什么。

在 Keras 模型中,我得到以下损失。

INFO:tensorflow:global_step/sec: 170.783
INFO:tensorflow:loss = 0.0018957269, step = 1701 (0.586 sec)
INFO:tensorflow:global_step/sec: 164.419
INFO:tensorflow:loss = 0.029586311, step = 1801 (0.608 sec)
INFO:tensorflow:global_step/sec: 155.381
INFO:tensorflow:loss = 0.0019212833, step = 1901 (0.644 sec)
INFO:tensorflow:Loss for final step: 0.0023477676. 

在转换后的模型中,我得到以下内容。

INFO:tensorflow:loss = 1.232958, step = 1701 (0.354 sec)
INFO:tensorflow:global_step/sec: 280.328
INFO:tensorflow:loss = 1.0923336, step = 1801 (0.357 sec)
INFO:tensorflow:global_step/sec: 291.823
INFO:tensorflow:loss = 1.4360821, step = 1901 (0.343 sec)
INFO:tensorflow:Loss for final step: 1.0532712.

正如预期的那样,Converted 模型的准确率(针对其训练的数据)达到了 60% 左右。 Keras 模型的准确度为 100%。

我的问题是转换中的一切看起来都正确吗?我可以对转换后的模型做些什么来获得相似的性能?

我已经开始在 Keras 源代码中进行挖掘,以查看模型编译函数对目标/输出的作用,但我也打算在这里联系,看看是否有人有建议/之前遇到过这个问题.

【问题讨论】:

    标签: tensorflow machine-learning keras deep-learning


    【解决方案1】:

    问题可能是您在 TensorFlow 版本中应用了两个 softmax 激活。请注意,tf.losses.softmax_cross_entropy 需要未缩放的 logits。您可以执行以下操作:

    logits = tf.keras.layers.Dense(
        NUM_CLASSES)(flattened)
    predictions = tf.keras.layers.Activation(
        'softmax')(logits)
    
    loss = tf.losses.softmax_cross_entropy(labels, logits)
    

    【讨论】:

      猜你喜欢
      • 2021-10-14
      • 2017-10-09
      • 2020-02-29
      • 2020-11-19
      • 1970-01-01
      • 2021-08-03
      • 2021-05-04
      • 1970-01-01
      • 2020-10-17
      相关资源
      最近更新 更多