简体   繁体   English

人工神经网络数字预测

[英]Artificial Neural Network Digit Predictions

I am working on a project on Kaggle about hand written digit recognition .我正在做一个关于手写数字识别的 Kaggle项目。 There is the database for those interested.有兴趣的人可以使用该数据库。 I did the first study using kNN and the results were great with an accuracy of 97%.我使用 kNN 进行了第一项研究,结果非常好,准确率为 97%。

I m trying to implement an Artificial Neural Network on to perform the same analysis.我正在尝试实现人工神经网络来执行相同的分析。

My python code is我的 python 代码是

    import keras
    from keras.models import Sequential
    from keras.layers import Dense# Initialising the ANN
    classifier = Sequential()# Adding the input layer and the first hidden layer
    classifier.add(Dense(units =15 , kernel_initializer = 'uniform', activation = 'sigmoid', input_dim = 784))# Adding the second hidden layer
    classifier.add(Dense(units = 15, kernel_initializer = 'uniform', activation = 'sigmoid')) # Adding the output layer
    classifier.add(Dense(units = 10, kernel_initializer = 'uniform', activation = 'sigmoid'))# Compiling the ANN
    classifier.compile(optimizer = 'sgd', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])# Fitting the ANN to the Training set
    classifier.fit(X_train, Y_train, batch_size = 128, epochs = 300)

# Activation functions: 'sigmoid', 'tanh', 'relu', 

# Predicting the Test set results
Y_pred_ann = classifier.predict(X_test)
score_ann = classifier.evaluate(X_test, Y_test)
score_ann


c_matrix_ann = confusion_matrix(Y_test, Y_pred_ann) # rows = truth, cols = prediction
sns.heatmap(c_matrix_ann, annot=True, square=True, cmap = 'Greens_r')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.rcParams['figure.figsize'] = (8.0, 8.0)    # ... and big plots

The output result is output 结果是

array([[2.7740002e-04, 3.3974648e-06, 4.1400194e-03, ..., 2.4288893e-05,
    7.5080991e-04, 2.3245811e-06],
   [1.0848045e-05, 4.5597553e-06, 6.5565109e-06, ..., 3.4272671e-06,
    1.7035007e-04, 1.5136600e-04],
   [3.2186508e-06, 3.0972064e-03, 9.4562769e-05, ..., 3.3088624e-03,
    1.4249086e-03, 2.0797372e-02],
   ...,
   [3.5762787e-07, 6.1863720e-02, 1.0821521e-03, ..., 1.0878146e-03,
    2.4909973e-03, 4.0937960e-03],
   [5.4240227e-06, 1.4960766e-05, 7.5727701e-05, ..., 1.9669533e-06,
    3.6263466e-04, 3.2812357e-05],
   [1.5248895e-02, 0.0000000e+00, 3.1948090e-05, ..., 1.0699034e-05,
    3.5730004e-04, 1.1205673e-05]], dtype=float32)

while it should be something similar to虽然它应该类似于

array([3, 6, 9, ..., 1, 6, 5])

I get the error message ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets我收到错误消息ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets

because the first array has the shape (12600,10) while the second has (12600,0).因为第一个数组的形状为 (12600,10),而第二个数组的形状为 (12600,0)。

What am I doing wrong?我究竟做错了什么? Does anybody had the same problem before?以前有人遇到过同样的问题吗? I can't find the error.我找不到错误。

The problem is in the confusion matrix.问题出在混淆矩阵中。

You can either change:您可以更改:

Y_pred_ann=classifier.predict_classes(X_test)

Or use或使用

Y_pred_ann=np.argmax(classifier.predict(X_test), axis=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM