检查目标时出错:预期density_10的形状为(1,),但数组的形状为(10,)

时间:2019-04-30 07:34:31

标签: python-3.x conv-neural-network

有人可以指导我完成此操作,这就是为什么我遇到此错误的原因。预先感谢

当我将标签更改为1或0时它起作用。但是现在我试图获得更具体的结果,所以我根据类别(即0,1 .... 9)更改了标签。总共10个类别。

CODE

    `print (train.shape)             #when read the .csv file
    print (test.shape)              #when read the .csv file

    y_train = train[:,0]            #assign train labels
    y_test = test[:,0]              #assign test labels

    #reshaping
    X_train = train[:, 1:].reshape(train.shape[0], 13, 3, 1)
    X_test = test[:, 1:].reshape(test.shape[0], 13, 13, 1)
    input_shape = (13, 3, 1)        

    # convert class vectors to binary class matrices
    y_train = keras.utils.to_categorical(y_train, 10)
    y_test = keras.utils.to_categorical(y_test, 10)
    print (X_train.shape)
    print (y_train.shape)
    print (X_test.shape)
    print (y_test.shape)

    model = Sequential()
    model.add(Conv2D(32, kernel_size=(2, 2), activation='relu', 
    input_shape=input_shape))
    model.add(Flatten())
    model.add(Dense(1000, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(10, activation='softmax'))

    model.fit(X_train, y_train,
      batch_size=128,
      epochs=10,
      verbose=1,
      validation_data=(X_test, y_test))          #error in this line`

输出

(82332, 40)                  #when read the .csv file
(175341, 40)                 #when read the .csv file

#reshaped

(82332, 13, 3, 1)
(82332, 10)
(175341, 13, 3, 1)
(175341, 10)

ValueError                  Traceback (most recent call last)
<ipython-input-20-526f89d6ead2> in <module>()
      6           epochs=10,
      7           verbose=1,
----> 8           validation_data=(X_test, y_test))
ValueError: Error when checking target: expected dense_2 to have shape 
(1,) but got array with shape (10,)

1 个答案:

答案 0 :(得分:0)

您可能需要提供更多信息?您如何编译模型?

使用您的代码作为简单测试的基础,我没有收到错误。

from tensorflow.python.keras import Sequential
from tensorflow.python.keras.layers import Conv2D, Flatten, Dense, Dropout
import numpy as np


X_train = np.zeros(shape=(100, 13, 3, 1))
y_train = np.zeros(shape=(100, 10))
X_test = np.zeros(shape=(100, 13, 3, 1))
y_test = np.zeros(shape=(100, 10))

# convert class vectors to binary class matrices
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)

input_shape = (13, 3, 1)

model = Sequential()
model.add(Conv2D(32, kernel_size=(2, 2), activation='relu',
                 input_shape=input_shape))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

model.compile("Adam", loss="categorical_crossentropy")  # Added this line

model.fit(X_train, y_train,
          batch_size=128,
          epochs=10,
          verbose=1,
          validation_data=(X_test, y_test))

Train on 100 samples, validate on 100 samples
Epoch 1/10
100/100 [==============================] - 1s 5ms/sample - loss: 0.0000e+00 - val_loss: 0.0000e+00
Epoch 2/10
100/100 [==============================] - 0s 156us/sample - loss: 0.0000e+00 - val_loss: 0.0000e+00
Epoch 3/10
100/100 [==============================] - 0s 377us/sample - loss: 0.0000e+00 - val_loss: 0.0000e+00
Epoch 4/10
100/100 [==============================] - 0s 312us/sample - loss: 0.0000e+00 - val_loss: 0.0000e+00
Epoch 5/10
100/100 [==============================] - 0s 156us/sample - loss: 0.0000e+00 - val_loss: 0.0000e+00
Epoch 6/10
100/100 [==============================] - 0s 312us/sample - loss: 0.0000e+00 - val_loss: 0.0000e+00
Epoch 7/10
100/100 [==============================] - 0s 221us/sample - loss: 0.0000e+00 - val_loss: 0.0000e+00
Epoch 8/10
100/100 [==============================] - 0s 312us/sample - loss: 0.0000e+00 - val_loss: 0.0000e+00
Epoch 9/10
100/100 [==============================] - 0s 312us/sample - loss: 0.0000e+00 - val_loss: 0.0000e+00
Epoch 10/10
100/100 [==============================] - 0s 377us/sample - loss: 0.0000e+00 - val_loss: 0.0000e+00