Drawing Neural Networks in R: Understanding Neural Network Structure with Line-by-Line Explanations (feat. neuralnet)
Directly to PPT, etc. Neural Networks Doesn't it seem too manual and time-consuming to try to draw a neural network? In this article, I'll explain how to draw a neural network in R. I'll show you step by step, line by line, so you can follow along, even if you're a beginner. I'll show you how to draw a neural network in R, and I'll explain the important points along the way.
What is Drawing a Neural Network?
Drawing neural networks refers to the process of visualizing network structures that we often encounter in machine learning or deep learning. A neural network consists of an input layer, a hidden layer, and an output layer, each of which is made up of multiple nodes (neurons). Visualizing these connected neural networks makes it easier to understand how they process data.
Why draw neural networks in R?
R is a powerful tool for data analysis and visualization, and it offers several packages that make it easy to plot complex models, especially neural networks. In this article, we'll look at the neuralnet packagewhich allows you to visualize beautiful neural network structures with just a little code in R.

R Code for Drawing Neural Networks
Now let's look at an example code for drawing a neural network in R. As you follow along, I'll explain in detail, line by line, how the structure of a neural network is drawn.
#Install and load the required packages
if(!require("neuralnet")) install.packages("neuralnet")
library(neuralnet)
#Generate simple neural network data
set.seed(123)
data <- data.frame(
input1 = runif(100, 0, 1),
input2 = runif(100, 0, 1),
output = sample(0:1, 100, replace = TRUE)
)
Create a # neural network model (2 inputs, 1 hidden layer, 3 nodes)
nn <- neuralnet(output ~ input1 + input2, data = data, hidden = c(3))
Visualize the # neural network
plot(nn)Have you ever panicked when code pops up out of nowhere? Installing R, RStudio - Windows Take a look at the post and try running the code above.
Code description (in line-by-line detail)
1. Install and load the package
if(!require("neuralnet")) install.packages("neuralnet")
library(neuralnet)In this example, we will use the neuralnet package for drawing neural networks in R. First, check if the package is installed with require(), and if not (!), install it with install.packages(). Then, use the library() function to load the package. This package makes it easy to construct and visualize neural network models.
2. generate data
set.seed(123)
data <- data.frame(
input1 = runif(100, 0, 1),
input2 = runif(100, 0, 1),
output = sample(0:1, 100, replace = TRUE)
)set.seed(123): Here we use the set.seed() function to fix a random value. This ensures that the same random value is always generated so that the results of running the code are consistent. This means that the results of my run or your run will be the same.
runif(100, 0, 1): Generate 100 random values between 0 and 1. These will be used as inputs named input1, input2.
sample(0:1, 100, replace = TRUE): Generate 100 random values between 0 and 1 and set them as output. The output is a value commonly used in binary classification problems.
data.frame(): Prepares the data for use in the neural network model by grouping the data created above into a single dataframe. This creates a dataframe with three columns: input1, input2, and output.
3. Create a neural network model
nn <- neuralnet(output ~ input1 + input2, data = data, hidden = c(3))The neuralnet() function trains a neural network. It takes inputs (input1, input2) and outputs (output) in the form of a formula.
hidden = c(3) is an option to set the number of nodes in the hidden layer. Here, we used 3 nodes for the hidden layer. Although increasing the number of nodes in the hidden layer can increase the complexity of the neural network, Overfittingso it's important to set it up properly.
4. Visualize the neural network
plot(nn)The plot() function visualizes the generated neural network model. It plots the structure of the connections between the input layer, hidden layer, and output layer. This visualization allows you to intuitively see how data flows through the neural network. When running the full code above, you can see the neural network model below.

Components of a neural network
Now let's look at the components of a neural network more specifically.
Input Layer: The first layer that accepts data, where each node represents one characteristic of the input data.
Hidden LayerThis is the intermediate layer between the input and output layers, responsible for processing data and recognizing patterns through training. The number of nodes in the hidden layer is directly related to the performance of the model.
Output LayerThis is the layer that outputs the final prediction result, which can take many forms depending on the given problem. For binary classification problems, the prediction will be either 0 or 1.
Drawing Neural Networks: For Better Visualization
The neuralnet package allows you to visualize basic neural network structures, but if you need more complex structures or more customization, you can use a package like DiagrammeR with it. This package provides a more intuitive representation of the connections between nodes and layers.
Organize
Now you have a better understanding of how neural networks are plotted in R. You know how to set up each component of a neural network and how to visualize it. Take a look at the code above and try drawing your own neural network structure with different inputs and hidden layers!
1Describe the output of the TP5T visualization
- Nodes:
Input Layer: There are two input nodes, labeled input1 and input2. Each node receives input1 and input2, which are the input data for the neural network.
Hidden Layer: There are three hidden nodes in the center. These nodes pass the calculated results to the output layer, which are weighted based on the values passed from the input layer.
Output Layer: Finally, there are output nodes that represent the predictions of the neural network.
- Connections:
Each input node (input1 and input2) is associated with each node in the hidden layer.
Each hidden layer node is also connected to an output node, with each line representing a connection between them.
The connection lines are labeled with weight values. Weights are values that are adjusted during the training process of a neural network, indicating how much a particular input affects the output.
- Weights:
In the figure, the number on each connection line represents the weight of that connection.
For example, the weight of the connection from input1 to the first hidden node is shown as 14.16047. This means that the value input from input1 is multiplied by this weight and passed to the first hidden node.
Also, the weights from the hidden nodes to the output node are important. For example, one of the weights from the hidden layer to the output node is shown as 35.66218.
- Bias:
The nodes marked with a 1 in the neural network visualization are bias nodes. By adding a bias to each layer, the neural network has more flexibility to learn the data. Bias nodes always have an input value of 1 and are treated by weights that are adjusted during the training process.
The lines from the bias nodes to each of the hidden and output nodes show the weight values in blue. For example, the weight going from the first bias node to the first hidden node is -29.11053.
- Error and Learning Steps:
At the bottom, it says Error: 9.78749, Steps: 8101.
Error represents the error value that the neural network made when attempting to optimize during the training process. The smaller this value is, the better the neural network has been trained.
Steps indicates the number of iterative learning steps before the lesson is completed. In this course, you've completed 8101 steps.
- Synthesized interpretation:
As the input data (input1, input2) passes through each node, a transformed value based on the weights is passed to the hidden layer, and the result is passed back to the output node to produce a prediction (output).
The higher the weight, the more that input value affects the output, while a negative weight means that the value affects the output in the opposite direction.
Bias nodes serve to make the activation function of a particular layer more flexible and improve learning performance.



