简体   繁体   English

训练中的验证 Keras

[英]Validation in training Keras

I'm very new to Keras and machine learning in general, and am training a model like so:我对 Keras 和机器学习非常陌生,我正在训练 model,如下所示:

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.其中 flight_generator 是 function 准备训练数据并对其进行格式化,然后将其返回给 model 以适应。 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: 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)

Your validation_data should be in format of tuple.您的validation_data应该是元组格式。 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)))

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我猜你应该使用 model.fit(........) 除非你真的需要它,否则不要尝试使用生成器在我看到的任何代码中,model.fit() 都有魔力

Please refer to Keras documentation for fit() https://keras.io/api/models/sequential/ And please mention the optimizer and the metrics请参阅 Keras 文档了解 fit() https://keras.io/api/models/sequential/请提及优化器和指标

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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