1

I was trying to do a tutorial on time series model with tensorflow and I got an error regarding reshaping presumably coming from a reshape layer.

train_dataset = tf.keras.utils.timeseries_dataset_from_array(
    train_norm,
    targets=temperature_train[delay:],
    sampling_rate=sampling_rate,
    sequence_length=sequence_length,
    shuffle=True,
    batch_size=batch_size,
)

valid_dataset = tf.keras.utils.timeseries_dataset_from_array(
    valid_norm,
    targets=temperature_valid[delay:],
    sampling_rate=sampling_rate,
    sequence_length=sequence_length,
    shuffle=True,
    batch_size=batch_size,
)

for batch in train_dataset.take(1):
    inputs, label = batch
    print(inputs.shape)
    print(label.shape)
    break

The print results are

(64, 120, 14)
(64,)

The following assertion block raised no error for train_dataset and valid_dataset:

for batch in train_dataset:
    inputs, label = batch
    assert inputs.shape[1:] == (120, 14), f"{inputs.shape[1:]}"

When I build a model and start training

inputs = tf.keras.Input(shape=(inputs.shape[1:]))
x = tf.keras.layers.Flatten()(inputs)
x = tf.keras.layers.Dense(16, activation="relu")(x)
outputs = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(inputs, outputs)
model.compile("adam", loss="mse", metrics=["mae"])
callbacks = [
tf.keras.callbacks.ModelCheckpoint("jena_dense.keras", save_best_only=True)]

history = model.fit(train_dataset, validation_data = valid_dataset, epochs = 15, callbacks=callbacks )

It throws a following error

InvalidArgumentError: Graph execution error:

Detected at node functional_10_1/flatten_13_1/Reshape defined at (most recent call last)

...
...

Only one input size may be -1, not both 0 and 1
     [[{{node functional_10_1/flatten_13_1/Reshape}}]] [Op:__inference_multi_step_on_iterator_40343]

The input shape for the flatten layer shouldn't be unambiguous confirming from the assertion test. I don't know where it comes from. Could someone help me?

2 Answers 2

0

It seems like your valid_dataset is actually completely empty because your validation array is too short to form a sequence after the delay slice. The assert block missed this because iterating over an empty dataset simply exits the loop immediately without running the assertion. Try to ensure len(temperature_valid[delay:]) >= sequence_length and the flatten error will disappear. Please let me know if it works for you. Thanks!

Sign up to request clarification or add additional context in comments.

3 Comments

Removing validation data in model.fit method gives the same result. history = model.fit( train_dataset, epochs=15 ) So it is not the case. And valid dataset is not at all empty after shortening with delay.
Did you try bypassing Flatten() entirely by replacing it with tf.keras.layers.Reshape((1680,))? Sometimes what happens is timeseries_dataset_from_array loses the static sequence length during graph execution which causes Flatten to crash. Hardcoding the math (120 * 14 = 1680) usually bypasses this.
Yes, reshaping works.. And this sounds like a bug
0

Try Replacing

inputs = tf.keras.Input(shape=(inputs.shape\[1:\]))

with:

inputs = tf.keras.Input(shape=(120, 14))

[EDIT]

train_dataset = tf.keras.utils.timeseries_dataset_from_array(
    train_norm[:-delay],
    targets=temperature_train[delay:],
    sampling_rate=sampling_rate,
    sequence_length=sequence_length,
    shuffle=True,
    batch_size=batch_size,
)

valid_dataset = tf.keras.utils.timeseries_dataset_from_array(
    valid_norm[:-delay],
    targets=temperature_valid[delay:],
    sampling_rate=sampling_rate,
    sequence_length=sequence_length,
    shuffle=True,
    batch_size=batch_size,
)

3 Comments

Yes, as my comment on the other answer says, I tried this and it gives the same result.
You shifted targets forward but didn’t shorten inputs, so the dataset produces mismatched sample counts, which breaks batching and triggers the reshape error. I've added a potential fix to the original rsponse
Actually, the implementation of the method takes care of it even if it goes without the shortening. Putting the full dataset or the shortened one does not make any difference. I tried it and it shows the same thing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.