python - 为什么神经网络不学习?

标签 python tensorflow keras neural-network

我正在使用一个简单的数据集训练神经网络。我尝试了参数、优化器、学习率的不同组合……但即使在 20 个时期之后,网络仍然没有学到任何东西。

我想知道下面的代码哪里出了问题?

from tensorflow.keras.models import  Sequential, load_model
from tensorflow.keras.layers import Input, Dense, Flatten
from tensorflow import keras
from livelossplot import PlotLossesKeras
from keras.models import Model
from sklearn.datasets import make_classification
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import pandas as pd


seed = 42

X, y = make_classification(n_samples=100000, n_features=2, n_redundant=0, 
                           n_informative=2, random_state=seed)

print(f"Number of features: {X.shape[1]}")
print(f"Number of samples: {X.shape[0]}")


df = pd.DataFrame(np.concatenate((X,y.reshape(-1,1)), axis=1))
df.set_axis([*df.columns[:-1], 'Class'], axis=1, inplace=True)

df['Class'] = df['Class'].astype('int')
X = df.drop('Class', axis=1)
y = df['Class']

X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)

print(f"Train set: {X_train.shape}")
print(f"Validation set: {X_val.shape}")


scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train.astype(np.float64))
X_val_scaled = scaler.transform(X_val.astype(np.float64))

inputs = Input(shape=X_train_scaled.shape[1:])
h0 = Dense(5, activation='relu')(inputs)
h1 = Dense(5, activation='relu')(h0)
preds = Dense(1, activation = 'sigmoid')(h1)

model = Model(inputs=inputs, outputs=preds)
opt = keras.optimizers.Adam(lr=0.0001)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(X_train_scaled, y_train, batch_size=128, epochs=20, verbose=0,
                    validation_data=(X_val_scaled, y_val),
                    callbacks=[PlotLossesKeras()]) 

score_train = model.evaluate(X_train_scaled, y_train, verbose=0)
score_test = model.evaluate(X_val_scaled, y_val, verbose=0) 
print('Train score:', score_train[0])
print('Train accuracy:', score_train[1])
print('Test score:', score_test[0])
print('Test accuracy:', score_test[1])

代码产生以下类型的输出

enter image description here

最佳答案

您使用了错误的损失函数,请更改此行

model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])

例如,

model.compile(optimizer=opt, loss='mse', metrics=['accuracy'])

分类交叉熵需要一个单热编码的y,这意味着,每个类都必须有一个01 。 MSE 只是均方误差,所以它会起作用。但您也可以尝试其他一些损失。

您的y:

[1,0,1]

one-hot 编码y:

[[0,1], [1,0], [0,1]]

enter image description here

关于python - 为什么神经网络不学习?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64634468/

相关文章:

python-3.x - 使用 Keras 进行多类图像分类的多重预测

python - 如何获取在 python 中传递给我的装饰器的函数的文件名?

python - 选择组合框值后获取字典的值

python - 使用进化算法实现对抗性示例生成器表现不佳

java - Python 中的 Tensorflow Java Api `toGraphDef` 等价物是什么?

python - 关于形状不匹配的 Tensorflow2 警告,仍在训练

python - bokeh.charts.Area 中的日期时间 x 轴

python - 将函数应用于 DataFrame 中的每个单元格

tensorflow - 了解 tensorflow 内部/内部并行线程

python - 导入 keras 时出现导入错误