自定义回调不适用于keras模型

时间:2019-06-17 18:58:40

标签: python tensorflow keras

我尝试在回调中评估我的tf.keras模型。进行拟合后可以进行模型评估,但不能在回调范围内进行评估。

class Eval(Callback):
    def __init__(self, model,validation_data,eval_batch=1):
        super(Callback, self).__init__()
        self.validation_data=validation_data
        self.eval_batch=eval_batch
        self.model=model
        #self.X_val, self.y_val = validation_data

    def on_epoch_end(self, epoch, logs={}):
        if epoch % 1 == 0:
            print(epoch)
            print(self.validation_data)
            res=self.model.evaluate(self.validation_data, batch_size=self.eval_batch)
            print(res)
            precisions.append(res[3])

            print("Evaluation - epoch: {:d} - Eval_Prec: {:.6f}".format(epoch, res[3]))

致电

  evaluation=Eval(keras_model,eval_input)

  keras_model.fit(
      x_train,y_train,
      epochs=args.num_epochs,
      validation_data=(x_test,y_test),
      batch_size=args.batch_size,
      verbose=1,
      callbacks=[evaluation])

这里是完整的追溯:

      Traceback (most recent call last):
  File "keras_cloud.py", line 435, in <module>
    train_and_evaluate(args)
  File "keras_cloud.py", line 421, in train_and_evaluate
    callbacks=[lr_decay_cb,evaluation])#,tensorboard_cb])
  File "C:\Users\\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1639, in fit
    validation_steps=validation_steps)
  File "C:\Users\\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py", line 239, in fit_loop
    callbacks.on_epoch_end(epoch, epoch_logs)
  File "C:\Users\\Anaconda3\lib\site-packages\tensorflow\python\keras\callbacks.py", line 214, in on_epoch_end
    callback.on_epoch_end(epoch, logs)
  File "keras_cloud.py", line 312, in on_epoch_end
    res=self.model.evaluate(self.validation_data, batch_size=self.eval_batch)
  File "C:\Users\\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1751, in evaluate
    steps=steps)
  File "C:\Users\\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 992, in _standardize_user_data
    class_weight, batch_size)
  File "C:\Users\\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1117, in _standardize_weights
    exception_prefix='input')
  File "C:\Users\\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training_utils.py", line 284, in standardize_input_data
    data = [standardize_single_array(x) for x in data]
  File "C:\Users\\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training_utils.py", line 284, in <listcomp>
    data = [standardize_single_array(x) for x in data]
  File "C:\Users\\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training_utils.py", line 218, in standardize_single_array
    if x.shape is not None and len(x.shape) == 1:
AttributeError: 'float' object has no attribute 'shape'

但是以下方法可行:

  keras_model.fit(...)

  print(keras_model.evaluate(eval_input, steps=1))

我不知道该回调发生了什么?

0 个答案:

没有答案