KERAS-前馈NN-损耗未减少结构数据

如何解决KERAS-前馈NN-损耗未减少结构数据

我正在尝试为(二进制)分类问题创建前馈NN。

我的数据集的头看起来像这样: dataset

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

  • 年龄:人数
  • juv_fel_count:数字
  • juv_misd_count:数字
  • juv_other_count:数量
  • 先验计数:num
  • c_charge_degree:猫

下面以keras为例(https://keras.io/examples/structured_data/structured_data_classification_from_scratch/),我编写了以下代码

    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,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,-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)

是以下内容

training

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

Loss

Accuracy

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

此外,我真的对Keras和机器学习陌生。因此我无法清楚地了解问题出在哪里。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?