人工神经网络数字预测

时间:2019-10-23 17:16:33

标签: python keras neural-network deep-learning

我正在Kaggle about hand written digit recognition上的一个项目上工作。有那些感兴趣的数据库。 我使用kNN进行了第一个研究,结果很好,准确率达97%。

我正在尝试实施人工神经网络以执行相同的分析。

我的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

输出结果是

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)

它应该类似于

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

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

因为第一个数组的形状为(12600,10),而第二个数组的形状为(12600,0)。

我在做什么错?有人遇到过同样的问题吗?我找不到错误。

1 个答案:

答案 0 :(得分:0)

问题出在混乱矩阵中。

您可以更改:

document.querySelector("div#the-div-id")

或使用

Y_pred_ann=classifier.predict_classes(X_test)