code: def train_model(
      base_model_file,
      feature_node_name,
      last_hidden_node_name,
      image_width,
      image_height,
      num_channels,
      num_classes,
      train_map_file,
      num_epochs,
      max_images=-1,
      freeze=False,
  ):
      epoch_size = sum(1 for line in open(train_map_file))
      if max_images > 0:
          epoch_size = min(epoch_size, max_images)
  
      # Create the minibatch source and input variables
      minibatch_source = create_mb_source(
          train_map_file,
          image_width,
          image_height,
          num_channels,
          num_classes,
      )
      image_input = C.input_variable(
          (num_channels, image_height, image_width)
      )
      label_input = C.input_variable(num_classes)
  
      # Define mapping from reader streams to network inputs
      input_map = {
          image_input: minibatch_source[features_stream_name],
          label_input: minibatch_source[label_stream_name],
      }
  
      # Instantiate the transfer learning model and loss function
      tl_model = create_model(
          base_model_file,
          feature_node_name,
          last_hidden_node_name,
          num_classes,
          image_input,
          freeze,
      )
      ce = cross_entropy_with_softmax(tl_model, label_input)
      pe = classification_error(tl_model, label_input)
  
      # Instantiate the trainer object
      lr_schedule = learning_parameter_schedule(lr_per_mb)
      mm_schedule = momentum_schedule(momentum_per_mb)
      learner = momentum_sgd(
          tl_model.parameters,
          lr_schedule,
          mm_schedule,
          l2_regularization_weight=l2_reg_weight,
      )
      progress_printer = ProgressPrinter(
          tag="Training", num_epochs=num_epochs
      )
      trainer = Trainer(tl_model, (ce, pe), learner, progress_printer)
  
      # Get minibatches of images and perform model training
      print(
          "Training transfer learning model for {0} epochs (epoch_size = {1}).".format(
              num_epochs, epoch_size
          )
      )
      log_number_of_parameters(tl_model)
      for epoch in range(num_epochs):  # loop over epochs
          sample_count = 0
          while (
              sample_count < epoch_size
          ):  # loop over minibatches in the epoch
              data = minibatch_source.next_minibatch(
                  min(mb_size, epoch_size - sample_count),
                  input_map=input_map,
              )
              trainer.train_minibatch(data)  # update model with it
              sample_count += (
                  trainer.previous_minibatch_sample_count
              )  # count samples processed so far
              if sample_count % (100 * mb_size) == 0:
                  print("Processed {0} samples".format(sample_count))
  
          trainer.summarize_training_progress()
  
      return tl_model
  
imports: from __future__ import print_function
  import numpy
  import cntk
  import os
  from PIL import Image
  from cntk.device import try_set_default_device, gpu
  from cntk import load_model, placeholder, Constant
  from cntk import Trainer
  from cntk.logging.graph import find_by_name, get_node_outputs
  from cntk.io import MinibatchSource, ImageDeserializer, StreamDefs, StreamDef
  import cntk.io.transforms
  from cntk.layers import Dense
  from cntk.learners import momentum_sgd, learning_parameter_schedule, momentum_schedule
  from cntk.ops import combine, softmax
  from cntk.ops.functions import CloneMethod
  from cntk.losses import cross_entropy_with_softmax
  from cntk.metrics import classification_error
  from cntk.logging import log_number_of_parameters, ProgressPrinter
label_desc: Use image node_id 3 for calling a global function with example usage: train_model(base_model_file, feature_node_name, last_hidden_node_name, image_width, image_height, num_channels, num_classes, train_map_file, num_epochs, max_images, freeze) and returns: tl_model