0

I'm having troubles grasping the shape input to the first layer of a network. This is my architecture:

    # Model Hyperparameters
    filter_sizes = [1, 2, 3, 4, 5]
    num_filters = 10
    dropout_prob = [0.5, 0.8]
    hidden_dims = 50

    model_input = Input(shape=(X.shape[0], X.shape[1]))
    z = model_input
    z = Dropout(0.5)(z)

    # Convolutional block
    conv_blocks = []
    for fz in filter_sizes:
        conv = Convolution1D(filters=num_filters,
                             kernel_size=fz,
                             padding="valid",
                             activation="relu",
                             strides=1)(z)
        conv = MaxPooling1D(pool_size=2)(conv)
        conv = Flatten()(conv)
        conv_blocks.append(conv)

    z = Concatenate()(conv_blocks) if len(conv_blocks) > 1 else conv_blocks[0]

    z = Dropout(dropout_prob[1])(z)
    z = Dense(hidden_dims, activation="relu")(z)
    model_output = Dense(3, activation="softmax")(z)

    model = Model(model_input, model_output)
    model.fit(X[train], to_categorical(y[train], num_classes=3))



ValueError: Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (12547, 261)

This is how my data looks like:

array([[ 1,  2,  3, ...,  0,  0,  0],
       [ 5,  6,  7, ...,  0,  0,  0],
       [15, 10,  4, ...,  0,  0,  0],
       ...,
       [ 5,  6,  8, ...,  0,  0,  0],
       [11, 10, 14, ...,  0,  0,  0],
       [14,  8,  8, ...,  0,  0,  0]])

I have 14640 samples with 261 dimensions

FranGoitia
  • 1,965
  • 3
  • 30
  • 49

2 Answers2

0

As the error says it's a shaping problem the shape of input (model_input) you provided should match with the input shape of data that you feed in model.fit

Recheck your shapes using: from keras import backend as K K.shape(input _tensor) if it's a tensor Or np.shape() if it's a numpy array. Also if the shapes don't match(and they won't)use the function K.reshape Fore more help see keras/backend API

Sanchit
  • 29
  • 9
0

According to Keras documentation, Convolution1D layer accepts a 3D tensor as its input. You need to provide step as an extra dimension in your input data. You can check this link to get more information.

Monaj
  • 854
  • 9
  • 16