How to use the cntk.Trainer function in cntk

To help you get started, we’ve selected a few cntk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / CNTK / Examples / Text / CharacterLM / char_rnn.py View on Github external
# and apply it to the input sequence    
    z = model(input_sequence)

    # setup the criterions (loss and metric)
    ce = cross_entropy_with_softmax(z, label_sequence)
    errs = classification_error(z, label_sequence)

    # Instantiate the trainer object to drive the model training
    lr_per_sample = learning_rate_schedule(0.001, UnitType.sample)
    momentum_time_constant = momentum_as_time_constant_schedule(1100)
    clipping_threshold_per_sample = 5.0
    gradient_clipping_with_truncation = True
    learner = momentum_sgd(z.parameters, lr_per_sample, momentum_time_constant,
                           gradient_clipping_threshold_per_sample=clipping_threshold_per_sample,
                           gradient_clipping_with_truncation=gradient_clipping_with_truncation)
    trainer = Trainer(z, ce, errs, learner)

    sample_freq = 1000
    epochs = 50
    minibatches_per_epoch = int((data_size / minibatch_size))
    minibatches = min(epochs * minibatches_per_epoch, max_num_minibatches)

    # print out some useful training information
    log_number_of_parameters(z) ; print()
    progress_printer = ProgressPrinter(freq=100, tag='Training')    
    
    e = 0
    p = 0
    for i in range(0, minibatches):

        if p + minibatch_size+1 >= data_size:
            p = 0
github microsoft / CNTK / bindings / python / examples / LanguageUnderstanding / LanguageUnderstanding.py View on Github external
# apply model to input
    z = model(query)

    # loss and metric
    ce = cross_entropy_with_softmax(z, slot_labels)
    pe = classification_error      (z, slot_labels)

    # define mapping from reader streams to network inputs
    input_map = {
        query       : reader.streams.query,
        slot_labels : reader.streams.slot_labels
    }

    # process minibatches and perform evaluation
    dummy_learner = adam_sgd(z.parameters, lr_per_sample=1, momentum_time_constant=0, low_memory=True) # BUGBUG: should not be needed
    evaluator = Trainer(z, ce, pe, [dummy_learner])
    progress_printer = ProgressPrinter(freq=100, first=10, tag='Evaluation') # more detailed logging
    #progress_printer = ProgressPrinter(tag='Evaluation')

    while True:
        minibatch_size = 1000
        data = reader.next_minibatch(minibatch_size, input_map=input_map) # fetch minibatch
        if not data:                                                      # until we hit the end
            break
        metric = evaluator.test_minibatch(data)                           # evaluate minibatch
        progress_printer.update(0, data[slot_labels].num_samples, metric) # log progress
    loss, metric, actual_samples = progress_printer.epoch_summary(with_metric=True)

    return loss, metric
github microsoft / CNTK / bindings / python / doc / simplernn.py View on Github external
rel_path = ("../../../Tests/EndToEndTests/Text/" +
                "SequenceClassification/Data/Train.ctf")
    path = os.path.join(os.path.dirname(os.path.abspath(__file__)), rel_path)

    reader = create_reader(path, True, input_dim, num_output_classes)

    input_map = {
            features: reader.streams.features,
            label:    reader.streams.labels
    }

    lr_per_sample = learning_parameter_schedule_per_sample(0.0005)
    # Instantiate the trainer object to drive the model training
    progress_printer = ProgressPrinter(0)
    trainer = Trainer(classifier_output, (ce, pe),
                      sgd(classifier_output.parameters, lr=lr_per_sample),
                      progress_printer)

    # Get minibatches of sequences to train with and perform model training
    minibatch_size = 200

    for i in range(255):
        mb = reader.next_minibatch(minibatch_size, input_map=input_map)
        trainer.train_minibatch(mb)

    evaluation_average = float(trainer.previous_minibatch_evaluation_average)
    loss_average = float(trainer.previous_minibatch_loss_average)
    return evaluation_average, loss_average
github microsoft / CNTK / bindings / python / examples / Autoencoder / encode_decode.py View on Github external
for dim in next_layers:
    W = np.random.normal(0, 1.0, size=(prevDim, dim)).astype(np.float32)
    param_W = C.parameter(init=W)
    b = np.random.normal(0, 1.0, size=[dim]).astype(np.float32)
    param_b = C.parameter(init=b)
    output = C.sigmoid(C.times(x, param_W) + param_b)
    x = output
    prevDim = dim
decoded = x

if __name__=='__main__':
    lr = 0.001
    se = C.sqrt(C.reduce_mean(C.square(Input-decoded), axis=0))
    pe = C.sqrt(C.reduce_mean(C.square(Input-decoded), axis=0))

    trainer = C.Trainer(decoded, se, pe, [C.sgd(decoded.parameters, lr)])
    
    # Get minibatches of training data and perform model training
    minibatch_size = 1
    num_samples_per_sweep = 1000
    num_sweeps_to_train_with = 5
    num_minibatches_to_train = (num_samples_per_sweep * num_sweeps_to_train_with) / minibatch_size
    training_progress_output_freq = 200

    for i in range(0, int(num_minibatches_to_train)):
        data = np.random.normal(0.6, 0.2, size=(input_dim)).astype(np.float32)
        f = trainer.train_minibatch(arguments={Input : data})
        
        if i % training_progress_output_freq == 0:
            print(i, "Input: ", data)
            print(i, "Output: ", trainer.model.eval(arguments={Input : data}))
            print_training_progress(trainer, i, training_progress_output_freq)
github astorfi / CNTK-World / codes / Python / p03-neural-networks / 3-autoencoders / autoencoders.py View on Github external
loss = -(target_normalized * C.log(net) + (1 - target_normalized) * C.log(1 - net))
label_error  = C.classification_error(net, target_normalized)

# Instantiate the trainer object to drive the model training
lr_per_sample = [0.0001]
learning_rate_schedule = C.learning_rate_schedule(lr_per_sample, C.UnitType.sample, epoch_size=int(num_training_samples/2.0))

# Momentum
momentum_as_time_constant = C.momentum_as_time_constant_schedule(200)

# Define the learner
learner = C.fsadagrad(net.parameters, lr=learning_rate_schedule, momentum=momentum_as_time_constant)

# Instantiate the trainer
progress_printer = C.logging.ProgressPrinter(0)
train_op = C.Trainer(net, (loss, label_error), learner, progress_printer)


###############################
########## Training ###########
###############################

# Plot data dictionary.
plotdata = {"iteration":[], "loss":[], "error":[]}

# Initialize the parameters for the trainer
num_iterations = (num_training_samples * num_epochs) / batch_size

# Training loop.
for iter in range(0, int(num_iterations)):

    # Read a mini batch from the training data file
github Master76 / snake-master / src / brain.py View on Github external
def __get_trainer_loss(self, model, action_count):
        q_target = C.sequence.input_variable(action_count, np.float32)

        # loss='mse'
        loss = C.reduce_mean(C.square(model - q_target), axis=0)
        meas = C.reduce_mean(C.square(model - q_target), axis=0)

        # optimizer
        lr_schedule = C.learning_rate_schedule(LEARNING_RATE, C.UnitType.minibatch)
        learner = C.sgd(
            model.parameters,
            lr_schedule,
            gradient_clipping_threshold_per_sample=10)
        trainer = C.Trainer(model, (loss, meas), learner)

        return trainer, loss
github microsoft / CNTK / bindings / python / examples / distributed / SimpleMNIST_parallel.py View on Github external
dist_trainer = distributed.data_parallel_distributed_trainer(communicator, False)
    
    print("Training on device type:{} id:{}".format('gpu' if default().type() else 'cpu', default().id()))
        
    # Get minibatches of images to train with and perform model training
    minibatch_size = 64
    epoch_size = 60000
    num_epochs = 10

    num_minibatches_to_train = int(epoch_size / minibatch_size) * num_epochs
    num_minibatches_to_train = int(num_minibatches_to_train / num_workers) * num_workers
    
    lr_per_sample = [0.01]*5+[0.005]
    lr_schedule = learning_rate_schedule(lr_per_sample, units=epoch_size)

    trainer = Trainer(netout, ce, pe, [sgd(netout.parameters,
        lr=lr_schedule)], distributed_trainer=dist_trainer)

    training_progress_output_freq = 500

    if debug_output:
        training_progress_output_freq = training_progress_output_freq/4

    for i in range(0, num_minibatches_to_train):
        mb = mb_source.next_minibatch(minibatch_size)

        # Specify the mapping of input variables in the model to actual
        # minibatch data to be trained with
        arguments = {input: mb[features_si],
                     label: mb[labels_si]}
        trainer.train_minibatch(arguments)
github microsoft / samples-for-ai / examples / cntk / python / MNIST / ConvNet_MNIST.py View on Github external
ce = C.losses.cross_entropy_with_softmax(z, label_var)
    pe = C.metrics.classification_error(z, label_var)

    reader_train = create_reader(os.path.join(data_path, 'Train-28x28_cntk_text.txt'), True, input_dim, num_output_classes)

    # Set learning parameters
    lr_per_sample    = [0.001]*10 + [0.0005]*10 + [0.0001]
    lr_schedule      = C.learning_rate_schedule(lr_per_sample, C.learners.UnitType.sample, epoch_size)
    mm_time_constant = [0]*5 + [1024]
    mm_schedule      = C.learners.momentum_as_time_constant_schedule(mm_time_constant, epoch_size)

    # Instantiate the trainer object to drive the model training
    learner = C.learners.momentum_sgd(z.parameters, lr_schedule, mm_schedule)
    progress_printer = C.logging.ProgressPrinter(tag='Training', num_epochs=max_epochs)
    trainer = C.Trainer(z, (ce, pe), learner, progress_printer)

    # define mapping from reader streams to network inputs
    input_map = {
        input_var : reader_train.streams.features,
        label_var : reader_train.streams.labels
    }
    C.logging.log_number_of_parameters(z) ; print()

    # Get minibatches of images to train with and perform model training
    for epoch in range(max_epochs):       # loop over epochs
        sample_count = 0
        while sample_count < epoch_size:  # loop over minibatches in the epoch
            data = reader_train.next_minibatch(min(minibatch_size, epoch_size - sample_count), input_map=input_map) # fetch minibatch.
            trainer.train_minibatch(data)                                   # update model with it
            sample_count += data[label_var].num_samples                     # count samples processed so far
github microsoft / CNTK / Examples / Image / Classification / ConvNet / Python / ConvNet_CIFAR10_DataAug_Distributed.py View on Github external
# Create learner
    if block_size != None and num_quantization_bits != 32:
        raise RuntimeError("Block momentum cannot be used with quantization, please remove quantized_bits option.")

    local_learner = cntk.learners.momentum_sgd(network['output'].parameters,
                                              lr_schedule, mm_schedule,
                                              l2_regularization_weight=l2_reg_weight)

    if block_size != None:
        parameter_learner = cntk.train.distributed.block_momentum_distributed_learner(local_learner, block_size=block_size)
    else:
        parameter_learner = cntk.train.distributed.data_parallel_distributed_learner(local_learner, num_quantization_bits=num_quantization_bits, distributed_after=warm_up)

    # Create trainer
    return cntk.Trainer(network['output'], (network['ce'], network['pe']), parameter_learner, progress_writers)
github Azure-Samples / MachineLearningSamples-AerialImageClassification / Code / 01_Data_Acquisition_and_Understanding / 02_Batch_AI_Training_Provisioning / retrain_model_distributed.py View on Github external
num_workers = distributed.Communicator.num_workers()
	num_minibatches = int(np.ceil(epoch_size / mb_size))

	progress_writers = [cntk.logging.progress_print.ProgressPrinter(
		tag='Training',
		num_epochs=num_epochs,
		freq=num_minibatches,
		rank=my_rank)]
	learner = cntk.learners.fsadagrad(parameters=model.parameters,
									  lr=lr_schedule,
									  momentum=mm_schedule,
									  l2_regularization_weight=l2_reg_weight)
	if num_workers > 1:
		parameter_learner = distributed.data_parallel_distributed_learner(
			learner, num_quantization_bits=32)
		trainer = cntk.Trainer(model, (ce, pe), parameter_learner,
							   progress_writers)
	else:
		trainer = cntk.Trainer(model, (ce, pe), learner, progress_writers)

	# Print summary lines to stdout and perform training
	if my_rank == 0:
		print('Retraining model for {} epochs.'.format(num_epochs))
		print('Found {} workers'.format(num_workers))
		print('Printing progress every {} minibatches'.format(num_minibatches))
		cntk.logging.progress_print.log_number_of_parameters(model)

	training_session(
		trainer=trainer,
		max_samples=num_epochs * epoch_size,
		mb_source=minibatch_source, 
		mb_size=mb_size,