Single Layered Neural Networks in R Programming

22 Jul, 2020
Neural networks also known as neural nets is a type of algorithm in machine learning and artificial intelligence that works the same as the human brain operates. The artificial neurons in the neural network depict the same behavior of neurons in the human brain. Neural networks are used in risk analysis of business, forecasting the sales, and many more. Neural networks are adaptable to changing inputs so that there is no need for designing the algorithm again based on the inputs. In this article, we’ll discuss single-layered neural network with its syntax and implementation of the neuralnet() function in R programming. Following function requires neuralnet package.
Neural Networks can be classified into multiple types based on their depth activation filters, Structure, Neurons used, Neuron density, data flow, and so on. The types of Neural Networks are as follows:
PerceptronFeed Forward Neural NetworksConvolutional Neural NetworksRadial Basis Function Neural NetworksRecurrent Neural NetworksSequence to Sequence ModelModular Neural Network
Perceptron
Feed Forward Neural Networks
Convolutional Neural Networks
Radial Basis Function Neural Networks
Recurrent Neural Networks
Sequence to Sequence Model
Modular Neural Network
Depending upon the number of layers, there are two types of neural networks:
Single Layered Neural Network: A single layer neural network contains input and output layer. The input layer receives the input signals and the output layer generates the output signals accordingly.Multilayer Neural Network: Multilayer neural network contains input, output and one or more than one hidden layer. The hidden layers perform intermediate computations before directing the input to the output layer.
Single Layered Neural Network: A single layer neural network contains input and output layer. The input layer receives the input signals and the output layer generates the output signals accordingly.
Multilayer Neural Network: Multilayer neural network contains input, output and one or more than one hidden layer. The hidden layers perform intermediate computations before directing the input to the output layer.
A single-layered neural network often called perceptrons is a type of feed-forward neural network made up of input and output layers. Inputs provided are multi-dimensional. Perceptrons are acyclic in nature. The sum of the product of weights and the inputs is calculated in each node. The input layer transmits the signals to the output layer. The output layer performs computations. Perceptron can learn only a linear function and requires less training output. The output can be represented in one or two values(0 or 1).
R language provides neuralnet() function which is available in neuralnet package to perform single layered neural network.
Syntax:neuralnet(formula, data, hidden)
Parameters:formula: represents formula on which model has to be fitteddata: represents dataframehidden: represents number of neurons in hidden layers
To know about more optional parameters of the function, use below command in console: help(“neuralnet”)
Example 1:In this example, let us create the single-layered neural network or perceptron of iris plant species of setosa and versicolor based on sepal length and sepal width.Step 1: Install the required package
# Install the required packageinstall.packages("neuralnet")
Step 2: Load the package
# Load the packagelibrary(neuralnet)
Step 3: Load the dataset
# Load datasetdf <- iris[1:100, ]
Step 4: Fitting neural network
nn = neuralnet(Species ~ Sepal.Length               + Sepal.Width, data = df,              hidden = 0, linear.output = TRUE)
Step 5: Plot neural network
# Output to be present as PNG filepng(file = "neuralNetworkGFG.png")  # Plotplot(nn)  # Saving the filedev.off()
Output:
Example 2:In this example, let us create more reliable neural network using multi-layer neural network and make predictions based on the dataset.Step 1: Install the required package
# Install the required packageinstall.packages("neuralnet")
Step 2: Load the package
# Load the packagelibrary(neuralnet)
Step 3: Load the dataset
# Load datasetdf <- mtcars
Step 4: Fitting neural network
nn <- neuralnet(am ~ vs + cyl + disp + hp + gear                 + carb + wt + drat, data = df,                hidden = 3, linear.output = TRUE)
Step 5: Plot neural network
# Output to be present as PNG filepng(file = "neuralNetwork2GFG.png")  # Plotplot(nn)  # Saving the filedev.off()
Step 6: Create test dataset
# Create test datasetvs = c(0, 1, 1)cyl =c(6, 8, 8)disp = c(170, 250, 350)hp = c(120, 240, 300)gear = c(4, 5, 4)carb = c(4, 3, 3)wt = c(2.780, 3.210, 3.425)drat = c(3.05, 4.02, 3.95)  test <- data.frame(vs, cyl, disp, hp,                   gear, carb, wt, drat)
Step 7: Make prediction of test dataset
Predict <- compute(nn, test)  cat("Predicted values:\n")print(Predict$net.result)
Step 8: Convert prediction into binary values
probability <- Predict$net.resultpred <- ifelse(probability > 0.5, 1, 0)cat("Result in binary values:\n")print(pred)
Output:
Predicted values:
          [,1]
[1,] 0.3681382
[2,] 0.9909768
[3,] 0.9909768

Result in binary values:
     [,1]
[1,]    0
[2,]    1
[3,]    1

Explanation:In above output, “am” value for each row of test dataset is predicted using multi-layer neural network. As in neural network created by the function, predicted values greater than 0.49 makes “am” value of car to be 1.
Single layer neural networks are easy to set up and train them as there is absence of hidden layers
It has explicit links to statistical models
It can work better only for linearly separable data.
Single layer neural network has low accuracy as compared to multi-layer neural network.
data-science
Picked
R Machine-Learning
Machine Learning
R Language
Machine Learning
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.