Tensorflow 的 DNNLinearCombinedClassifier 打印回归损失而不是分类损失



我正在Kaggle的信用卡欺诈(分类)数据集上尝试Tensorflow的DNNLinearCombinedClassifier(版本1.3):

m = tf.estimator.DNNLinearCombinedClassifier(model_dir='/.../model', dnn_feature_columns=deep_columns,
                                            dnn_hidden_units=[20,5])
def input_fn(df, num_epochs):
    return tf.estimator.inputs.pandas_input_fn(
        x = df,
        y = df.Class,
        batch_size = 1000,
        num_epochs = num_epochs,
        shuffle=False)

使用模型的输出(此处为 DF。类)作为二进制特征。Tensorflow的训练日志

m.train(input_fn(data, 3))

是:

信息:张量流:损失 = 532.633,步长 = 2566信息:张量流:global_step/秒:37.9815 信息:张量流:损失 =560.574,步长 = 2666(2.635 秒) 信息:张量流:global_step/秒:38.3186

这里使用的损失函数是什么?

这里使用的损失函数是什么?

在二元分类的情况下,它是_BinaryLogisticHeadWithSigmoidCrossEntropyLoss - 一个围绕tf.nn.sigmoid_cross_entropy_with_logits损失函数的内部包装器。您对大值感到困惑,但如果您仔细观察此函数的计算内容,您会发现大值是很有可能的。

如果x是对数,z是标签(单个示例),则损失等于 x - x * z + log(1 + exp(-x)) ,相当于 xz == 0。总训练损失定义为小批量损失的总和。您的batch_size = 1000和数据具有 ~30 个特征,因此550左右的训练损失几乎是有意义的。

看看这个微型例子:

feature_columns = [tf.feature_column.numeric_column("x", shape=[1])]
estimator = tf.estimator.DNNLinearCombinedClassifier(dnn_feature_columns=feature_columns,
                                                     dnn_hidden_units=[20, 5])
x_train = np.array([100., 20., 300., 40.])
y_train = np.array([0, 1, 0, 1])
input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x": x_train}, y_train, batch_size=4, num_epochs=None, shuffle=True)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
    {"x": x_train}, y_train, batch_size=4, num_epochs=1000, shuffle=False)
estimator.train(input_fn=input_fn, steps=1000)

这是我运行时的输出:

INFO:tensorflow:loss = 32.7514, step = 1
INFO:tensorflow:global_step/sec: 30.5939
INFO:tensorflow:loss = 18.7906, step = 101 (3.099 sec)
INFO:tensorflow:global_step/sec: 32.3183
INFO:tensorflow:loss = 15.5917, step = 201 (1.175 sec)
INFO:tensorflow:global_step/sec: 88.6797

您可以想象,将batch_size4更改为1000可能会导致损失 8000 左右 .因此,总而言之,无需担心此损失值。

根据深度和广义学习的原始出版物,他们使用逻辑损失函数进行联合训练。更具体地说,模型实现依赖于应用于softmax输出的交叉熵损失。

在应用逻辑回归之前,他们使用加权和来组合来自两个模型的对数赔率。

损失声明可以在源代码中找到:

...        
head = head_lib._multi_class_head_with_softmax_cross_entropy_loss(  # pylint: disable=protected-access
              n_classes,
              weight_column=weight_column,
label_vocabulary=label_vocabulary)

最新更新