Q:

How can I generate validation data through a data generator when the model is trained through 'fit_generator' function?

I'm generating my image captioning model's training data through a data generator which is added below. This model is based on the model provided here. How can I generate and set validation data in a similar fashion during the training? I do have the features of the validation images and their captions.
Data Generator:
def data_generator(all_train_captions, train_features, wordtoix, max_length, num_photos_per_batch, vocab_size):
    X1, X2, y = list(), list(), list()
    n = 0
    # loop for ever over images
    while True:
        for image_id, desc in all_train_captions.items():
            image_id = image_id.strip()

            n += 1
            # retrieve the photo feature
            photo = train_features[image_id]
            # encode the sequence
            seq = [wordtoix[word] for word in desc.split(' ') if word in wordtoix]
            # split one sequence into multiple X, y pairs
            for i in range(1, len(seq)):
                # split into input and output pair
                in_seq, out_seq = seq[:i], seq[i]
                # pad input sequence
                in_seq = pad_sequences([in_seq], maxlen=max_length)[0]
                # encode output sequence
                out_seq = to_categorical([out_seq], num_classes=vocab_size)[0]
                # store
                X1.append(photo)
                X2.append(in_seq)
                y.append(out_seq)
            # yield the batch data
            if n == num_photos_per_batch:
                yield [[array(X1), array(X2)], array(y)]
                X1, X2, y = list(), list(), list()
                n = 0

Model training
generator = data_generator(all_train_captions, encoding_train, wordtoix, max_caption_length,
                           number_pics_per_bath, vocab_size)
history = model.fit_generator(generator, epochs=epochs, steps_per_epoch=steps, callbacks=callbacks, verbose=1)

p.s. Using Keras on the TensorFlow backend as the deep learning library.

A:

You need another generator. 
One for training, one for validation. 
Just create two generators, one using training data, the other using validation data. 
train_generator = data_generator(all_train_captions, encoding_train, wordtoix, max_caption_length, 
                                 number_pics_per_bath, vocab_size)
val_generator = data_generator(all_val_captions, encoding_val, wordtoix, max_caption_length, 
                               number_pics_per_bath, vocab_size)

