gpt4 book ai didi

python - keras MLP 精度为零

转载 作者:行者123 更新时间:2023-11-30 08:48:46 25 4
gpt4 key购买 nike

以下是我的 MLP 模型,

layers = [10,20,30,40,50]
model = keras.models.Sequential()
#Stacking Layers
model.add(keras.layers.Dense(layers[0], input_dim = input_dim, activation='relu'))
#Defining the shape of input
for layer in layers[1:]:
model.add(keras.layers.Dense(layer, activation='relu'))
#Layer activation function
# Output layer
model.add(keras.layers.Dense(1, activation='sigmoid'))
#Pre-training
model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
#Training
model.fit(train_set, test_set, validation_split = 0.10, epochs = 50, batch_size = 10, shuffle = True, verbose = 2)
# evaluate the network
loss, accuracy = model.evaluate(train_set, test_set)
print("\nLoss: %.2f, Accuracy: %.2f%%" % (loss, accuracy*100))
#predictions
predt = model.predict(final_test)
print(predt)

问题是,准确度始终为0,错误日志如图所示,

Epoch 48/50 - 0s - loss: 1.0578 - acc: 0.0000e+00 - val_loss: 0.4885 - val_acc: 0.0000e+00 
Epoch 49/50 - 0s - loss: 1.0578 - acc: 0.0000e+00 - val_loss: 0.4885 - val_acc: 0.0000e+00
Epoch 50/50 - 0s - loss: 1.0578 - acc: 0.0000e+00 - val_loss: 0.4885 - val_acc: 0.0000e+00
2422/2422 [==============================] - 0s 17us/step

Loss: 1.00, Accuracy: 0.00%

按照建议,我已将学习信号从 -1,1 更改为 0,1,但以下是错误日志

Epoch 48/50 - 0s - loss: 8.5879 - acc: 0.4672 - val_loss: 8.2912 - val_acc: 0.4856 
Epoch 49/50 - 0s - loss: 8.5879 - acc: 0.4672 - val_loss: 8.2912 - val_acc: 0.4856
Epoch 50/50 - 0s - loss: 8.5879 - acc: 0.4672 - val_loss: 8.2912 - val_acc: 0.4856
2422/2422 [==============================] - 0s 19us/step

最佳答案

你的代码很难读。这不是编写 Keras 模型的推荐标准。尝试一下,让我们知道您会得到什么。假设 X 是一个矩阵,其中行是实例,列是特征。 Y 是标签

您需要添加一个 channel 作为最后一个维度,如使用 TensorFlow 后端时所述。此外,标签应分为 2 个节点,以获得更好的成功机会。与使用具有 2 个节点的概率输出相比,单个神经元映射通常不太成功。

n = 1000         # Number of instances
m = 4 # Number of features
num_classes = 2 # Number of output classes

... # Your code for loading the data

X = X.reshape(n, m,)
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.33)

y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

构建您的模型。最后一层应使用 sigmoid 或 softmax 来执行分类任务。尝试使用 Adadelta 优化器,它已被证明可以通过更有效地遍历梯度并减少振荡来产生更好的结果。我们还将使用交叉熵作为损失函数,这是分类任务的标准。二元交叉熵也很好。

尝试使用标准模型配置。增加节点数量并没有多大意义。该模型应该看起来像一个棱镜,由一小组输入特征、许多隐藏节点和一小组输出节点组成。您应该以最少的隐藏层数为目标,使层更厚,而不是添加层。

input_shape = (m,)

model = Sequential()
model.add(Dense(32, activation='relu', input_shape=input_shape))
model.add(Dense(64, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])

您可以使用以下方式获取模型摘要

model.summary()

训练你的模型

epochs = 100
batch_size = 128
# Fit the model weights.
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))

查看训练期间发生的情况

plt.figure(figsize=(8,10))
plt.subplot(2,1,1)

# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='lower right')

plt.subplot(2,1,2)
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper right')
plt.show()

关于python - keras MLP 精度为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50481178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com