Python 神经网络模型的超低精度
我遵循了关于使用代码交叉验证进行神经网络模型评估的教程:Python 神经网络模型的超低精度,python,machine-learning,keras,neural-network,cross-validation,Python,Machine Learning,Keras,Neural Network,Cross Validation,我遵循了关于使用代码交叉验证进行神经网络模型评估的教程: # Multiclass Classification with the Iris Flowers Dataset import numpy import pandas from keras.models import Sequential from keras.layers import Dense from keras.wrappers.scikit_learn import KerasClassifier from ke
# Multiclass Classification with the Iris Flowers Dataset
import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.utils import np_utils
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import Pipeline
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load dataset
dataframe = pandas.read_csv("/content/drive/My Drive/iris.data", header=None)
dataset = dataframe.values
X = dataset[:,0:4].astype(float)
Y = dataset[:,4]
# encode class values as integers
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
# convert integers to dummy variables (i.e. one hot encoded)
dummy_y = np_utils.to_categorical(encoded_Y)
# define baseline model
def baseline_model():
# create model
model = Sequential()
model.add(Dense(4, input_dim=4, activation="relu", kernel_initializer="normal"))
model.add(Dense(3, activation="sigmoid", kernel_initializer="normal"))
# Compile model
model.compile(loss= 'categorical_crossentropy' , optimizer= 'adam' , metrics=[ 'accuracy' ])
return model
estimator = KerasClassifier(build_fn=baseline_model, nb_epoch=200, batch_size=5, verbose=0)
kfold = KFold(n_splits=10, shuffle=True, random_state=seed)
results = cross_val_score(estimator, X, dummy_y, cv=kfold)
print("Accuracy: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
准确率应该在95.33%(4.27%)
左右,但我几次尝试就获得了~准确率:34.00%(13.15%)
。模型代码似乎完全相同。我按照指示从中下载了数据。会出什么问题?谢谢更换此:
model.add(Dense(4, input_dim=4, activation="relu", kernel_initializer="normal"))
为此:
model.add(Dense(16, activation="relu"))
model.add(Dense(32, activation="relu"))
然后,将输出层设置为:
model.add(Dense(3, activation="softmax", kernel_initializer="normal"))
你的隐藏层很小,你的激活功能是错误的。对于3+类,它必须是softmax
完整工作代码:
import numpy
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.utils import np_utils
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import MinMaxScaler
seed = 7
numpy.random.seed(seed)
from sklearn.datasets import load_iris
X, encoded_Y = load_iris(return_X_y=True)
mms = MinMaxScaler()
X = mms.fit_transform(X)
dummy_y = np_utils.to_categorical(encoded_Y)
def baseline_model():
model = Sequential()
model.add(Dense(4, input_dim=4, activation="relu", kernel_initializer="normal"))
model.add(Dense(8, activation="relu", kernel_initializer="normal"))
model.add(Dense(3, activation="softmax", kernel_initializer="normal"))
model.compile(loss= 'categorical_crossentropy' , optimizer='adam', metrics=[
'accuracy' ])
return model
estimator = KerasClassifier(build_fn=baseline_model, epochs=200, verbose=0)
kfold = KFold(n_splits=10, shuffle=True, random_state=seed)
results = cross_val_score(estimator, X, dummy_y, cv=kfold)
print(results)
仅凭机会,你就应该获得33%的准确率 如何改进代码:
sigmoid
dosoftmax
更改nb_epoch
(旧Keras)更改为epoch
请记住,完全连接的层并不总是最佳解决方案。谢谢您的帮助。它略微提高到36.67%(15.28%),但不幸的是仍然很低。在密集之前试试CNN。@NicolasGervais谢谢。我得到了同样的结果。我不确定的是,10个输出中有2个在60%左右。这是否意味着2/10的试验精度较低,表明模型性能可能不稳定?本教程怎么可能达到
95.33%
?性能有些不稳定,因为它是一个很小的数据集,变量很少。当您进行“折叠”时,您的数据集甚至更小。诚然,我没有玩太多的超参数。我只是想让它发挥作用。您可能希望降低优化器的学习速度,使其不会徘徊在最小值附近。谢谢您的回答。我可以问一下,如果我们使用测试数据来评估准确性会怎样?它还会过胖吗?替换你建议的代码,同时保持其余不变,我得到92%的acc
Out[5]:
array([0.60000002, 0.93333334, 1. , 0.66666669, 0.80000001,
1. , 1. , 0.93333334, 0.80000001, 0.86666667])
from sklearn.preprocessing import StandardScaler, MinMaxScaler
scaler = StandardScaler()
X = scaler.fit_transform(X)
# define baseline model
def baseline_model():
# create model
model = Sequential()
model.add(Dense(8, input_dim=4, activation="relu"))
model.add(Dense(3, activation="softmax"))
# Compile model
model.compile(loss= 'categorical_crossentropy' , optimizer= 'adam' , metrics=[ 'accuracy' ])
return model
estimator = KerasClassifier(build_fn=baseline_model, epochs=50, batch_size=5, verbose=1)