I'm still new to using TensorFlow datasets and would like some help with the following. Assume I have a matrix where each row is an observation and I would like to use the window function in order to prepare the data. A sample code is as follows:
import tensorflow as tf
import numpy as np
my_random = np.random.rand(3, 5)
dataset = tf.data.Dataset.from_tensor_slices(my_random)
There are three rows for three observations and each one has five columns. I would like to use the window function with a size of 3:
dataset = dataset .window(3, shift=1, drop_remainder=True)
dataset = dataset .flat_map(lambda window: window.batch(3))
dataset = dataset .map(lambda window: (window[:, :-1], window[:, -1:]))
Then I would like to use this data set to train a RNN model:
model = keras.models.Sequential([
keras.layers.GRU(128, return_sequences=True, input_shape=[None, 4], dropout=0.2, recurrent_dropout=0.2),
keras.layers.GRU(128, return_sequences=True, dropout=0.2, recurrent_dropout=0.2),
keras.layers.TimeDistributed(keras.layers.Dense(1, activation='relu'))
])
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(dataset, epochs=20)
When I run the above code I get an error, and I am not sure what is the issue.
Input 0 of layer "gru_9" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 4)
As I said, I am still new to this and would like to know what the issue is and how to fix it.
I think you just forgot to batch your dataset, meaning you are missing the batch dimension. Try:
dataset = dataset.map(lambda window: (window[:, :-1], window[:, -1:])).batch(2)