from __future__ import absolute_import
from __future__ import print_function
import theano
import theano.tensor as T
import numpy as np
import warnings
import time
from collections import deque

from .utils.generic_utils import Progbar

class CallbackList(object):

    def __init__(self, callbacks, queue_length=10):
        self.callbacks = callbacks
        self.queue_length = queue_length

    def append(self, callback):
        self.callbacks.append(callback)

    def _set_params(self, params):
        for callback in self.callbacks:
            callback._set_params(params)

    def _set_model(self, model):
        for callback in self.callbacks:
            callback._set_model(model)

    def on_epoch_begin(self, epoch):
        for callback in self.callbacks:
            callback.on_epoch_begin(epoch)
        self._delta_t_batch = 0.
        self._delta_ts_batch_begin = deque([], maxlen=self.queue_length)
        self._delta_ts_batch_end = deque([], maxlen=self.queue_length)

    def on_epoch_end(self, epoch):
        for callback in self.callbacks:
            callback.on_epoch_end(epoch)

    def on_batch_begin(self, batch):
        t_before_callbacks = time.time()
        for callback in self.callbacks:
            callback.on_batch_begin(batch)
        self._delta_ts_batch_begin.append(time.time() - t_before_callbacks)
        delta_t_median = np.median(self._delta_ts_batch_begin)
        if self._delta_t_batch > 0. and delta_t_median > 0.95 * self._delta_t_batch \
            and delta_t_median > 0.1:
            warnings.warn('Method on_batch_begin() is slow compared '
                'to the batch update (%f). Check your callbacks.' % delta_t_median)
        self._t_enter_batch = time.time()

    def on_batch_end(self, batch):
        self._delta_t_batch = time.time() - self._t_enter_batch
        t_before_callbacks = time.time()
        for callback in self.callbacks:
            callback.on_batch_end(batch)
        self._delta_ts_batch_end.append(time.time() - t_before_callbacks)
        delta_t_median = np.median(self._delta_ts_batch_end)
        if self._delta_t_batch > 0. and delta_t_median > 0.95 * self._delta_t_batch \
            and delta_t_median > 0.1:
            warnings.warn('Method on_batch_end() is slow compared '
                'to the batch update (%f). Check your callbacks.' % delta_t_median)

    def on_train_begin(self):
        for callback in self.callbacks:
            callback.on_train_begin()

    def on_train_end(self):
        for callback in self.callbacks:
            callback.on_train_end()


class Callback(object):

    def __init__(self):
        pass

    def _set_params(self, params):
        self.params = params

    def _set_model(self, model):
        self.model = model

    def on_epoch_begin(self, epoch):
        pass

    def on_epoch_end(self, epoch):
        pass

    def on_batch_begin(self, batch):
        pass

    def on_batch_end(self, batch):
        pass

    def on_train_begin(self):
        pass

    def on_train_end(self):
        pass

class History(Callback):

    def on_train_begin(self):
        self.epochs = []
        self.losses = []
        if self.params['show_accuracy']:
            self.accuracies = []
        if self.params['do_validation']:
            self.validation_losses = []
            if self.params['show_accuracy']:
                self.validation_accuracies = []

    def on_epoch_begin(self, epoch):
        self.seen = 0
        self.tot_loss = 0.
        self.tot_accuracy = 0.

    def on_batch_end(self, batch):
        batch_size = self.model.batch_history['batch_size'][-1]
        self.seen += batch_size
        self.tot_loss += self.model.batch_history['loss'][-1] * batch_size
        if self.params['show_accuracy']:
            self.tot_accuracy += self.model.batch_history['accuracy'][-1] * batch_size

    def on_epoch_end(self, epoch):
        val_loss = self.model.epoch_history['loss'][-1]
        val_acc = self.model.epoch_history['accuracy'][-1]
        self.epochs.append(epoch)
        self.losses.append(self.tot_loss / self.seen)
        if self.params['show_accuracy']:
            self.accuracies.append(self.tot_accuracy / self.seen)
        if self.params['do_validation']:
            self.validation_losses.append(val_loss)
            if self.params['show_accuracy']:
                self.validation_accuracies.append(val_acc)

class BaseLogger(Callback):

    def on_train_begin(self):
        self.verbose = self.params['verbose']

    def on_epoch_begin(self, epoch):
        if self.verbose:
            print('Epoch %d' % epoch)
            self.progbar = Progbar(target=self.params['nb_sample'], \
                verbose=self.verbose)
        self.current = 0

    def on_batch_begin(self, batch):
        self.log_values = []

    def on_batch_end(self, batch_index):
        self.current += self.model.batch_history['batch_size'][-1]
        # skip progbar update for the last batch; will be handled by on_epoch_end
        if self.current < self.params['nb_sample']:
            loss = self.model.batch_history['loss'][-1]
            self.log_values.append(('loss', loss))
            if self.params['show_accuracy']:
                accuracy = self.model.batch_history['accuracy'][-1]
                self.log_values.append(('acc.', accuracy))
            if self.verbose:
                self.progbar.update(self.current, self.log_values)

    def on_epoch_end(self, epoch):
        loss = self.model.batch_history['loss'][-1]
        self.log_values.append(('loss', loss))

        if self.params['show_accuracy']:
            accuracy = self.model.batch_history['accuracy'][-1]
            self.log_values.append(('acc.', accuracy))
        if self.params['do_validation']:
            val_loss = self.model.epoch_history['val_loss'][-1]
            self.log_values.append(('val. loss', val_loss))
            if self.params['show_accuracy']:
                val_acc = self.model.epoch_history['val_accuracy'][-1]
                self.log_values.append(('val. acc.', val_acc))
        self.progbar.update(self.current, self.log_values)

@@ -3054,1359 +3054,8 @@
 ss%0A%0A
-class History(Callback):%0A%0A    def on_train_begin(self):%0A        self.epochs = %5B%5D%0A        self.losses = %5B%5D%0A        if self.params%5B'show_accuracy'%5D:%0A            self.accuracies = %5B%5D%0A        if self.params%5B'do_validation'%5D:%0A            self.validation_losses = %5B%5D%0A            if self.params%5B'show_accuracy'%5D:%0A                self.validation_accuracies = %5B%5D%0A%0A    def on_epoch_begin(self, epoch):%0A        self.seen = 0%0A        self.tot_loss = 0.%0A        self.tot_accuracy = 0.%0A%0A    def on_batch_end(self, batch):%0A        batch_size = self.model.batch_history%5B'batch_size'%5D%5B-1%5D%0A        self.seen += batch_size%0A        self.tot_loss += self.model.batch_history%5B'loss'%5D%5B-1%5D * batch_size%0A        if self.params%5B'show_accuracy'%5D:%0A            self.tot_accuracy += self.model.batch_history%5B'accuracy'%5D%5B-1%5D * batch_size%0A%0A    def on_epoch_end(self, epoch):%0A        val_loss = self.model.epoch_history%5B'loss'%5D%5B-1%5D%0A        val_acc = self.model.epoch_history%5B'accuracy'%5D%5B-1%5D%0A        self.epochs.append(epoch)%0A        self.losses.append(self.tot_loss / self.seen)%0A        if self.params%5B'show_accuracy'%5D:%0A            self.accuracies.append(self.tot_accuracy / self.seen)%0A        if self.params%5B'do_validation'%5D:%0A            self.validation_losses.append(val_loss)%0A            if self.params%5B'show_accuracy'%5D:%0A                self.validation_accuracies.append(val_acc)%0A%0A
 clas

Remove History callback (redundant)