0

I'm very new to Keras and machine learning in general, and am training a model like so:

history = model.fit_generator(flight_generator(train_files_train, 4), steps_per_epoch=500, epochs=50)

Where flight_generator is a function that prepares the training data and formats it, and then yields it back to the model to fit. this works great, so now I want to add some validation and after much looking online I still don't know how to implement it.

My best guess would be something like:

history = model.fit_generator(flight_generator(train_files_train, 4), steps_per_epoch=500, epochs=50, validation_data=flight_generator(train_files_cv, 4))

But when I run the code it just freezes in the first epoch. What am I missing?

EDIT:

Code for flight_generator:

def flight_generator(files, batch_size):

    while True:
          batch_inputs  = numpy.random.choice(a    = files, 
                                          size = batch_size)
          batch_input_X = []
          batch_input_Y = []
          c=0
          for batch_input in batch_inputs:
            # reshape into X=t and Y=t+1
            trainX, trainY = create_dataset(batch_input, look_back)
            # reshape input to be [samples, time steps, features]
            trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))

            if c is 0:
              batch_input_X = trainX
              batch_input_Y = trainY

            else:
              batch_input_X = numpy.concatenate((batch_input_X, trainX), axis = 0)
              batch_input_Y = numpy.concatenate((batch_input_Y, trainY), axis = 0)

            c += 1


          # Return a tuple of (input) to feed the network

          batch_x = numpy.array( batch_input_X )
          batch_y = numpy.array( batch_input_Y )


          yield(batch_x, batch_y)

2 Answers2

0

Your validation_data should be in format of tuple. So you should try changing it :

history = model.fit_generator(flight_generator(train_files_train, 4), steps_per_epoch=500, epochs=50,batch_size=32,validation_data=(flight_generator(train_files_cv, 4)))

  • Hi, thanks for answering. I tried that before, but get the following error: TypeError: fit_generator() got an unexpected keyword argument 'validation_split' – David García Ballester May 07 '20 at 17:52
  • Maybe validation_split comes only in model.fit and not in model.fit_generator ? – David García Ballester May 07 '20 at 17:53
  • Yes, I didn't see `fit_generatior` –  May 07 '20 at 17:54
  • Check this answer : https://stackoverflow.com/questions/42443936/keras-split-train-test-set-when-using-imagedatagenerator –  May 07 '20 at 17:55
  • @DavidGarcíaBallester please try using the `batch_size` , maybe data is too big to load. –  May 07 '20 at 18:00
  • fit_generator also doesn't have batch_size. Tried with the tuple in validation_data and same problem. Also changed to model.fit to try out validation_split and got the following error: ValueError: `validation_split` is only supported for Tensors or NumPy arrays, found: (, None, None) – David García Ballester May 07 '20 at 18:05
  • According to keras documentation, it supports validation data in tuple. I think you have a different problem. Can you edit your question by adding detail about `fligh_generator`? One other possibility is that your validation data is sequential and in that case you have to add `validation_steps`, if we don't add it then it validation step is equal to length of validation data. Can you try that? –  May 07 '20 at 19:40
  • Edited with the code for flight_generator. I don't know why its giving me such a strange error, since it does yield a tuple. – David García Ballester May 08 '20 at 10:54
0

I guess you should be using model.fit(........) Do not try to use generator unless you actually require it In whatever code I have seen, model.fit() does the magic

Please refer to Keras documentation for fit() https://keras.io/api/models/sequential/ And please mention the optimizer and the metrics

Raj
  • 86
  • 5