简体   繁体   中英

Keras model.fit() Raises Error About Unspecified Parameter `steps_per_epoch`

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

Your train_dataset and validation_dataset are Datasets (take a look at the documentation of tensorflow for the function from_tensor_slices ): https://www.tensorflow.org/api_docs/python/tf/data/Dataset#from_tensor_slices

I think that you need to consume the data from the dataset. For example you can do it an iterator over your full dataset with the following function:

iterator = dataset.make_one_shot_iterator()

Take a look at tensorflow's documentation about how to consume data from a dataset object: https://www.tensorflow.org/guide/datasets#batching_dataset_elements

Take a look also to this post titled How to Properly Combine TensorFlow's Dataset API and Keras? : How to Properly Combine TensorFlow's Dataset API and Keras?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM