KERAS-前馈NN-损失不减少(结构数据)

KERAS-前馈NN-损失不减少(结构数据),keras,neural-network,classification,loss-function,feed-forward,Keras,Neural Network,Classification,Loss Function,Feed Forward,我试图为(二进制)分类问题创建一个前馈神经网络 我的数据集的头部如下所示: 我的数据帧的形状是(7214,7)。我的目标是二进制,输入变量是: 年龄:num juv_fel_count:num juv_misd_计数:num 其他数量:num 优先计数:num c_费用_学位:cat 在keras示例()之后,我编写了以下代码: val_dataframe = dataframe.sample(frac=0.3, random_state=1337) train_dataframe

我试图为(二进制)分类问题创建一个前馈神经网络

我的数据集的头部如下所示:

我的数据帧的形状是(7214,7)。我的目标是二进制,输入变量是:

  • 年龄:num
  • juv_fel_count:num
  • juv_misd_计数:num
  • 其他数量:num
  • 优先计数:num
  • c_费用_学位:cat
在keras示例()之后,我编写了以下代码:

    val_dataframe = dataframe.sample(frac=0.3, random_state=1337)
train_dataframe = dataframe.drop(val_dataframe.index)

print(
    "Using %d samples for training and %d for validation"
    % (len(train_dataframe), len(val_dataframe))
)
import tensorflow as tf
def dataframe_to_dataset(dataframe):
    dataframe = dataframe.copy()
    labels = dataframe.pop("target")
    ds = tf.data.Dataset.from_tensor_slices((dict(dataframe), labels))
    ds = ds.shuffle(buffer_size=len(dataframe))
    return ds


train_ds = dataframe_to_dataset(train_dataframe)
val_ds = dataframe_to_dataset(val_dataframe)

train_ds = train_ds.batch(50)
val_ds = val_ds.batch(50)

from tensorflow.keras.layers.experimental.preprocessing import CategoryEncoding
from tensorflow.keras.layers.experimental.preprocessing import StringLookup
from tensorflow.keras.layers.experimental.preprocessing import Normalization

def encode_numerical_feature(feature, name, dataset):
    # Create a Normalization layer for our feature
    normalizer = Normalization()

    # Prepare a Dataset that only yields our feature
    feature_ds = dataset.map(lambda x, y: x[name])
    feature_ds = feature_ds.map(lambda x: tf.expand_dims(x, -1))

    # Learn the statistics of the data
    normalizer.adapt(feature_ds)

    # Normalize the input feature
    encoded_feature = normalizer(feature)
    return encoded_feature


def encode_string_categorical_feature(feature, name, dataset):
    # Create a StringLookup layer which will turn strings into integer indices
    index = StringLookup()

    # Prepare a Dataset that only yields our feature
    feature_ds = dataset.map(lambda x, y: x[name])
    feature_ds = feature_ds.map(lambda x: tf.expand_dims(x, -1))

    # Learn the set of possible string values and assign them a fixed integer index
    index.adapt(feature_ds)

    # Turn the string input into integer indices
    encoded_feature = index(feature)

    # Create a CategoryEncoding for our integer indices
    encoder = CategoryEncoding(output_mode="binary")

    # Prepare a dataset of indices
    feature_ds = feature_ds.map(index)

    # Learn the space of possible indices
    encoder.adapt(feature_ds)

    # Apply one-hot encoding to our indices
    encoded_feature = encoder(encoded_feature)
    return encoded_feature

import keras as keras
juv_fel_count = keras.Input(shape=(1,), name="juv_fel_count")
juv_misd_count = keras.Input(shape=(1,), name="juv_misd_count")
juv_other_count = keras.Input(shape=(1,), name="juv_other_count")
priors_count = keras.Input(shape=(1,), name="priors_count")
age = keras.Input(shape=(1,), name="age")
c_charge_degree = keras.Input(shape=(1,), name="c_charge_degree", dtype="string")

all_inputs = [
    age,
    juv_fel_count,
    juv_misd_count,
    juv_other_count,
    priors_count,
    c_charge_degree,
]

c_charge_degree_encoded = encode_string_categorical_feature(c_charge_degree, "c_charge_degree", train_ds)
age_encoded = encode_numerical_feature(age, "age", train_ds)
juv_fel_count_encoded = encode_numerical_feature(juv_fel_count, "juv_fel_count", train_ds)
juv_misd_count_encoded = encode_numerical_feature(juv_misd_count, "juv_misd_count", train_ds)
juv_other_count_encoded = encode_numerical_feature(juv_other_count, "juv_other_count", train_ds)
priors_count_encoded = encode_numerical_feature(priors_count, "priors_count", train_ds)

all_features = layers.concatenate(
    [   juv_fel_count_encoded,
        juv_misd_count_encoded,
        juv_other_count_encoded,
        priors_count_encoded,
        c_charge_degree_encoded,
        age_encoded
    ])

x = layers.Dense(50, activation="relu")(all_features)
x = layers.Dropout(0.5)(x)
z = layers.Dense(50, activation="relu")(x)
z = layers.Dropout(0.5)(z)
y = layers.Dense(50, activation="relu")(z)
y = layers.Dropout(0.5)(y)
h = layers.Dense(20, activation="relu")(y)
output = layers.Dense(1, activation="sigmoid")(h)
model = keras.Model(all_inputs, output)

model.compile('adam', "binary_crossentropy", metrics=["accuracy"])

history = model.fit(train_ds, epochs=200, validation_data=val_ds)
损耗减小,达到0.5;然后它被卡在那里,不再减少。精度约为0.75

结果

pd.DataFrame(history.history)
详情如下:

此外,这些是损失和准确性(培训和验证)的曲线图:

改变网络浅部的学习速率或节点数似乎无法解决问题

而且,我对Keras和机器学习还是个新手;因此,我不能清楚地理解问题在哪里