{"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":"
We have published an example in the ThingSpeak 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 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\u00a0Neural Network Toolbox<\/a>\u00a0in MATLAB along with the data collected in a ThingSpeak<\/a> channel. We will be using data collected by a weather station located at MathWorks offices in Natick, Massachusetts.<\/p>\n The process for creating, training, and using a feedforward network to predict the temperature is as follows:<\/p>\n ThingSpeak<\/a> channel 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\u00a0thingSpeakRead<\/em>\u00a0function.<\/p>\n Use the\u00a0feedforwardnet<\/em>\u00a0function to create a two-layer feedforward network. The network has one hidden layer with 10 neurons and an output layer.<\/p>\n Use the\u00a0train<\/em>\u00a0function to train the feed-forward network.<\/p>\n After the network is trained and validated, you can use the network object to calculate the network response to any input.<\/p>\n This example can be adapted\u00a0to other IoT applications. Check out the ThingSpeak documentation<\/a> for the code and explanation.<\/p>\n","protected":false},"excerpt":{"rendered":" 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… 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}]}}IoT Application<\/h3>\n
<\/a><\/p>\n\n
Read Data from the Weather Station\u00a0ThingSpeak<\/span>\u00a0Channel<\/h3>\n
data = thingSpeakRead(12397,'Fields'<\/span>,[2 3 4 6],'DateRange'<\/span>,[datetime('Jul 30, 2018'<\/span>),datetime('Jul 31, 2018'<\/span>)],...<\/span>\r\n 'outputFormat'<\/span>,'table'<\/span>);<\/pre>\nCreate Two-Layer Feedforward Network<\/h3>\n
net = feedforwardnet(10);<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n
Train the Feedforward Network<\/h3>\n
[net,tr] = train(net,inputs,targets);<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n
Use the Trained Model to Predict Data<\/h3>\n
output = net(inputs(:,5))<\/pre>\n<\/div>\n<\/div>\n<\/div>\n
output =\r\n\r\n 74.9756<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n
<\/a><\/p>\n
<\/div>\n