Code Duplication    Length = 21-24 lines in 2 locations

deepy/trainers/delayed_trainers.py 1 location

@@ 25-48 (lines=24) @@
22
    Update parameters after N iterations.
23
    """
24
25
    def __init__(self, network, config=None, batch_size=20):
26
        """
27
        Create a SGD trainer.
28
        :type network:
29
        :type config: deepy.conf.TrainerConfig
30
        :return:
31
        """
32
        super(DelayedBatchSGDTrainer, self).__init__(network, config)
33
34
        self.learning_rate = self.config.learning_rate
35
        self.batch_size = batch_size
36
37
        logging.info('compiling %s learning function', self.__class__.__name__)
38
39
        network_updates = list(network.updates) + list(network._learning_updates)
40
        learning_updates = list(self.learning_updates())
41
        update_list = network_updates + learning_updates
42
        logging.info("network updates: %s" % " ".join(map(str, [x[0] for x in network_updates])))
43
        logging.info("learning updates: %s" % " ".join(map(str, [x[0] for x in learning_updates])))
44
45
        self.learning_func = theano.function(
46
            network.inputs,
47
            self.training_variables,
48
            updates=update_list, allow_input_downcast=True, mode=theano.Mode(linker=THEANO_LINKER))
49
50
51
    def learning_updates(self):

deepy/trainers/trainers.py 1 location

@@ 72-92 (lines=21) @@
69
        logging.info("Added %d free parameters for optimization" % len(free_parameters))
70
        return updates
71
72
    def learning_function(self):
73
        """
74
        Get the learning function.
75
        :param func:
76
        :return:
77
        """
78
        network_updates = list(self.network.updates) + list(self.network.training_updates)
79
        learning_updates = list(self._learning_updates())
80
        update_list = network_updates + learning_updates
81
82
        logging.info("network updates: %s" % " ".join(map(str, [x[0] for x in network_updates])))
83
        logging.info("learning updates: %s" % " ".join(map(str, [x[0] for x in learning_updates])))
84
85
        variables = self.network.input_variables + self.network.target_variables
86
        givens = None
87
        return theano.function(
88
            variables,
89
            map(lambda v: theano.Out(v, borrow=True), self.training_variables),
90
            updates=update_list, allow_input_downcast=True,
91
            mode=self.config.get("theano_mode", None),
92
            givens=givens)
93
94
95
class SGDTrainer(GeneralNeuralTrainer):