Before you go, check out these stories!

0
Hackernoon logoHow to Create A Neural Network Using R by@mridulrb

How to Create A Neural Network Using R

Author profile picture

@mridulrbMridul Bhandari

Hi there! 👋 I'm a ☁️ Developer Advocate 👩‍💻👨‍💻from 👁️🐝Ⓜ️

Create and visualize a neural network that takes in a dataset and trains a model to predict outcomes.

GitHub Repsitory

What is R?
R is a programming language and free software environment for statistical computing and graphics. The R language is widely used among statisticians and data miners for developing statistical software and data analysis.

Advantages of using R
It’s an open-source language, related to other languages making it cross-platform compatible.

Installation Guide
Download R & RStudio

Packages
CRAN — Comprehensive R Archive Network
CRAN is a network of FTP and web servers around the world that store identical, up-to-date, versions of code and documentation for R.

To install packages in R, type the below mentioned commands in R Console or Go To Tools and Click on *Install Packages* and install the below-mentioned packages.

<
shiny — install.packages(“shiny”) 
nnet — install.packages(“nnet”)
EBImage — install.packages(“BiocManager”)
BiocManager::install(“EBImage”) >

Syntax in R
To get familiear with R. Run the code from Syntax.R which contains an example of if-else statement, for loop, while loop, break & next statement and a function.

Linear Regression and Curve Fitting
Linear regression is a linear approach to modelling the relationship between a scalar response dependent variable and independent variables.

Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subject to constraints.

Run the code from Regression&Curvefit.R to understand how Linear Regression and Curve Fitting works in R.

Neural Network
A primer in neural networks
An Artificial Neuron Network (ANN), popularly known as Neural Network is a computational model based on the structure and functions of biological neural networks. It is like an artificial human nervous system for receiving, processing, and transmitting information in terms of Computer Science.
Basically, there are 3 different layers in a neural network :-
Input Layer (All the inputs are fed in the model through this layer)
Hidden Layers (There can be more than one hidden layers which are used for processing the inputs received from the input layers)
Output Layer (The data after processing is made available at the output layer)

Input Layer
The Input layer communicates with the external environment that presents a pattern to the neural network. Its job is to deal with all the inputs only. This input gets transferred to the hidden layers which are explained below. The input layer should represent the condition for which we are training the neural network. Every input neuron should represent some independent variable that influences the output of the neural network.

Hidden Layer
The hidden layer is the collection of neurons that has activation function applied to it and it is an intermediate layer found between the input layer and the output layer. Its job is to process the inputs obtained by the previous layer. So it is the layer that is responsibly for extracting the required features from the input data. Much researches have been made in evaluating the number of neurons in the hidden layer but still, none of them was successful in finding the accurate result. Also, there can be multiple hidden layers in a Neural Network. So you must be thinking that how many hidden layers have to be used for which kind of problem. Suppose that if we have data that can be separated linearly, then there is no need to use a hidden layer as the activation function can be implemented to input layer which can solve the problem. But in case of problems that deals with complex decisions, we can use 3 to 5 hidden layers based on the degree of complexity of the problem or the degree of accuracy required. That certainly not means that if we keep on increasing the number of layers, the neural network will give high accuracy! A stage comes when the accuracy becomes constant or falls if we add an extra layer! Also, we should calculate the number of neurons in each network. If the number of neurons is less as compared to the complexity of the problem data then there will be very few neurons in the hidden layers to adequately detect the signals in a complicated data set. If unnecessary more neurons are present in the network then Overfitting may occur. Several methods are used till now which do not provide the exact formula for calculating the number of the hidden layers as well as the number of neurons in each hidden layer.

Output Layer
The output layer of the neural network collects and transmits the information accordingly in the way it has been designed to give. The pattern presented by the output layer can be directly traced back to the input layer. The number of neurons in the output layer should be directly related to the type of work that the neural network was performing. To determine the number of neurons in the output layer, first, consider the intended use of the neural network.

To understand a Neural Network we need to visualize a Neural Network.
Run the code from NeuralNet_Visualization.R which will plot a small Neural Network.

Running your first neural network!
It’s as simple as it can get. The steps are as follows:
1. Clone this repository
2. Open up the neuralNetwork.R file on RStudio
3. Install all the packages mentioned earlier in the readme.
4. Run the R file by clicking on the terminal tab at the lower half of the screen and typing Rscript “yourfilepath”. An example of a valid file path in Mac would be “~/neuralNetwork.R”. In this file, the neural network is primed to recognize digits from a handwritten dataset called MNIST. Observe the terminal until it shows iteration. This signifies that training has occurred and the network is at its nth iteration. Wait until the terminal is empty.
5. Open up Project_101411025, which is the R file where the GUI interface using Shiny, an R package has been used.
6. Install all the packages mentioned earlier in the readme.
7. Run the program and use the interface to upload test images present in this repository. Then watch your neural network as it successfully recognizes the digits.

Sign-up for an IBM Cloud account to try this tutorial on cloud.

Conclusion

In this tutorial, we learned how to create and visualize a neural network that takes in a dataset and trains a model to predict outcomes.


Author profile picture

@mridulrbMridul Bhandari

Read my stories

Hi there! 👋 I'm a ☁️ Developer Advocate 👩‍💻👨‍💻from 👁️🐝Ⓜ️

Tags

The Noonification banner

Subscribe to get your daily round-up of top tech stories!