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):

        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)
Train the neural network via mini-batch gradient descent.
dataset/ETHPy150Open bbabenko/simple_convnet/simple_convnet/convnet.py/NNet.fit