How do you pass validation data to Keras using a sequence?

Viewed 419

Looking at the TensorFlow documentation it says that model.fit(validation_data) cannot be used with keras.utils.Sequence

Note that validation_data does not support all the data types that are supported in x, eg, dict, generator or keras.utils.Sequence.

My validation set is probably just small enough to fit into RAM, but I'd like to avoid loading it all into RAM in case my dataset grows.

To get an idea of how my current Sequence is working, here is the code:

NOTES:

  • The sequence currently only processes train_data which is a normalized array containing my examples and labels. I have similar arrays for val_data and test_data.
  • This loop may look a bit odd because I am working with time-series data that pulls a window for each example.
class MyGenerator(tf.keras.utils.Sequence):
    'Generates data for Keras'
    def __init__(self, ids, train_dir):
        'Initialization'
        self.ids = ids
        self.train_dir = train_dir

    def __len__(self):
        'Denotes the number of batches per epoch'
        return len(self.ids)

    def __getitem__(self, index):
        batch_id = self.ids[index]
        # load data
        X_train, y_train = [], []
        start_index = seq_len*batch_id
        end_index = start_index + seq_len
        for i in range(start_index, end_index):  
            start_seq = i + start_index
            X_train.append(train_data[i-seq_len:i])  
            y_train.append(train_data[:, 4][i]) 
        # Save our batch
        X = np.array(X_train)
        y = np.array(y_train)

        return X, y

Is there a way for me to process my validation set in batches? I would prefer to use Sequence, but if that is not possible I'm open to other options.

1 Answers

According to the documentstion:

validation_data could be:

  • tuple (x_val, y_val) of Numpy arrays or tensors
  • tuple (x_val, y_val, val_sample_weights) of Numpy arrays
  • dataset

For the first two cases, batch_size must be provided. For the last case, validation_steps could be provided.

So by providing batch_size or validation_steps the validation_data would be processed in batches.

Related