def fit(self,
            x,
            y=None,
            val_x=None,
            val_y=None,
            val_freq=10,
            batch_size=128,
            num_epoch=10,
            momentum=0.9,
            learn_rate=0.01,
            learn_rate_decay=0.05,
            chill_out_iters=10,
            weight_decay=.0005,
            verbose=False):
        """
        Train the neural network via mini-batch gradient descent.

        Parameters
        ----------
        x : numpy array, training examples; shape should be (NUMBER_OF_EXAMPLES,) + self.input_shape
        y : numpy array, training labels, shape should be (NUMBER_OF_EXAMPLES, shape of labels)
        val_x : validation examples, similar shape as x
        val_y : validation labels, similar shape as y
        val_freq : validation will be performed every val_freq iterations
        batch_size : number of examples to use in each batch
        num_epoch : number of epochs to train for (maximum, may terminate earlier)
        learn_rate : initial learning rate, will decay as learning proceeds
        learn_rate_decay : at each iteration i the learning rate will be
            learn_rate/(i*learn_rate_decay+1)
        chill_out_iters : if there is no improvement in validation error after this many iterations
            of validation, the learning rate will be cut in half and the network will go back to
            the set of parameters that achieved the lower cost so far
        weight_decay : amount of weight decay to apply
        verbose : whether to print debug messages during training or not

        """

        if verbose:
            print '='*80
            print 'training net on %d samples' % x.shape[0]
            if val_x is not None:
                print 'using %d validation samples' % val_x.shape[0]
            print '='*80

        min_cost = 1e8
        velocity = np.zeros(self.num_params(), dtype='float32')
        best_params = self.get_params()
        stop = False
        no_improvement_iters = 0
        num_train = x.shape[0]
        num_batch = NNet.get_num_batch(num_train, batch_size)
        init_learn_rate = learn_rate

        start_time = time()
        for epoch in xrange(self.num_epoch, self.num_epoch + num_epoch):
            inds = self._rand_state.permutation(x.shape[0])
            if stop: 
                break
            for batch in xrange(num_batch):
                batch_x, batch_y = self.get_batch(
                        x, y=y, batch_size=batch_size, inds=inds, batch_ind=batch)
                assert batch_x.shape[0] > 0
                param_grad = self.param_grad(batch_x, y=batch_y)
                params = self.get_params()
                learn_rate = init_learn_rate/((epoch*num_batch + batch)*learn_rate_decay+1)
                velocity = (
                        momentum*velocity - 
                        learn_rate*param_grad - 
                        learn_rate*weight_decay*params)
                self.set_params(params + velocity)

                # check validation error every once in a while
                if (batch%val_freq == 0 and batch>0) or batch == num_batch-1:
                    if val_x is None:
                        val_x, val_y = batch_x, batch_y
                    val_acts = self.forward_final(val_x, batch_size=batch_size)
                    cost = self.cost(val_x, val_y, final_acts=val_acts)
                    # child classes don't necessarily have a concept of "accuracy" and might not
                    # implement the accuracy method
                    try:
                        acc = self.accuracy(val_acts, val_y)
                    except NotImplemented:
                        acc = np.nan

                    cost_diff = cost - min_cost

                    # if there has been significant regression in cost, chill out
                    if ((cost_diff > 0 and cost_diff/min_cost > 1) or 
                        no_improvement_iters>chill_out_iters):
                        self.set_params(best_params)
                        no_improvement_iters = 0
                        init_learn_rate /= 2
                        velocity = np.zeros_like(velocity, dtype='float32')
                        print 'cost was %.3e, chilling out...' % cost
                        cost = min_cost
                    elif cost < min_cost:
                        best_params = self.get_params()
                        min_cost = cost
                        no_improvement_iters = 0
                    else:
                        no_improvement_iters += 1

                    if verbose:
                        print 'epoch %03d, batch=%04d/%04d' % (epoch, batch+1, num_batch)
                        print 'cost=%.3e, min_cost=%.3e, acc=%.2f' % (cost, min_cost, acc)
                        print 'learn_rate=%.3e, velocity L1 norm %f' % (learn_rate, 
                                                                        np.abs(velocity).sum(0))
                        print '-'*80

        self.num_epoch = epoch
        end_time = time()
        print 'training complete [%.2f min]' % ((end_time-start_time)/60)
dataset/ETHPy150Open bbabenko/simple_convnet/simple_convnet/convnet.py/NNet.fit/original