0

The model was fuctioinnal with only one output, now I try to use multi output forcasting one timestep, but got this error:

Epoch 1/10
2025-11-04 16:20:09.341769: W tensorflow/core/framework/op_kernel.cc:1844] INVALID_ARGUMENT: required broadcastable shapes
InvalidArgumentError: Graph execution error:

Detected at node compile_loss/mse/sub defined at (most recent call last):
  File "/databricks/python_shell/scripts/db_ipykernel_launcher.py", line 52, in <module>

My input data are build this way from a pandas df:

train_data = features.loc[0 : train_split - 1]
val_data = features.loc[train_split:]

train_data.shape
(500000, 10)

x_train = train_data.iloc[:, :6].values
y_train1 = train_data.iloc[:,6].values
y_train2 = train_data.iloc[:,7].values
y_train3 = train_data.iloc[:,8].values
y_train4 = train_data.iloc[:,9].values
combined_targets_train = np.stack([y_train1, y_train2, y_train3, y_train4], axis=-1)

dataset_train = keras.preprocessing.timeseries_dataset_from_array(
    x_train,
    combined_targets_train,
    sequence_length=sequence_length,
    sampling_rate=step,
    batch_size=batch_size,
    shuffle=True,
)

for batch in dataset_train.take(1):
    inputs, targets = batch

print("Input shape:", inputs.numpy().shape)
print("Target shape:", targets.numpy().shape)
Input shape: (16, 120, 6)
Target shape: (16, 4)

In the same way, validation set was build and got this shape Input shape: (16, 120, 6) Target shape: (16, 4)

The model simplify version:

inputs_seq = keras.layers.Input(shape=(120, 6), name='meteo')
lstm_out = LSTM(10, return_sequences=True)(inputs_seq )
...

output_surfrun = Dense(1, activation='linear', name='pred_surf_runoff')(lstm_out )
output_subsurf = Dense(1, activation='linear', name='pred_sub_surf_runoff')(lstm_out )
output_evap = Dense(1, activation='linear', name='pred_evapo')(lstm_out )
output_een = Dense(1, activation='linear', name='pred_een')(lstm_out )

model = keras.Model(inputs=inputs_seq, outputs=[output_surfrun, output_subsurfacerun, output_evap, output_een], name='model_multi_heads')

model.compile(optimizer=keras.optimizers.Adam(learning_rate=learning_rate), loss="mse")

history = model.fit(
    dataset_train,
    epochs=10,
    validation_data=dataset_val
)

I make many shape validation but don't find what the problem.

1 Answer 1

1

Issue 1: Time dimension mismatch: your code return_sequences=True , therefore LSTM return all of ouput series in format of (batch, timesteps, units) , more specific (B, 120, 10) . After go through Dense(1) , return (B, 120, 1) . While target in format (B, 4).

Solution:

  • Remove return_sequences=True or
  • Only get latest step.

Issue 2

Multiple inputs, but only one output targeted to format (B, 4) .

Solution

  • Convert (B, 4) to 4 tensors (B, 1) or,
  • Using one Dense(4) only.
Sign up to request clarification or add additional context in comments.

2 Comments

Issue 2, Convert (B,4) to 4 tensors (B,1), this is not what I do with this line?? :combined_targets_train = np.stack([y_train1, y_train2, y_train3, y_train4], axis=-1)
finaly found how, turn all my label into new df like this: combined_targets = pd.DataFrame(dtset.iloc[:,6:]) in lieu of combined_targets_train = np.stack([y_train1, y_train2, y_train3, y_train4], axis=-1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.