PEREZje
PEREZje

Reputation: 2502

ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (2849, 1, 2)

So I want to predict the location of an agent within an environment encoded using cartesian coordinates. For that I want to use an LSTM model but I am having some issues with setting a simple one up that I can then expand on. The data I use looks like this:

    x0  y0  x1  y1  x2  y2  x5  y5
0   0   5   1   5   1   4   3   3
1   1   5   1   4   2   4   3   2
2   1   4   2   4   2   3   4   2
3   2   4   2   3   3   3   4   1

Where x0 through y2 are the features (or X) (with the number indicating the time step) and x5 and y5 is the to be predicted value (or y). So first I preprocessed the data to fit into an LSTM model like so:

path_df = pd.read_csv("data/preprocessed_data.csv", sep="\t", index_col=0)

X = path_df[["x0", "y0", "x1", "y1", "x2", "y2"]].to_numpy()
y = path_df[["x5", "y5"]].to_numpy()

X = X.reshape(len(X), 3, 2)
y = y.reshape(len(y), 1, 2)

This gives me arrays that look like this:

X[0] = 
  [[[ 3  1]
    [ 3  2]
    [ 2  2]]
Y[0] = 
  [[ 1 4]]

I think this is properly formatted to use in an LSTM model (if it is not please tell me). I then create a simple model usig keras like so:

model = Sequential()

model.add(LSTM(4, input_shape=(3, 2)))
model.add(Dense(1))
model.compile(loss="mean_squared_error", optimizer="adam")
model.fit(X, y, epochs=100, verbose=2)

If I'm correct I believe that this would give me a model that has an input layer of the shape (3,2) which is correct given the input data. And an output layer that should give me 1 value, which would be the predicted location. But when I run this I get:

ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (2849, 1, 2)

And I don't fully understand where this is coming from, the 2849 is the size of my data-set so that is where that number is coming from but I don't understand how to fix this. Any help would be appreciated!

Upvotes: 1

Views: 125

Answers (1)

Marco Cerliani
Marco Cerliani

Reputation: 22031

your model output is actually 2D so you need to pass a 2D target. you don't need to reshape the target in this way y.reshape(len(y), 1, 2). simply let it in original 2D format

X = np.random.uniform(0,1, (100,3,2))
y = np.random.uniform(0,1, (100,2))

model = Sequential()
model.add(LSTM(4, input_shape=(3, 2)))
model.add(Dense(2))
model.compile(loss="mean_squared_error", optimizer="adam")
model.fit(X, y, epochs=100, verbose=2)

your inputs look correct. remember to set your Dense(2) in the output because you have 2 output features/coordinates

Upvotes: 1

Related Questions