Skip to content

Commit

Permalink
changed Trainer() signature from Trainer(model, loss, metric, ...) to…
Browse files Browse the repository at this point in the history
… Trainer(model, (loss, metric), ...) to prep for Layers lib where (loss, metric) is the criterion function;

fixed an existing bug in a doc example
  • Loading branch information
frankseide committed Feb 9, 2017
1 parent 81df1d8 commit 99c25c0
Show file tree
Hide file tree
Showing 44 changed files with 65 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def create_trainer(network, epoch_size, num_quantization_bits):
distributed_after=0)

# Create trainer
return cntk.Trainer(network['output'], network['ce'], network['pe'], parameter_learner)
return cntk.Trainer(network['output'], (network['ce'], network['pe']), parameter_learner)

# Train and test
def train_and_test(network, trainer, train_source, test_source, progress_printer, minibatch_size, epoch_size, restore):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def convnetlrn_cifar10_dataaug(reader_train, reader_test, epoch_size=50000, max_
learner = cntk.learner.momentum_sgd(z.parameters, lr_schedule, mm_schedule,
unit_gain = True,
l2_regularization_weight = l2_reg_weight)
trainer = cntk.Trainer(z, ce, pe, learner)
trainer = cntk.Trainer(z, (ce, pe), learner)

# define mapping from reader streams to network inputs
input_map = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def convnet_cifar10(debug_output=False):
# Instantiate the trainer object to drive the model training
learner = cntk.learner.momentum_sgd(z.parameters, lr_schedule, mm_schedule,
l2_regularization_weight = l2_reg_weight)
trainer = cntk.Trainer(z, ce, pe, learner)
trainer = cntk.Trainer(z, (ce, pe), learner)

# define mapping from reader streams to network inputs
input_map = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def convnet_cifar10_dataaug(reader_train, reader_test, epoch_size = 50000, max_e
# trainer object
learner = cntk.learner.momentum_sgd(z.parameters, lr_schedule, mm_schedule,
l2_regularization_weight = l2_reg_weight)
trainer = cntk.Trainer(z, ce, pe, learner)
trainer = cntk.Trainer(z, (ce, pe), learner)

# define mapping from reader streams to network inputs
input_map = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def create_trainer(network, epoch_size, num_quantization_bits, block_size, warm_
parameter_learner = cntk.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)
return cntk.Trainer(network['output'], (network['ce'], network['pe']), parameter_learner)

# Train and test
def train_and_test(network, trainer, train_source, test_source, progress_printer, minibatch_size, epoch_size, restore):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def convnet_mnist(debug_output=False):

# Instantiate the trainer object to drive the model training
learner = cntk.learner.momentum_sgd(z.parameters, lr_schedule, mm_schedule)
trainer = cntk.Trainer(z, ce, pe, learner)
trainer = cntk.Trainer(z, (ce, pe), learner)

# define mapping from reader streams to network inputs
input_map = {
Expand Down
2 changes: 1 addition & 1 deletion Examples/Image/Classification/MLP/Python/SimpleMNIST.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def simple_mnist(debug_output=False):

lr_per_minibatch=learning_rate_schedule(0.2, UnitType.minibatch)
# Instantiate the trainer object to drive the model training
trainer = Trainer(z, ce, pe, sgd(z.parameters, lr=lr_per_minibatch))
trainer = Trainer(z, (ce, pe), sgd(z.parameters, lr=lr_per_minibatch))

# Get minibatches of images to train with and perform model training
minibatch_size = 64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def train_and_evaluate(reader_train, reader_test, network_name, epoch_size, max_
# trainer object
learner = momentum_sgd(z.parameters, lr_schedule, mm_schedule,
l2_regularization_weight = l2_reg_weight)
trainer = Trainer(z, ce, pe, learner)
trainer = Trainer(z, (ce, pe), learner)

# define mapping from reader streams to network inputs
input_map = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def create_trainer(network, minibatch_size, epoch_size, num_quantization_bits, b
else:
learner = data_parallel_distributed_learner(local_learner, num_quantization_bits=num_quantization_bits, distributed_after=warm_up)

return Trainer(network['output'], network['ce'], network['pe'], learner)
return Trainer(network['output'], (network['ce'], network['pe']), learner)

# Train and test
def train_and_test(network, trainer, train_source, test_source, progress_printer, minibatch_size, epoch_size, profiler_dir=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def create_trainer(network, epoch_size, num_quantization_bits):
distributed_after=0)

# Create trainer
return cntk.Trainer(network['output'], network['ce'], network['pe'], parameter_learner)
return cntk.Trainer(network['output'], (network['ce'], network['pe']), parameter_learner)

# Train and test
def train_and_test(network, trainer, train_source, test_source, progress_printer, minibatch_size, epoch_size, restore):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def create_trainer(network, epoch_size, num_quantization_bits):
distributed_after=0)

# Create trainer
return cntk.Trainer(network['output'], network['ce'], network['pe'], parameter_learner)
return cntk.Trainer(network['output'], (network['ce'], network['pe']), parameter_learner)

# Train and test
def train_and_test(network, trainer, train_source, test_source, progress_printer, minibatch_size, epoch_size, restore):
Expand Down
2 changes: 1 addition & 1 deletion Examples/Image/Detection/FastRCNN/A2_RunCntk_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def train_fast_rcnn(debug_output=False):

# Instantiate the trainer object
learner = momentum_sgd(frcn_output.parameters, lr_schedule, mm_schedule, l2_regularization_weight=l2_reg_weight)
trainer = Trainer(frcn_output, ce, pe, learner)
trainer = Trainer(frcn_output, (ce, pe), learner)

# Get minibatches of images and perform model training
print("Training Fast R-CNN model for %s epochs." % max_epochs)
Expand Down
2 changes: 1 addition & 1 deletion Examples/Image/GettingStarted/07_Deconvolution_PY.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def deconv_mnist(max_epochs=3):

# Instantiate the trainer object to drive the model training
learner = cntk.learner.momentum_sgd(z.parameters, lr_schedule, mm_schedule, unit_gain=True)
trainer = cntk.Trainer(z, rmse_loss, rmse_eval, learner)
trainer = cntk.Trainer(z, (rmse_loss, rmse_eval), learner)

# define mapping from reader streams to network inputs
input_map = {
Expand Down
2 changes: 1 addition & 1 deletion Examples/Image/TransferLearning/TransferLearning.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def train_model(base_model_file, feature_node_name, last_hidden_node_name,
lr_schedule = learning_rate_schedule(lr_per_mb, unit=UnitType.minibatch)
mm_schedule = momentum_schedule(momentum_per_mb)
learner = momentum_sgd(tl_model.parameters, lr_schedule, mm_schedule, l2_regularization_weight=l2_reg_weight)
trainer = Trainer(tl_model, ce, pe, learner)
trainer = Trainer(tl_model, (ce, pe), learner)

# Get minibatches of images and perform model training
print("Training transfer learning model for {0} epochs (epoch_size = {1}).".format(num_epochs, epoch_size))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def train(reader, model, max_epochs):
low_memory=True,
gradient_clipping_threshold_per_sample=15, gradient_clipping_with_truncation=True)

trainer = cntk.Trainer(z, ce, pe, [learner])
trainer = cntk.Trainer(z, (ce, pe), [learner])

# define mapping from reader streams to network inputs
input_map = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def train_sequence_classifier(debug_output=False):

lr_per_sample = learning_rate_schedule(0.0005, UnitType.sample)
# Instantiate the trainer object to drive the model training
trainer = Trainer(classifier_output, ce, pe,
trainer = Trainer(classifier_output, (ce, pe),
sgd(classifier_output.parameters, lr=lr_per_sample))

# Get minibatches of sequences to train with and perform model training
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def sequence_to_sequence_translator(debug_output=False, run_test=False):
lr_per_minibatch, 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)
trainer = Trainer(z, (ce, errs), learner)

# setup data
train_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "Data", "cmudict-0.7b.train-dev-20-21.ctf")
Expand Down Expand Up @@ -240,7 +240,7 @@ def sequence_to_sequence_translator(debug_output=False, run_test=False):
label_sequence = sequence.slice(find_arg_by_name('raw_labels',z), 1, 0)
ce = cross_entropy_with_softmax(z, label_sequence)
errs = classification_error(z, label_sequence)
trainer = Trainer(z, ce, errs, [momentum_sgd(
trainer = Trainer(z, (ce, errs), [momentum_sgd(
z.parameters, lr_per_minibatch, momentum_time_constant, True,
clipping_threshold_per_sample, gradient_clipping_with_truncation)])

Expand Down
2 changes: 1 addition & 1 deletion Examples/Tensorboard/LanguageUnderstanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def train(reader, model, max_epochs):
low_memory=True,
gradient_clipping_threshold_per_sample=15, gradient_clipping_with_truncation=True)

trainer = Trainer(z, ce, pe, [learner])
trainer = Trainer(z, (ce, pe), [learner])

# define mapping from reader streams to network inputs
input_map = {
Expand Down
2 changes: 1 addition & 1 deletion Examples/Tensorboard/SimpleMNIST.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def simple_mnist():

lr_per_minibatch = learning_rate_schedule(0.2, UnitType.minibatch)
# Instantiate the trainer object to drive the model training
trainer = Trainer(netout, ce, pe, sgd(netout.parameters, lr=lr_per_minibatch))
trainer = Trainer(netout, (ce, pe), sgd(netout.parameters, lr=lr_per_minibatch))

# Instantiate a ProgressPrinter.
logdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "mnist_log")
Expand Down
2 changes: 1 addition & 1 deletion Examples/Text/CharacterLM/char_rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def train_lm(training_file, max_num_minibatches):
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)
trainer = Trainer(z, (ce, errs), learner)

sample_freq = 1000
epochs = 50
Expand Down
2 changes: 1 addition & 1 deletion Examples/Video/GettingStarted/Python/Conv3D_UCF11.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def conv3d_ucf11(train_reader, test_reader, max_epochs=30):

# Instantiate the trainer object to drive the model training
learner = momentum_sgd(z.parameters, lr_schedule, mm_schedule, True)
trainer = Trainer(z, ce, pe, learner)
trainer = Trainer(z, (ce, pe), learner)

log_number_of_parameters(z) ; print()
progress_printer = ProgressPrinter(tag='Training')
Expand Down
2 changes: 1 addition & 1 deletion Tutorials/CNTK_101_LogisticRegression.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@
"learning_rate = 0.5\n",
"lr_schedule = learning_rate_schedule(learning_rate, UnitType.minibatch) \n",
"learner = sgd(z.parameters, lr_schedule)\n",
"trainer = Trainer(z, loss, eval_error, [learner])"
"trainer = Trainer(z, (loss, eval_error), [learner])"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion Tutorials/CNTK_102_FeedForward.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@
"learning_rate = 0.5\n",
"lr_schedule = learning_rate_schedule(learning_rate, UnitType.minibatch) \n",
"learner = C.learner.sgd(z.parameters, lr_schedule)\n",
"trainer = Trainer(z, loss, eval_error, [learner])"
"trainer = Trainer(z, (loss, eval_error), [learner])"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion Tutorials/CNTK_103B_MNIST_FeedForwardNetwork.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@
"learning_rate = 0.2\n",
"lr_schedule = learning_rate_schedule(learning_rate, UnitType.minibatch)\n",
"learner = sgd(z.parameters, lr_schedule)\n",
"trainer = Trainer(z, loss, label_error, [learner])"
"trainer = Trainer(z, (loss, label_error), [learner])"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@
"loss = C.cross_entropy_with_softmax(z, label)\n",
"label_error = C.classification_error(z, label)\n",
"lr_per_minibatch = learning_rate_schedule(0.125,UnitType.minibatch)\n",
"trainer = cntk.Trainer(z, loss, label_error, [sgd(z.parameters, lr=lr_per_minibatch)])"
"trainer = cntk.Trainer(z, (loss, label_error), [sgd(z.parameters, lr=lr_per_minibatch)])"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@
" lr=lr_schedule, momentum=momentum_as_time_constant) \n",
" \n",
" # Instantiate the trainer\n",
" trainer = Trainer(model, loss, label_error, learner)\n",
" trainer = Trainer(model, (loss, label_error), learner)\n",
" \n",
" # Map the data streams to the input and labels.\n",
" # Note: for autoencoders input == label\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@
" lr = lr_schedule, \n",
" momentum = momentum_time_constant, \n",
" unit_gain = True)\n",
"trainer = C.Trainer(z, loss, error, [learner])"
"trainer = C.Trainer(z, (loss, error), [learner])"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion Tutorials/CNTK_201B_CIFAR-10_ImageHandsOn.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@
" learner = momentum_sgd(z.parameters, \n",
" lr = lr_per_minibatch, momentum = momentum_time_constant, \n",
" l2_regularization_weight=l2_reg_weight)\n",
" trainer = Trainer(z, ce, pe, [learner])\n",
" trainer = Trainer(z, (ce, pe), [learner])\n",
"\n",
" # define mapping from reader streams to network inputs\n",
" input_map = {\n",
Expand Down
4 changes: 2 additions & 2 deletions Tutorials/CNTK_202_Language_Understanding.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@
" gradient_clipping_threshold_per_sample=15, gradient_clipping_with_truncation=True)\n",
"\n",
" # trainer\n",
" trainer = Trainer(model, criterion.outputs[0], criterion.outputs[1], learner)\n",
" trainer = Trainer(model, criterion, learner)\n",
"\n",
" # process minibatches and perform model training\n",
" log_number_of_parameters(model)\n",
Expand Down Expand Up @@ -561,7 +561,7 @@
" momentum_as_time_constant = momentum_as_time_constant_schedule(0)\n",
" dummy_learner = adam_sgd(criterion.parameters, \n",
" lr=lr_schedule, momentum=momentum_as_time_constant, low_memory=True)\n",
" evaluator = Trainer(model, criterion.outputs[0], criterion.outputs[1], dummy_learner)\n",
" evaluator = Trainer(model, criterion, dummy_learner)\n",
" progress_printer = ProgressPrinter(tag='Evaluation')\n",
"\n",
" while True:\n",
Expand Down
2 changes: 1 addition & 1 deletion Tutorials/CNTK_203_Reinforcement_Learning_Basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@
" # optimizer=opt\n",
" lr_schedule = learning_rate_schedule(lr, UnitType.minibatch)\n",
" learner = sgd(model.parameters, lr_schedule, gradient_clipping_threshold_per_sample=10)\n",
" trainer = Trainer(model, loss, meas, learner)\n",
" trainer = Trainer(model, (loss, meas), learner)\n",
"\n",
" # CNTK: return trainer and loss as well\n",
" return model, trainer, loss\n",
Expand Down
4 changes: 2 additions & 2 deletions Tutorials/CNTK_204_Sequence_To_Sequence.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@
" lr_per_sample, momentum_time_constant,\n",
" gradient_clipping_threshold_per_sample=clipping_threshold_per_sample,\n",
" gradient_clipping_with_truncation=gradient_clipping_with_truncation)\n",
"trainer = Trainer(model, ce, errs, learner)"
"trainer = Trainer(model, (ce, errs), learner)"
]
},
{
Expand Down Expand Up @@ -1029,7 +1029,7 @@
" lr_per_sample, momentum_time_constant,\n",
" gradient_clipping_threshold_per_sample=clipping_threshold_per_sample,\n",
" gradient_clipping_with_truncation=gradient_clipping_with_truncation)\n",
" trainer = Trainer(model, ce, errs, learner)\n",
" trainer = Trainer(model, (ce, errs), learner)\n",
"\n",
" # Get minibatches of sequences to train with and perform model training\n",
" i = 0\n",
Expand Down
14 changes: 6 additions & 8 deletions Tutorials/CNTK_206_Basic_GAN.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -417,16 +417,14 @@
"\n",
" # Instantiate the trainers\n",
" G_trainer = Trainer(\n",
" model = X_fake,\n",
" loss_function = G_loss,\n",
" eval_function = None,\n",
" parameter_learners = G_learner\n",
" X_fake,\n",
" (G_loss, None),\n",
" G_learner\n",
" )\n",
" D_trainer = Trainer(\n",
" model = D_real,\n",
" loss_function = D_loss,\n",
" eval_function = None,\n",
" parameter_learners = D_learner\n",
" D_real,\n",
" (D_loss, None,\)n",
" D_learner\n",
" )\n",
"\n",
" return X_real, X_fake, Z, G_trainer, D_trainer"
Expand Down
2 changes: 1 addition & 1 deletion Tutorials/CNTK_207_Training_with_Sampled_Softmax.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@
" learning_rate_schedule = C.learning_rate_schedule(Param.learning_rate, C.UnitType.sample)\n",
" momentum_schedule = C.momentum_as_time_constant_schedule(Param.momentum_time_constant)\n",
" learner = C.momentum_sgd(z.parameters, learning_rate_schedule, momentum_schedule, True)\n",
" trainer = C.Trainer(z, cross_entropy, errs, learner)\n",
" trainer = C.Trainer(z, (cross_entropy, errs), learner)\n",
"\n",
" minbatch = 0\n",
" average_cross_entropy = compute_average_cross_entropy(z)\n",
Expand Down
2 changes: 1 addition & 1 deletion Tutorials/NumpyInterop/FeedForwardNet.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def ffnet():

lr_per_minibatch=learning_rate_schedule(0.5, UnitType.minibatch)
# Instantiate the trainer object to drive the model training
trainer = Trainer(netout, ce, pe, sgd(netout.parameters, lr=lr_per_minibatch))
trainer = Trainer(netout, (ce, pe), sgd(netout.parameters, lr=lr_per_minibatch))

# Get minibatches of training data and perform model training
minibatch_size = 25
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/cntk/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ def times(left, right, output_rank=1, infer_input_rank_to_map=-1, name=''):
array([[ 17.],
[ 39.]], dtype=float32)
>>> C.times(1.*np.reshape(np.arange(8), (2.,2.,2.)),1.*np.reshape(np.arange(8), (2.,2.,2.)), output_rank=1).eval()
>>> C.times(1.*np.reshape(np.arange(8), (2,2,2)),1.*np.reshape(np.arange(8), (2,2,2)), output_rank=1).eval()
array([[ 28., 34.],
[ 76., 98.]])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def train(nonlinearity, num_hidden_layers, device_id):
eval_error = classification_error(z, label)

learner = sgd(z.parameters, lr_schedule)
trainer = Trainer(z, loss, eval_error, [learner])
trainer = Trainer(z, (loss, eval_error), [learner])


minibatch_size = 25
Expand Down
6 changes: 3 additions & 3 deletions bindings/python/cntk/ops/tests/userfunction_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_ext_train():

momentum_time_constant = momentum_as_time_constant_schedule(1100)
lr_per_sample = learning_rate_schedule(0.007, UnitType.sample)
trainer = Trainer(z, z+0, z+0, \
trainer = Trainer(z, (z+0, z+0), \
[momentum_sgd(z.parameters, lr_per_sample, momentum_time_constant,
True)])

Expand Down Expand Up @@ -191,7 +191,7 @@ def backward(self, state, root_gradients):
z = m+p

lr_per_sample = learning_rate_schedule(0.007, UnitType.sample)
trainer = Trainer(z, z+0, z+0, \
trainer = Trainer(z, (z+0, z+0), \
[sgd(z.parameters, lr_per_sample)])

for i in range(100):
Expand Down Expand Up @@ -243,7 +243,7 @@ def inc(self, arg):

momentum_time_constant = momentum_as_time_constant_schedule(1100)
lr_per_sample = learning_rate_schedule(0.007, UnitType.sample)
trainer = Trainer(z, z+0, z+0, \
trainer = Trainer(z, (z+0, z+0), \
[momentum_sgd(z.parameters, lr_per_sample, momentum_time_constant,
True)])

Expand Down
Loading

0 comments on commit 99c25c0

Please sign in to comment.