如何解决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)
是以下内容:
此外,这些是损失和准确性(训练和验证)的图:
此外,我真的对Keras和机器学习陌生。因此我无法清楚地了解问题出在哪里。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。