Classifying Data with Neural Networks

Classifying Vector Data

Create an artificial dataset from three normally distributed clusters:
In[1]
Click for copyable input
In[2]
Click for copyable input
Plot the dataset:
In[3]
Click for copyable input
Out[3]
The training data consists of rules mapping the point to the cluster it is in:
In[4]
Click for copyable input
Out[4]
Create a net to compute the probability of a point lying in each cluster, using a "Class" decoder to classify the input as Red, Green or Blue:
In[5]
Click for copyable input
Out[5]
Train the net on the data:
In[6]
Click for copyable input
Out[6]
Evaluate the net on the centers of each cluster:
In[7]
Click for copyable input
Out[7]
Show the contours in feature space in which each class reaches a posterior probability of 0.75:
In[8]
Click for copyable input
Out[8]
Plot the degree of uncertainty of the classifier as measured by entropy as a function of the position:
In[9]
Click for copyable input
Out[9]
Perform logistic regression on the Fisher Iris dataset. First, obtain the training data:
In[10]
Click for copyable input
In[11]
Click for copyable input
Out[11]
Build a list of the unique labels to which each example is assigned:
In[12]
Click for copyable input
Out[12]
Create a NetChain to perform the classification, using a "Class" decoder to interpret the output of the net as probabilities for each class:
In[13]
Click for copyable input
Out[13]
NetTrain will automatically use a CrossEntropyLossLayer with a NetEncoder to interpret the class labels in the training data:
In[14]
Click for copyable input
Out[14]
Classify an input:
In[15]
Click for copyable input
Out[15]
Obtain the probabilities associated with the classification:
In[16]
Click for copyable input
Out[16]
Produce a ClassifierMeasurementsObject to test the accuracy of the trained net on the test set:
In[17]
Click for copyable input
In[18]
Click for copyable input
Out[18]
In[19]
Click for copyable input
Out[19]

Classifying Categorical Data

Perform logistic regression on a dataset containing both categorical and numeric values.

First, obtain the training data, and drop rows containing missing data:
In[1]
Click for copyable input
Out[1]
Split the data into training and test datasets:
In[2]
Click for copyable input

Categorical variables cannot be used directly in neural networks and must be encoded as numeric tensors.

Create "Class" encoders that encode the categorical variables as one-hot encoded vectors:
In[3]
Click for copyable input
Out[3]
Out[3]
Applying the encoders to class labels produces unit vectors:
In[4]
Click for copyable input
Out[4]
In[5]
Click for copyable input
Out[5]

Create a network with an input corresponding to each feature and using a "Boolean" decoder to interpret the output of the net as the probability of survival.

The input features are first concatenated together before being further processed:
In[6]
Click for copyable input
Out[6]
Train the net on the training data. NetTrain will automatically attach a CrossEntropyLossLayer["Binary"] layer to the output of the net:
In[7]
Click for copyable input
Out[7]
Predict whether a passenger will survive:
In[8]
Click for copyable input
Out[8]
Obtain the probability of survival associated with the input:
In[9]
Click for copyable input
Out[9]
Plot the survival probability as a function of age for some combinations of "class" and "sex":
In[10]
Click for copyable input
In[11]
Click for copyable input
Out[11]
Use ClassifierMeasurements to test the accuracy of the trained net on the test set:
In[12]
Click for copyable input
Out[12]
The accuracy is typically comparable to that obtained using Classify when specifying the method "LogisticRegression":
In[13]
Click for copyable input
Out[13]

Multitask Classification

Perform multitask learning by creating a net that produces two separate classifications.

First, obtain training data:
In[1]
Click for copyable input
Out[1]
The training data consists of an image and the corresponding high-level and low-level labels:
In[2]
Click for copyable input
Out[2]
Extract the unique labels from the "Label" and "SubLabel" columns:
In[3]
Click for copyable input
Out[3]
In[4]
Click for copyable input
Out[4]
Create a base convolutional net that will produce a vector of 500 features:
In[5]
Click for copyable input
Out[5]
Create a NetGraph that will produce separate classifications for the high-level and low-level labels:
In[6]
Click for copyable input
Out[6]
Train the network. NetTrain will automatically attach CrossEntropyLossLayer objects to both outputs, taking the target values from the training data using the corresponding names "Label" and "SubLabel":
In[7]
Click for copyable input
Out[7]
Evaluate the trained network on a single image:
In[8]
Click for copyable input
Out[8]
Evaluate the trained network on several images, taking only the "SubLabel" output:
In[9]
Click for copyable input
Out[9]
Get a property of the class decoder of the "SubLabel" output on an input image:
In[10]
Click for copyable input
Out[10]
From a random sample, select the images for which the net produces highest and lowest entropy predictions for "Label":
In[11]
Click for copyable input
In[12]
Click for copyable input
In[13]
Click for copyable input
Out[13]
Out[13]
Produce a subnetwork that computes only "SubLabel" predictions:
In[14]
Click for copyable input
Out[14]
Make a prediction on a single image:
In[15]
Click for copyable input
Out[15]

Related Tutorials