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

标签 python machine-learning tensorflow deep-learning

我正在 Kaggle's Credit Card Fraud 上尝试 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.Class)作为二进制特征。 Tensorflow 的训练日志

m.train(input_fn(data, 3))

是:

INFO:tensorflow:loss = 532.633, step = 2566 INFO:tensorflow:global_step/sec: 37.9815 INFO:tensorflow:loss = 560.574, step = 2666 (2.635 sec) INFO:tensorflow:global_step/sec: 38.3186

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

最佳答案

What is the loss function being used here?

如果是二元分类,则为_BinaryLogisticHeadWithSigmoidCrossEntropyLoss - tf.nn.sigmoid_cross_entropy_with_logits 周围的内部包装损失函数。您会对大值感到困惑,但如果您仔细观察该函数的计算内容,您会发现大值是很有可能的。

如果x是 logits 和 z是标签(单个示例),则损失等于 x - x * z + log(1 + exp(-x)) ,相当于x ,当 z == 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_size来自41000可能会导致 8000 附近的损失。因此,总而言之,无需担心此损失值。

关于python - Tensorflow 的 DNNLinearCombinedClassifier 打印回归损失而不是分类损失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46729794/

相关文章:

python-3.x - 值错误: The number of class labels must be greater than one in Passive Aggressive Classifier

python - K 表示具有指定簇内距离的簇

python - pandas Mean() 值给出空白

machine-learning - 在 Keras 中为序列到序列自动编码器制作解码器模型

python - 正则表达式匹配 - Python - 任意数量的字符

machine-learning - tensorflow word2vec 示例中权重和偏差的目的是什么?

tensorflow - 将张量的所有负值设置为零(在 tensorflow 中)

tensorflow - 如何计算 tensorflow 中tfprof的触发器?

python - 尝试使用Python和BeautifulSoup来提取准确的值

python - 从 rtsp H.264 视频流中捕获单个图像