Page 54 - MSDN Magazine, March 2019
P. 54

TesT Run JAMES MCCAFFREY Neural Regression Using PyTorch
The goal of a regression problem is to predict a single numeric value. For example, you might want to predict the price of a house based on its square footage, age, ZIP code and so on. In this article I show how to create a neural regression model using the PyTorch code library. The best way to understand where this article is headed is to take a look at the demo program in Figure 1.
The demo program creates a prediction model based on the Boston Housing dataset, where the goal is to predict the median house price in one of 506 towns close to Boston. The data comes from the early 1970s. Each data item has 13 predictor variables, such as crime index of the town, average number of rooms per house in the town and so on. There’s only one output value because the goal is to predict a single numeric value.
The demo loads 404 training items and 102 test items into mem- ory, and then creates a 13-(10-10)-1 neural network. The neural network has two hidden processing layers, each of which has 10 nodes. The number of input and output nodes is determined by the data, but the number of hidden layers and the number
of nodes in each are free parameters that must be deter-
mined by trial and error.
The demo trains the neural network, meaning the values of the weights and biases that define the behavior of the neural network are computed using the training data, which has known correct input and output values. After training, the demo computes the accuracy of the model on the test data (75.49 percent, 77 out of 102 correct). The test accuracy is a rough measure of how well you’d expect the model to do on new, previously unseen data.
The demo concludes by making a prediction for the first test town. The 13 raw input values are (0.09266, 34.0, . . 8.67). When the neural regression model was trained, normal- ized data (scaled so all values are between 0.0 and 1.0) was used, so when making a prediction the demo had to use normalized data, which is (0.00097, 0.34, . . 0.191501). The model predicts that the median house price is $24,870.07, quite close to the actual median price of $26,400.
This article assumes you have intermediate or better programming skill with a C-family language and a basic familiarity with machine learning. The complete demo code is presented in this article. The source code and the two data files used by the demo are also available in the
accompanying download. All normal error checking has been removed to keep the main ideas as clear as possible.
Installing PyTorch
Installing PyTorch involves two main steps. First you install Python and several required auxiliary packages, such as NumPy and SciPy, then you install PyTorch as an add-on Python package.
The goal of a regression problem is to predict a single numeric value.
Although it’s possible to install Python and the packages required to run PyTorch separately, it’s much better to install a Python distribution. For my demo, I installed the Anaconda3 5.2.0 distri- bution (which contains Python 3.6.5) and PyTorch 1.0.0. If you’re
Code download available at msdn.com/magazine/0319magcode.
48 msdn magazine
Figure 1 Neural Regression Using a PyTorch Demo Run


































































































   52   53   54   55   56