# Getting Started with Neural Networks Using MATLAB

A neural network is an adaptive system that learns by using interconnected nodes. Neural networks are useful in many applications: you can use them for clustering, classification, regression, and time-series predictions. In this video, you’ll walk through an example that shows what neural networks are and how to work with them in MATLAB^{®}. The video outlines how to train a neural network to classify human activities based on sensor data from smartphones.

You’ll also learn how to create a network; prepare data; train, validate and improve your network; and update your network and deploy it.

**Published:
2 Jun 2020**

Hello and welcome back to another MATLAB video. Today we’re going to be talking about neural networks and we will train one to classify human activities based on sensor data from smartphones.

Neural networks are useful in many applications – they can be used for clustering, classification, regression, and time series predictions.

A Neural Network* *is an adaptive system that learns by using interconnected nodes.

Neural networks consist of one or more layers. They include at least 3 layers: the input layer, a hidden layer, and the output layer.

In general, the algorithm involves a series of mathematical operations that calculate a weighted sum of the inputs at each node.

Each neuron in a layer has adjustable weights for its inputs and an adjustable bias.

A neural network operates (is trained) by adjusting all of these weights and biases and minimizing the error throughout the training phase to achieve more accurate results.

Let’s discuss this further this with a demo

This example uses sensor data containing measurements taken from smartphones worn by people while doing 5 different activities - walking, sitting, laying, walking upstairs and downstairs.

The goal of this analysis is to build a model to automatically identify the activity type given the sensor measurements, using a neural network.

We first import the data set, which contains the activity label and statistical measurements from the sensors.

In this case, we are solving a classification problem and will create a neural network for pattern recognition.

There are different functions for creating various types of networks. Use the documentation to determine the function and to learn more about the types of networks.

Let’s create a simple feed-forward pattern recognition network with the default values.

**Create network**

In this case, we are solving a classification problem and will create a neural network for pattern recognition.

There are different functions for creating various types of networks. Use the documentation to determine the function and to learn more about the types of networks.

Let’s create a simple feed-forward pattern recognition network with the default values.

The network variable contains information about the parameters and equations and will be updated as the model trains.

You can visualize the network. The default is 10 neurons in one hidden layer.

You can access the layer information including the weights and biases. These are currently empty, since we haven’t yet trained the model.

Next, we will include the ratio for splitting the training, validation and test data. The network uses this information to evaluate the accuracy and optimize the parameters during training.

Before we can train the network, the data must be prepared

The pattern recognition network expects the variables to be along the rows and observations along the columns. We can simply transpose the data in our example to achieve this arrangement.

For this type of network, the predictor and response, or X and Y variables must be numeric. You can use a dummy variable to represent categorical data, like the activity, as a matrix of 1’s and 0’s.

Now we are ready to train the network using the training data!

You can examine progress while the network is training and stop early if needed.

Now, the network has finished training and contains updated parameter values, learned from the data.

Remember there was one hidden layer with 10 nodes and one output layer. Our data set has 5 classes, so there are 5 output nodes.

The weights and biases have been updated with the values determined from training.

Now we can test the network and predict the activity using the test data.

The result is a score of belonging to each class.

We can determine the predicted class and convert the numeric values to the original labels for comparison.

Find the accuracy and plot a confusion matrix to evaluate the classifications.

Laying and sitting are almost all classified correctly. And while most are correct, the different types of walking are occasionally classified as one another.

This seems like a good network with reasonable misclassifications, but you can explore ways to improve even more.

There are many strategies for improving the network.

We can try updating some parameters for training and evaluating the network. See the documentation for details about the parameters for different networks.

There’s not much improvement in the output here. We could continue to make adjustments or try different types of networks.

For example, you could create a network with more hidden layers, or a deep neural network. There are many types of deep networks supported in MATLAB and resources for deep learning. For more info, check out the links in the description below.

There’s always room for improvement, but this model seems to be performing well enough with 92% accuracy. We could now take steps to prepare to use this model in a production environment or integrate with a system.

When deploying, you capture your steps into a function and will also need to save the network or recreate it.

You can generate a MATLAB function or Simulink diagram for simulating your neural network. Use genfunction to create the neural network including all settings, weight and bias values, functions, and calculations in one MATLAB function file.

Now our neural network could be used in a Simulink model or included in an application written in C/C++, Java, Python and more. See the documentation for more info.

Now that we have a deeper understanding of neural networks in MATLAB, we can more effectively train and evaluate these models.

For more practice, you can search the documentation for examples.

Thanks for watching and I’ll see you in another video.

Featured Product

#### Statistics and Machine Learning Toolbox

### Up Next:

### Related Videos:

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)