为什么在Keras二进制分类模型中我的精度为零? [英] Why I'm getting zero accuracy in Keras binary classification model?

查看:91
本文介绍了为什么在Keras二进制分类模型中我的精度为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Keras顺序模型,该模型从csv文件中获取输入.当我运行模型时,即使经过20个纪元,其精度仍为零.

I have a Keras Sequential model taking inputs from csv files. When I run the model, its accuracy remains zero even after 20 epochs.

我经历了这两个stackoverflow线程( zero-准确性培训为什么我的角膜塑形模型的精度始终为0 ),但没有任何方法可以解决我的问题.

I have gone through these two stackoverflow threads (zero-accuracy-training and why-is-the-accuracy-for-my-keras-model-always-0) but nothing solved my problem.

由于我的模型是二进制分类,因此我认为它不应该像回归模型那样工作,从而使准确性指标无效. 这是模型

As my model is binary classification, and I think it should not work like a regression model to make accuracy metric ineffective. Here is the Model

def preprocess(*fields):
    return tf.stack(fields[:-1]), tf.stack(fields[-1:]) # x, y


import tensorflow as tf
from tensorflow.keras import layers
from tensorflow import feature_column

import pathlib

csvs =  sorted(str(p) for p in pathlib.Path('.').glob("My_Dataset/*/*/*.csv"))

data_set=tf.data.experimental.CsvDataset(
    csvs, record_defaults=defaults, compression_type=None, buffer_size=None,
    header=True, field_delim=',', use_quote_delim=True, na_value=""
)
print(type(data_set))

#Output: <class 'tensorflow.python.data.experimental.ops.readers.CsvDatasetV2'>

data_set.take(1)

#Output: <TakeDataset shapes: ((), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ()), types: (tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32)>

validate_ds = data_set.map(preprocess).take(10).batch(100).repeat()
train_ds = data_set.map(preprocess).skip(10).take(90).batch(100).repeat()

model = tf.keras.Sequential([
    layers.Dense(256,activation='elu'),  
    layers.Dense(128,activation='elu'),  
    layers.Dense(64,activation='elu'),  
    layers.Dense(1,activation='sigmoid') 
])


model.compile(optimizer='adam',
            loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
            metrics=['accuracy'])    #have to find the related evaluation metrics


model.fit(train_ds,
        validation_data=validate_ds,
        validation_steps=5,
        steps_per_epoch= 5,
        epochs=20,
        verbose=1
        )

我做错了什么?

推荐答案

您确定您的任务是分类任务吗?

Are you sure that yours is a classification task?

因为从目标变量(从csv中提取的目标变量)可以看出,类型是浮点数

Because as I can see from your target variable, the one that you extract from the csv, the type is a float

#Output: <TakeDataset shapes: ((), (), ..., tf.float32)>

如果这是二进制分类任务,则还要检查目标值中的值是否为0和1.否则该模型将无法正常运行

If it's a binary classification task, check also that the values in the target values are 0s and 1s. Otherwise the model will perform poorly

类似这样的东西:

[0, 1, 0, 1, 0, 0, 0 ..., 1]

因为交叉熵适用于0和1

Because the crossentropy works with 0 and 1

这就是为什么您将Sigmoid用作激活函数的原因,该函数将输出[0,1]范围内的值

That's the reason why you use the sigmoid as activation function, which will output values in the range [0, 1]

也已经建议您设置from_logits=False

这篇关于为什么在Keras二进制分类模型中我的精度为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆