def train(model, train_fn, val_fn, datagen, save_dir, epochs=10, mb_size=16,
          do_sortagrad=True):
    """ Main training routine for speech-models
    Params:
        model (keras.model): Constructed keras model
        train_fn (theano.function): A theano function that takes in acoustic
            inputs and updates the model
        val_fn (theano.function): A theano function that calculates the cost
            over a validation set
        datagen (DataGenerator)
        save_dir (str): Path where model and costs are saved
        epochs (int): Total epochs to continue training
        mb_size (int): Size of each minibatch
        do_sortagrad (bool): If true, we sort utterances by their length in the
            first epoch
    """
    train_costs, val_costs = [], []
    iters = 0
    for e in range(epochs):
        if do_sortagrad:
            shuffle = e != 0
            sortagrad = e == 0
        else:
            shuffle = True
            sortagrad = False
        for i, batch in \
                enumerate(datagen.iterate_train(mb_size, shuffle=shuffle,
                                                sort_by_duration=sortagrad)):
            inputs = batch['x']
            labels = batch['y']
            input_lengths = batch['input_lengths']
            label_lengths = batch['label_lengths']
            # Due to convolution, the number of timesteps of the output
            # is different from the input length. Calculate the resulting
            # timesteps
            output_lengths = [model.conv_output_length(l)
                              for l in input_lengths]
            _, ctc_cost = train_fn([inputs, output_lengths, labels,
                                    label_lengths, True])
            train_costs.append(ctc_cost)
            if i % 10 == 0:
                logger.info("Epoch: {}, Iteration: {}, Loss: {}"
                            .format(e, i, ctc_cost, input_lengths))
            iters += 1
            if iters % 500 == 0:
                val_cost = validation(model, val_fn, datagen, mb_size)
                val_costs.append(val_cost)
                save_model(save_dir, model, train_costs, val_costs, iters)
        if iters % 500 != 0:
            # End of an epoch. Check validation cost and save costs
            val_cost = validation(model, val_fn, datagen, mb_size)
            val_costs.append(val_cost)
            save_model(save_dir, model, train_costs, val_costs, iters)
 Main training routine for speech-models
    Params:
        model (keras.model): Constructed keras model
        train_fn (theano.function): A theano function that takes in acoustic
            inputs and updates the model
        val_fn (theano.function): A theano function that calculates the cost
            over a validation set
        datagen (DataGenerator)
        save_dir (str): Path where model and costs are saved
        epochs (int): Total epochs to continue training
        mb_size (int): Size of each minibatch
        do_sortagrad (bool): If true, we sort utterances by their length in the
            first epoch
    
train.py