def AddTrainingOperators(model, softmax, label):
    """Adds training operators to the model."""
    xent = model.LabelCrossEntropy([softmax, label], 'xent')
    # compute the expected loss
    loss = model.AveragedLoss(xent, "loss")
    # track the accuracy of the model
    model_defs.AddAccuracy(model, softmax, label)
    # use the average loss we just computed to add gradient operators to the model
    model.AddGradientOperators([loss])
    # do a simple stochastic gradient descent
    optimizer.build_sgd(
        model,
        base_learning_rate=0.1,
        policy="step",
        stepsize=1,
        gamma=0.999,
    )
# Validation model
val_model = model_helper.ModelHelper(name="val_net",
                                     arg_scope=arg_scope,
                                     init_params=False)
# Uncomment following two lines for GPU
val_model.param_init_net.RunAllOnGPU()
val_model.net.RunAllOnGPU()
data, label = model_defs.AddInput(val_model,
                                  batch_size=validation_images,
                                  db=validation_lmdb,
                                  db_type='lmdb')
softmax = model_defs.AddUpgradedLeNetModel_GPU(val_model, data, num_classes,
                                               image_height, image_width,
                                               image_channels, device_opts)
model_defs.AddAccuracy(val_model, softmax, label)

# Testing model
test_model = model_helper.ModelHelper(name="test_net",
                                      arg_scope=arg_scope,
                                      init_params=False)
# Uncomment following two lines for GPU
test_model.param_init_net.RunAllOnGPU()
test_model.net.RunAllOnGPU()
data, label = model_defs.AddInput(test_model,
                                  batch_size=testing_images,
                                  db=testing_lmdb,
                                  db_type='lmdb')
softmax = model_defs.AddUpgradedLeNetModel_GPU(test_model, data, num_classes,
                                               image_height, image_width,
                                               image_channels, device_opts)