I am trying to fit a Keras model with a tf.Dataset as my dataset. I specify the parameter steps_per_epoch
. However, this error is raised:
ValueError: When using iterators as input to a model, you should specify the 'steps_per_epoch' argument.
This error confuses me because I am specifying the steps_per_epoch
argument to the length of my dataset. I have tried None
as well as integers less than my dataset length to no avail.
Here is my code:
def build_model():
'''
Function to build a LSTM RNN model that takes in quantitiy, converted week; outputs predicted price
'''
# define model
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(128, activation='relu', input_shape=(num_steps,num_features*input_size)))
model.add(tf.keras.layers.Dense(128*input_size, input_shape=(num_steps,num_features*input_size)))
model.add(tf.keras.layers.Dense(input_size))
model.compile(optimizer='adam', loss='mse')
print(train_data[0].shape, train_data[1].shape)
#cast data
features_type = tf.float32
target_type = tf.float32
train_dataset = tf.data.Dataset.from_tensor_slices((
tf.cast(train_data[0], features_type),
tf.cast(train_data[1], target_type))
)
validation_dataset = tf.data.Dataset.from_tensor_slices((
tf.cast(val_data[0], features_type),
tf.cast(val_data[1], target_type))
)
# fit model
es = tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=1)
model.fit(train_dataset, epochs=500,steps_per_epoch = 134,verbose=1, validation_data = validation_dataset)
# validation_data = (val_data[0], val_data[1])
print(model.summary())
return model