0

我具有以下形状的列车数据集:(300, 5, 720)卷积神经网络input_shape尺寸误差(KERAS,PYTHON)

[[[ 6. 11. 389. ..., 0. 0. 0.] 
    [ 2. 0. 0. ..., 62. 0. 0.] 
    [ 0. 0. 18. ..., 0. 0. 0.] 
    [ 38. 201. 47. ..., 0. 108. 0.] 
    [ 0. 0. 1. ..., 0. 0. 0.]] 

    [[ 136. 95. 0. ..., 0. 0. 0.] 
    [ 85. 88. 85. ..., 0. 31. 0.] 
    [ 0. 0. 0. ..., 0. 0. 0.] 
    [ 0. 0. 0. ..., 0. 0. 0.] 
    [ 13. 19. 0. ..., 0. 0. 0.]]] 

我试图通过每个样品输入到CNN模型中,每个输入是大小(5,720) ,我使用在keras以下模型:

cnn = Sequential() 

    cnn.add(Conv2D(64, (5, 50), 
    padding="same", 
    activation="relu",data_format="channels_last", 
    input_shape=in_shape)) 

    cnn.add(MaxPooling2D(pool_size=(2,2),data_format="channels_last")) 

    cnn.add(Flatten()) 
    cnn.add(Dropout(0.5)) 

    cnn.add(Dense(number_of_classes, activation="softmax")) 

    cnn.compile(loss="categorical_crossentropy", optimizer="adam", metrics= 
    ['accuracy']) 

    cnn.fit(x_train, y_train, 
    batch_size=batch_size, 
    epochs=epochs, 
    validation_data=(x_test, y_test), 
    shuffle=True) 

我使用输入的形状为:

rows,cols=x_train.shape[1:] 
    in_shape=(rows,cols,1) 

,但我收到以下错误:

ValueError: Error when checking model input: expected conv2d_1_input to have 4 dimensions, but got array with shape (300, 5, 720)

如何解决这个问题?

回答

1

这是凯拉斯卷积中最经典的错误之一。它的起源在于当使用channels_last输入尺寸时,即使在只有一个通道的情况下,您也需要输入尺寸为(height, width, channels)。所以基本上重塑:

x_train = x_train.reshape((x_train.shape[0], 5, 720, 1) 

应该解决您的问题。