Keras Functional API: "Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (X, Y)"

keras model
keras dense
keras concatenate
keras multiple outputs
tensorflow keras functional api example
keras layers
keras inputlayer
keras dropout functional api

I'm trying to fit a ConvNet model using Keras' fit generator, but it fails when trying to feed the data to the input layer. It's telling me it's expecting a three-dimensional input, but my input is only two. If I add a channel to my input shape, it asks for four dimensions.

Here's the exact error when I don't add a channel parameter:

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

And again when I change the input shape to (1000, 597, 1):

ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (1000, 597)

Here's the code for my model:

def initialise_model():
    input_layer = Input((1, 1000, 597))
    conv_layer_1 = Conv2D(filters=30, kernel_size=(10, 1), strides=(1, 1), padding="same", activation="relu")(input_layer)
    conv_layer_2 = Conv2D(filters=30, kernel_size=(8, 1), strides=(8, 1), padding="same", activation="relu")(conv_layer_1)
    conv_layer_3 = Conv2D(filters=40, kernel_size=(6, 1), strides=(6, 1), padding="same", activation="relu")(conv_layer_2)
    conv_layer_4 = Conv2D(filters=50, kernel_size=(5, 1), strides=(1, 1), padding="same", activation="relu")(conv_layer_3)
    conv_layer_5 = Conv2D(filters=50, kernel_size=(5, 1), strides=(1, 1), padding="same", activation="relu")(conv_layer_4)
    flatten_layer = Flatten()(conv_layer_5)
    dense_layer = Dense(1024, activation="relu")(flatten_layer)
    label_layer = Dense(1024, activation="relu")(dense_layer)
    output_layer = Dense(1, activation="linear")(label_layer)

    model = Model(inputs=input_layer, outputs=output_layer)

    adam_optimiser = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
    model.compile(optimizer=adam_optimiser, loss="mean_squared_error", metrics=["accuracy", "mean_squared_error"])
    return model

And my fit generator:

model = initialise_model()
early_stopping = EarlyStopping(monitor="val_loss", min_delta=0, patience=0, verbose=1, restore_best_weights=True)
model.fit_generator(generator, epochs=1, steps_per_epoch=1, verbose=2, callbacks=[early_stopping])

It's worth noting that the output of my generator is as expected, with the correct shape.

Many thanks

set our input layer as

input_layer = Input((1, 1000, 597))

or if channels are set to last

input_layer = Input((1000, 597, 1))

and make sure that your generator yields x_train data of shape

(batch_size, 1, 1000, 597)

or

(batch_size, 1000, 597, 1)

The Model class, 2. Keras Functional Models. The Keras functional API provides a more flexible way for defining models. It specifically allows you to define multiple  Introduction The Keras functional API is a way to create models that is more flexible than the tf.keras.Sequential API. The functional API can handle models with non-linear topology, models with shared layers, and models with multiple inputs or outputs. The main idea that a deep learning model is usually a directed acyclic graph (DAG) of layers.

Have a look at the Keras documentation for Convolutional layers. The input shape should be (batch, channels, rows, cols) for 2D convolutions. That means you need to pass (channels, rows, cols) to your Input-layer. There is actually no need for an reshape-layer as far as I can tell.

What are the dimensions of your images? I assume you want to pass images since you use 2D convolutions, but it looks a little bit more like you're trying to pass vectors.

Your loss is MSE which is fine, but your metric is accuracy. Why?

How to Use the Keras Functional API for Deep Learning, The Keras functional API is the way to go for defining complex models, such as multi-output models, directed acyclic graphs, or models with shared layers. The Keras functional API provides a more flexible way for defining models. It specifically allows you to define multiple input or output models as well as models that share layers. More than that, it allows you to define ad hoc acyclic network graphs.

conv2D layer has by default data_format = "channels_last" if you want to use image having a shape (1, 1000, 597) then you have to set data_format = "channels_first" or change input dim to (1000, 597, 1)

Guide to the Functional API • keras, Keras sequential api is a popular model for creating any deep learning model. However the model comes with the assumption that it would take  The Keras functional API is the way to go for defining complex models, such as multi-output models, directed acyclic graphs, or models with shared layers. This guide assumes that you are already familiar with the Sequential model. Let’s start with something simple. First example: a densely-connected network

Developing With Keras Functional API - Data Driven Investor, If we need to build arbitrary graphs of layers, Keras functional API can do that for us. we are going to build each of these models and explain  Keras graph construction using Functional API A graph consists of edges and nodes and Keras graph is no different. Keras graph is a directed graph 4 in which layers act as nodes and tensors act as edges. Specifying the edges is straightforward – we simply need to create the Layer objects.

Sequential API vs Functional API model in Keras, Keras API specification does not define how the tensor computations are performed at a lower level; that is the job for a deep learning backend  The Keras functional API is useful for creating complex models, such as multi-input/multi-output models, directed acyclic graphs (DAGs), and models with shared layers. The functional API uses the

A Guide to Keras Functional API – Perfectly Random, The Keras functional API is the way to go for defining complex models, such as multi-output models, directed acyclic graphs, or models with shared layers. The core data structure of Keras is a model, which let us to organize and design layers. Sequential and Functional are two ways to build Keras models. Sequential model is simplest type of model, a

Comments
  • what is you target shape?
  • please provide full error
  • please provide generator code too, why you need a reshape layer??
  • @IoannisNasios the target shape of the convnet is (1000, 1)
  • @Mukul As someone else pointed out, the reshape was a mistake on my part and it's been removed from the sample code
  • Having changed my code to reflect your suggested changes, I'm still getting the same error. I'll update the code in my question though! I'm using the generator to produce sliding windows, which is why I'm using 2D convolutions.