{"id":2465,"date":"2018-07-31T11:04:53","date_gmt":"2018-07-31T15:04:53","guid":{"rendered":"https:\/\/blogs.mathworks.com\/iot\/?p=2465"},"modified":"2020-12-16T21:16:19","modified_gmt":"2020-12-17T02:16:19","slug":"create-and-train-a-feedforward-neural-network","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/iot\/2018\/07\/31\/create-and-train-a-feedforward-neural-network\/","title":{"rendered":"Create and Train a Feedforward Neural Network"},"content":{"rendered":"<p>We have published an example in the ThingSpeak <a href=\"https:\/\/www.mathworks.com\/help\/thingspeak\/create-and-train-a-feedforward-neural-network.html\">documentation<\/a> that shows you how to train a feedforward neural network to predict temperature. The feedforward neural network is one of the simplest\u00a0types of artificial networks but has broad applications in IoT.\u00a0Feedforward networks consist of a series of layers. The first layer has a connection from the network input. Each other layer has a connection from the previous layer. The final layer produces the network\u2019s output. In our IoT application, the output will be the predicted temperature.<\/p>\n<h3>IoT Application<\/h3>\n<p>We are collecting data in a ThingSpeak channel and will use the integrated MATLAB analytics. To\u00a0predict the temperature, this example\u00a0makes use of the\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/nnet\/index.html\">Neural Network Toolbox<\/a>\u00a0in MATLAB along with the data collected in a <a href=\"https:\/\/thingspeak.com\">ThingSpeak<\/a> channel. We will be using data collected by a weather station located at MathWorks offices in Natick, Massachusetts.<\/p>\n<p><a href=\"https:\/\/www.mathworks.com\/help\/thingspeak\/create-and-train-a-feedforward-neural-network.html\"><img decoding=\"async\" loading=\"lazy\" width=\"640\" height=\"480\" class=\"aligncenter size-full wp-image-2056\" src=\"https:\/\/blogs.mathworks.com\/iot\/files\/2017\/04\/ThingSpeak_weather_station.jpg\" alt=\"\" \/><\/a><\/p>\n<p>The process for creating, training, and using a feedforward network to predict the temperature is as follows:<\/p>\n<ol>\n<li>Gather data from the weather station<\/li>\n<li>Create a two-layer feedforward network<\/li>\n<li>Train the feedforward network<\/li>\n<li>Use the trained model to predict data<\/li>\n<\/ol>\n<h3 id=\"bvjl55b-2\" class=\"title\">Read Data from the Weather Station\u00a0<span class=\"trademark\">ThingSpeak<\/span>\u00a0Channel<\/h3>\n<p><a href=\"https:\/\/thingspeak.com\">ThingSpeak<\/a> channel <a href=\"https:\/\/thingspeak.com\/channels\/12397\">12397<\/a> contains data from the MathWorks\u00a0weather station, located in Natick, Massachusetts.\u00a0The data is collected once every minute. Fields 2, 3, 4, and 6 contain wind speed (mph), relative humidity, temperature (F), and atmospheric pressure (hg) data respectively. To read the data from the weather station within MATLAB,\u00a0use the\u00a0<em>thingSpeakRead<\/em>\u00a0function.<\/p>\n<div class=\"codeinput\">\n<pre>data = thingSpeakRead(12397,<span style=\"color: #a020f0;\">'Fields'<\/span>,[2 3 4 6],<span style=\"color: #a020f0;\">'DateRange'<\/span>,[datetime(<span style=\"color: #a020f0;\">'Jul 30, 2018'<\/span>),datetime(<span style=\"color: #a020f0;\">'Jul 31, 2018'<\/span>)],<span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'outputFormat'<\/span>,<span style=\"color: #a020f0;\">'table'<\/span>);<\/pre>\n<h3 id=\"bvjl55b-3\" class=\"title\">Create Two-Layer Feedforward Network<\/h3>\n<p class=\"shortdesc\">Use the\u00a0<em>feedforwardnet<\/em>\u00a0function to create a two-layer feedforward network. The network has one hidden layer with 10 neurons and an output layer.<\/p>\n<div class=\"procedure\">\n<div class=\"code_responsive\">\n<div class=\"programlisting\">\n<div class=\"codeinput\">\n<pre>net = feedforwardnet(10);<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 id=\"bvjl6m7-1\" class=\"title\">Train the Feedforward Network<\/h3>\n<p class=\"shortdesc\">Use the\u00a0<em>train<\/em>\u00a0function to train the feed-forward network.<\/p>\n<div class=\"procedure\">\n<div class=\"code_responsive\">\n<div class=\"programlisting\">\n<div class=\"codeinput\">\n<pre>[net,tr] = train(net,inputs,targets);<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3 id=\"bvjl6p8-1\" class=\"title\">Use the Trained Model to Predict Data<\/h3>\n<p class=\"shortdesc\">After the network is trained and validated, you can use the network object to calculate the network response to any input.<\/p>\n<div class=\"procedure\">\n<div class=\"code_responsive\">\n<div class=\"programlisting\">\n<div class=\"codeinput\">\n<pre>output = net(inputs(:,5))<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"code_responsive\">\n<div class=\"programlisting\">\n<div class=\"codeoutput\">\n<pre>output =\r\n\r\n   74.9756<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><a href=\"https:\/\/www.mathworks.com\/help\/thingspeak\/create-and-train-a-feedforward-neural-network.html\"><img decoding=\"async\" loading=\"lazy\" width=\"457\" height=\"711\" class=\"aligncenter size-full wp-image-2481\" src=\"https:\/\/blogs.mathworks.com\/iot\/files\/2018\/07\/feedforward.png\" alt=\"\" \/><\/a><\/p>\n<p>This example can be adapted\u00a0to other IoT applications. Check out the ThingSpeak <a href=\"https:\/\/www.mathworks.com\/help\/thingspeak\/create-and-train-a-feedforward-neural-network.html\">documentation<\/a> for the code and explanation.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/iot\/files\/2017\/04\/ThingSpeak_weather_station.jpg\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>We have published an example in the ThingSpeak documentation that shows you how to train a feedforward neural network to predict temperature. The feedforward neural network is one of the&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/iot\/2018\/07\/31\/create-and-train-a-feedforward-neural-network\/\">read more >><\/a><\/p>\n","protected":false},"author":148,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22,293],"tags":[210,126,269,209,271],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/posts\/2465"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/users\/148"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/comments?post=2465"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/posts\/2465\/revisions"}],"predecessor-version":[{"id":2483,"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/posts\/2465\/revisions\/2483"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/media?parent=2465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/categories?post=2465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/iot\/wp-json\/wp\/v2\/tags?post=2465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}