Keras 预测二进制问题的浮点输出

Posted

技术标签:

【中文标题】Keras 预测二进制问题的浮点输出【英文标题】:Keras predicting floating point output for a binary problem 【发布时间】:2021-12-15 14:44:55 【问题描述】:

我在 Keras 有一个模型:

import tensorflow as tf
import numpy as np
import pandas as pd
from tensorflow import keras
from tensorflow.keras import layers
from sklearn.model_selection import train_test_split
import random

df = pd.read_csv('/home/Datasets/creditcard.csv')

output = df['Class']
features = df.drop('Class', 1)

train_features, test_features, train_labels, test_labels = train_test_split(df, output, test_size = 0.2, random_state = 42)

train_features = train_features.to_numpy()
test_features = test_features.to_numpy()
train_labels = train_labels.to_numpy()
test_labels = test_labels.to_numpy()

model = tf.keras.Sequential()

num_nodes = [1]
act_functions = [tf.nn.relu]
optimizers = ['SGD']
loss_functions = ['categorical_crossentropy']
epochs_count = ['10']
batch_sizes = ['500']

act = random.choice(act_functions)
opt = random.choice(optimizers)
ep = random.choice(epochs_count)
batch = random.choice(batch_sizes)
loss = random.choice(loss_functions) 
count = random.choice(num_nodes)

model.add(tf.keras.layers.Dense(31, activation = act, input_shape=(31,)))  
model.add(tf.keras.layers.Dense(count, activation = act)) 
model.add(tf.keras.layers.Dense(1, activation = act)) 
model.compile(loss = loss,
         optimizer = opt,
         metrics = ['accuracy'])

epochs = int(ep)
batch_size = int(batch)
model.fit(train_features, train_labels, epochs=epochs, batch_size=batch_size)

火车标签是二进制的:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

但是,输出:

z = model.predict(test_features)

是:

array([[ 4574.6   ],
       [ 4896.158 ],
       [ 3867.8225],
       ...,
       [15516.117 ],
       [ 6441.43  ],
       [ 5453.437 ]], dtype=float32)

为什么要预测这些值?

谢谢

【问题讨论】:

【参考方案1】:

在最后一层使用 sigmoid 激活,

model.add(tf.keras.layers.Dense(1, activation='sigmoid'))

【讨论】:

谢谢!在这样做的时候,我得到了这样的回应——我应该把它弄圆吗?数组([[1.7022928e-20],[1.7022928e-20],[1.7022928e-20], 啊,我看到它返回属于 True 类的概率,对吧? 是的,这就是概率。 谢谢!感谢您的帮助

以上是关于Keras 预测二进制问题的浮点输出的主要内容,如果未能解决你的问题,请参考以下文章

在 keras 中使用 CNN 对图像进行二值分类时正确预测的总数

Keras 用于二元分类预测的 fit_generator() 总是 50%

Keras 中二进制分类的输出层

keras ImageDataGenerator 插值二进制掩码

使用 keras ResNet50 模型进行二进制分类的输出层

如何在 Python 中将浮点数列表输出到二进制文件