Page 69 - MSDN Magazine, March 2018
P. 69

all_test = rdr.next_minibatch(num_test, input_map=heart_input_map) acc = (1.0 - trainer.test_minibatch(all_test)) * 100 print("Classification accuracy on the %d data items = %0.2f%%" \
% (num_test,acc))
A new data reader is created. Notice that unlike the reader used for training, the new reader doesn’t traverse the data in random order, and that the number of sweeps is set to 1. The heart_input_map dictionary object is recreated. A common mistake is to try and use the original object—but the rdr object has changed, so you need to recreate the mapping. The test_minibatch function returns the average classification error for its mini-batch argument, which in this case is the entire dataset.
The demo program doesn’t compute the loss/error for the entire dataset. You can use the previous_minibatch_loss_average function, but you have to be careful not to perform an additional training iteration, which would change the network.
After training, or during training, you’ll usually want to save the model. In CNTK, saving looks like:
mdl_name = ".\\Models\\cleveland_bnn.model" model.save(mdl_name)
This saves using the default CNTK v2 format. An alternative is to use the Open Neural Network Exchange (ONNX) format. Notice that you’ll generally want to save the model object (with softmax activation) rather than the nnet object.
From a different program, a saved model could be loaded into memory along the lines of:
mdl_name = ".\\Models\\cleveland_bnn.model" model = C.ops.functions.Function.load(mdl_name)
After loading, the model can be used as if it had just been trained.
The demo program doesn’t use the trained model to make a pre- diction. You can write code like this:
unknown = np.array([0.5555, -1, ... ], dtype=np.float32) predicted = model.eval(unknown)
The result returned into variable predicted would be a 1x2 matrix with values that sum to 1.0, for example [[0.2500, 0.7500]]. Because the second value is larger, the result would map to (0, 1), which in turn would map to “no disease.”
Wrapping Up
Most deep learning code libraries perform neural network binary classification using the one-node technique. Using this approach, the values of the variable to predict are encoded as either 0 or 1. The output dimension would be set to 1 instead of 2. You’d have to use binary cross-entropy error instead of ordinary cross-entropy error. CNTK doesn’t have a built-in classification error function that works with one node, so you’d have to implement your own function from scratch. When training, less information is typically gained on each iteration (although training is a bit faster), so you’ll usually have to train a one-node model for more iterations than when using the two-node technique. For these reasons, I prefer using the two-node technique for neural binary classification. n
Dr. James mccaffrey works for Microsoft Research in Redmond, Wash. He has worked on several Microsoft products, including Internet Explorer and Bing. Dr. McCaffrey can be reached at jamccaff@microsoft.com.
Thanks to the following Microsoft technical experts who reviewed this article: Chris Lee, Ricky Loynd, Ken Tran
msdnmagazine.com
Get news from MSDN in your inbox!
Sign up to receive the
MICROSOFT DEVELOPER NEWSLETTER, which delivers the latest resources, SDKs, downloads, partner offers, security news, and updates on national and local developer events.
magazine
msdn.microsoft.com/flashnewsletter


































































































   67   68   69   70   71