Title: | Generator of 'LaTeX' Code for Drawing Neural Network Diagrams with 'TikZ' |
---|---|
Description: | Generates 'LaTeX' code for drawing well-formatted neural network diagrams with 'TikZ'. Users have to define number of neurons on each layer, and optionally define neuron connections they would like to keep or omit, layers they consider to be oversized and neurons they would like to draw with lighter color. They can also specify the title of diagram, color, opacity of figure, labels of layers, input and output neurons. In addition, this package helps to produce 'LaTeX' code for drawing activation functions which are crucial in neural network analysis. To make the code work in a 'LaTeX' editor, users need to install and import some 'TeX' packages including 'TikZ' in the setting of 'TeX' file. |
Authors: | Chencheng Fang [aut, cre] |
Maintainer: | Chencheng Fang <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.0 |
Built: | 2025-03-09 02:55:24 UTC |
Source: | https://github.com/ccfang2/nndiagram |
The activation_curve
command is used to produce 'LaTeX' code for drawing well-formatted activation functions, which are crucial in neural network analysis.
To make the code work in a 'LaTeX' editor, users need to install and import two 'TeX' packages TikZ and pgfplots in the setting of 'TeX' file.
Syntax of importing these packages is included in the output of function.
activation_curve( expr = "ReLU", title = NULL, xlabel = NULL, ylabel = NULL, xmin = -10, xmax = 10, ymin = NULL, ymax = NULL, suppress = FALSE )
activation_curve( expr = "ReLU", title = NULL, xlabel = NULL, ylabel = NULL, xmin = -10, xmax = 10, ymin = NULL, ymax = NULL, suppress = FALSE )
expr |
an optional character that specifies the activation function. It could be |
title |
an optional character that specifies the main title of diagram. Default is |
xlabel |
an optional character that specifies the label of x-axis. Default is |
ylabel |
an optional character that specifies the label of y-axis. Default is |
xmin |
an optional numeric value that specifies the minimum value of x-axis. Default is |
xmax |
an optional numeric value that specifies the maximum value of x-axis. Default is |
ymin |
an optional numeric value that specifies the minimum value of y-axis. Default is |
ymax |
an optional numeric value that specifies the maximum value of y-axis. Default is |
suppress |
an optional logical value that specifies whether |
activation_curve
uses cat()
to print out 'LaTeX' code on console, if not suppressed. Also, activation_curve
saves the same output as a character vector invisibly, so users could use cat()
to print it out later at their demand, as shown in Examples.
The activation_curve
'LaTeX' output can be directly copied and pasted to produce activation curve in any 'LaTeX' editor.
Chencheng Fang, Bonn Graduate School of Economics, University of Bonn. Email: [email protected]
nndiagram; nndiagram_oversize; nndiagram_nodeCoverup.
# Rectified Linear Unit Function with all arguments default activation_curve() # Sigmoid function with domain from -5 to 5. activation_curve(expr="sigmoid", title="Sigmoid Function", xmin=-5, xmax=5) # Define a parametric ReLU in the argument of \code{expr}. activation_curve(expr="(x>=0)*x+0.2*x*(x<0)", title="Parametric Rectified Linear Unit Function") # Suppress the output of 'LaTeX' code to be directly printed on the console and save the output # to an object, which can be printed later at demand. nnd_activation <- activation_curve(suppress=TRUE) cat(paste(nnd_activation,"\n"))
# Rectified Linear Unit Function with all arguments default activation_curve() # Sigmoid function with domain from -5 to 5. activation_curve(expr="sigmoid", title="Sigmoid Function", xmin=-5, xmax=5) # Define a parametric ReLU in the argument of \code{expr}. activation_curve(expr="(x>=0)*x+0.2*x*(x<0)", title="Parametric Rectified Linear Unit Function") # Suppress the output of 'LaTeX' code to be directly printed on the console and save the output # to an object, which can be printed later at demand. nnd_activation <- activation_curve(suppress=TRUE) cat(paste(nnd_activation,"\n"))
The nndiagram
command is used to produce 'LaTeX' code for drawing well-formatted neural network diagrams. To make the code work in a 'LaTeX' editor, users need
to install and import two 'TeX' packages, i.e., TikZ and
ifthen
in the setting of 'TeX' file. Syntax of importing these packages is included in the output of function.
nndiagram( input, hidden, keep = NULL, omit = NULL, title = NULL, color = "black", alpha = 1, layer.sep = 2.5, layer.label = NULL, input.label = NULL, output.label = NULL, suppress = FALSE )
nndiagram( input, hidden, keep = NULL, omit = NULL, title = NULL, color = "black", alpha = 1, layer.sep = 2.5, layer.label = NULL, input.label = NULL, output.label = NULL, suppress = FALSE )
input |
a positive integer that specifies the number of input neurons. |
a positive integer vector that specifies the number of neurons on each hidden layer. For example, c(4,4,4) specifies that there are 3 hidden layers and 4 neurons on each hidden layer. Non-positive and non-integer numbers are not allowed. |
|
keep |
an optional character vector that specifies the connections of neurons to be kept. For example, c("1->4","5->8") specifies the connections from neuron 1 to 4 and from neuron 5 to 8. Neurons are counted from
top to bottom and from left to right. As special cases, "->4" specifies all connections with neuron 4 as destination, and "5->" specifies all connections with neuron 5 as source. No space in the string is allowed. Default is |
omit |
an optional character vector that specifies the connections of neurons to be omitted. The specification of connections is the same as that in 'keep'. However, users are not allowed to assign values to 'keep'
and 'omit' simultaneously. Default is |
title |
an optional character that specifies the main title of diagram. Default is |
color |
an optional character that specifies the color of lines. Default is |
alpha |
an optional numeric value between 0 and 1 that specifies the opacity of lines. |
layer.sep |
an optional positive numeric value that specifies the distance between layers of a neural network. Default is |
layer.label |
an optional character vector that specifies label for each layer, including input, hidden and output layers. |
input.label |
an optional character vector that specifies label for each input neuron. |
output.label |
an optional character that specifies label for output neuron. |
suppress |
an optional logical value that specifies whether |
nndiagram
uses cat()
to print out 'LaTeX' code on console, if not suppressed. Also, nndiagram
saves the same output as a character vector invisibly, so users could use cat()
to print it out later at their demand, as shown in Examples.
The nndiagram
'LaTeX' output can be directly copied and pasted to produce neural network diagram in any 'LaTeX' editor.
This package is an ongoing project, and more functions will be added in the future, such as those to produce pdf version of diagrams or convert handdrawing neural network diagrams to computerized ones. Collaborations are sincerely welcome. Comments and suggestions are always highly appreciated.
Chencheng Fang, Bonn Graduate School of Economics, University of Bonn. Email: [email protected]
nndiagram_oversize; nndiagram_nodeCoverup; activation_curve.
# A neural network with 3 neurons on input layer, 4 neurons on each of 3 hidden layers, # and 1 neuron on output layer. No connection is omitted and all other arguments are default. nndiagram(input=3, hidden=c(4,4,4)) # Same as the first example but connections from neuron 1 to 4 and from neuron 5 to 8 are omitted. nndiagram(input=3, hidden=c(4,4,4), omit=c("1->4","5->8")) # Same as the first example but connections with neuron 4 as destination are omitted. nndiagram(input=3, hidden=c(4,4,4), omit=c("->4")) # Same as the first example but connections with neuron 5 as source are omitted. nndiagram(input=3, hidden=c(4,4,4), omit=c("5->")) # Suppress the output of 'LaTeX' code to be directly printed on the console and save the output # to an object, which can be printed later at demand. nnd <- nndiagram(input=3, hidden=c(4,4,4), suppress=TRUE) cat(paste(nnd,"\n"))
# A neural network with 3 neurons on input layer, 4 neurons on each of 3 hidden layers, # and 1 neuron on output layer. No connection is omitted and all other arguments are default. nndiagram(input=3, hidden=c(4,4,4)) # Same as the first example but connections from neuron 1 to 4 and from neuron 5 to 8 are omitted. nndiagram(input=3, hidden=c(4,4,4), omit=c("1->4","5->8")) # Same as the first example but connections with neuron 4 as destination are omitted. nndiagram(input=3, hidden=c(4,4,4), omit=c("->4")) # Same as the first example but connections with neuron 5 as source are omitted. nndiagram(input=3, hidden=c(4,4,4), omit=c("5->")) # Suppress the output of 'LaTeX' code to be directly printed on the console and save the output # to an object, which can be printed later at demand. nnd <- nndiagram(input=3, hidden=c(4,4,4), suppress=TRUE) cat(paste(nnd,"\n"))
The nndiagram_nodeCoverup
command is used to produce 'LaTeX' code for drawing well-formatted neural network diagrams, some neurons of which users hope to cover up with lighter color.
To make the code work in a 'LaTeX' editor, users need to install and import the 'TeX' package TikZ in the setting of 'TeX' file. Syntax of importing this package is included in the output of function.
nndiagram_nodeCoverup( input, hidden, node.coverup = NULL, title = NULL, color = "black", alpha = 1, layer.sep = 2.5, layer.label = NULL, input.label = NULL, output.label = NULL, suppress = FALSE )
nndiagram_nodeCoverup( input, hidden, node.coverup = NULL, title = NULL, color = "black", alpha = 1, layer.sep = 2.5, layer.label = NULL, input.label = NULL, output.label = NULL, suppress = FALSE )
input |
a positive integer that specifies the number of input neurons. |
a positive integer vector that specifies the number of neurons on each hidden layer. For example, c(6,6,6) specifies that there are 3 hidden layers and 6 neurons on each hidden layer. Non-positive and non-integer numbers are not allowed. |
|
node.coverup |
an optional positive integer vector that specifies the index of neurons users hope to cover up with lighter color. Neurons are counted from top to bottom and from left to right. For example, in a neural network with |
title |
an optional character that specifies the main title of diagram. Default is |
color |
an optional character that specifies the color of lines. Default is |
alpha |
an optional numeric value between 0 and 1 that specifies the opacity of lines. |
layer.sep |
an optional positive numeric value that specifies the distance between layers of a neural network. Default is |
layer.label |
an optional character vector that specifies label for each layer, including input, hidden and output layers. |
input.label |
an optional character vector that specifies label for each input neuron. |
output.label |
an optional character that specifies label for output neuron. |
suppress |
an optional logical value that specifies whether |
nndiagram_nodeCoverup
uses cat()
to print out 'LaTeX' code on console, if not suppressed. Also, nndiagram_nodeCoverup
saves the same output as a character vector invisibly, so users could use cat()
to print it out later at their demand, as shown in Examples.
The nndiagram_nodeCoverup
'LaTeX' output can be directly copied and pasted to produce neural network diagram in any 'LaTeX' editor.
This package is an ongoing project, and more functions will be added in the future, such as those to produce pdf version of diagrams or convert handdrawing neural network diagrams to computerized ones. Collaborations are sincerely welcome. Comments and suggestions are always highly appreciated.
Chencheng Fang, Bonn Graduate School of Economics, University of Bonn. Email: [email protected]
nndiagram; nndiagram_oversize; activation_curve.
# A neural network with 3 neurons on input layer, 6 neurons on each of 3 hidden layers, # and 1 neuron on output layer. All other arguments are default, so no neuron is # drawn with lighter color. nndiagram_nodeCoverup(input=3, hidden=c(6,6,6)) # Same as the first example but neurons indexed with 4 and 11 are designed to be drawn # with lighter color. nndiagram_nodeCoverup(input=3, hidden=c(6,6,6), node.coverup=c(4,10)) # Same as the first example but distance between layers is defined to be smaller. nndiagram_nodeCoverup(input=3, hidden=c(6,6,6), layer.sep=1.5) # Suppress the output of 'LaTeX' code to be directly printed on the console and save the output # to an object, which can be printed later at demand. nnd_nodeCoverup <- nndiagram_nodeCoverup(input=3, hidden=c(6,6,6), suppress=TRUE) cat(paste(nnd_nodeCoverup,"\n"))
# A neural network with 3 neurons on input layer, 6 neurons on each of 3 hidden layers, # and 1 neuron on output layer. All other arguments are default, so no neuron is # drawn with lighter color. nndiagram_nodeCoverup(input=3, hidden=c(6,6,6)) # Same as the first example but neurons indexed with 4 and 11 are designed to be drawn # with lighter color. nndiagram_nodeCoverup(input=3, hidden=c(6,6,6), node.coverup=c(4,10)) # Same as the first example but distance between layers is defined to be smaller. nndiagram_nodeCoverup(input=3, hidden=c(6,6,6), layer.sep=1.5) # Suppress the output of 'LaTeX' code to be directly printed on the console and save the output # to an object, which can be printed later at demand. nnd_nodeCoverup <- nndiagram_nodeCoverup(input=3, hidden=c(6,6,6), suppress=TRUE) cat(paste(nnd_nodeCoverup,"\n"))
The nndiagram_oversize
command is used to produce 'LaTeX' code for drawing well-formatted neural network diagrams, some layers of which have excess neurons that users hope to leave out. To make the code work in a 'LaTeX' editor, users need
to install and import the 'TeX' package TikZ in the setting of 'TeX' file. Syntax of importing this package is included in the output of function.
nndiagram_oversize( input, hidden, size.cutoff = 7, title = NULL, color = "black", alpha = 1, layer.sep = 2.5, layer.label = NULL, input.label = NULL, output.label = NULL, suppress = FALSE )
nndiagram_oversize( input, hidden, size.cutoff = 7, title = NULL, color = "black", alpha = 1, layer.sep = 2.5, layer.label = NULL, input.label = NULL, output.label = NULL, suppress = FALSE )
input |
a positive integer that specifies the number of input neurons. |
a positive integer vector that specifies the number of neurons on each hidden layer. For example, c(6,6,6) specifies that there are 3 hidden layers and 6 neurons on each hidden layer. Non-positive and non-integer numbers are not allowed. |
|
size.cutoff |
an optional numeric value that specifies the cutoff number of neurons. If the number of neurons on a certain layer is greater than this cutoff value, it is considered as an over-sized layer, which will be drawn with some neurons left out.
Otherwise, all neurons on the layer will be drawn. In |
title |
an optional character that specifies the main title of diagram. Default is |
color |
an optional character that specifies the color of lines. Default is |
alpha |
an optional numeric value between 0 and 1 that specifies the opacity of lines. |
layer.sep |
an optional positive numeric value that specifies the distance between layers of a neural network. Default is |
layer.label |
an optional character vector that specifies label for each layer, including input, hidden and output layers. |
input.label |
an optional character vector that specifies label for each input neuron. |
output.label |
an optional character that specifies label for output neuron. |
suppress |
an optional logical value that specifies whether |
nndiagram_oversize
uses cat()
to print out 'LaTeX' code on console, if not suppressed. Also, nndiagram_oversize
saves the same output as a character vector invisibly, so users could use cat()
to print it out later at their demand, as shown in Examples.
The nndiagram_oversize
'LaTeX' output can be directly copied and pasted to produce neural network diagram in any 'LaTeX' editor.
This package is an ongoing project, and more functions will be added in the future, such as those to produce pdf version of diagrams or convert handdrawing neural network diagrams to computerized ones. Collaborations are sincerely welcome. Comments and suggestions are always highly appreciated.
Chencheng Fang, Bonn Graduate School of Economics, University of Bonn. Email: [email protected]
nndiagram; nndiagram_nodeCoverup; activation_curve.
# A neural network with 3 neurons on input layer, 6 neurons on each of 3 hidden layers, and 1 neuron # on output layer. All other arguments are default, so no layer is considered to be over-sized. nndiagram_oversize(input=3, hidden=c(6,6,6)) # Same as the first example but cutoff value of size is designed to be 5, so all hidden layers are # considered to be over-sized. nndiagram_oversize(input=3, hidden=c(6,6,6), size.cutoff=5) # Same as the second example but labels of input neurons are designed to be letters from a to c. nndiagram_oversize(input=3, hidden=c(6,6,6), size.cutoff=5, input.label=letters[1:3]) # Suppress the output of 'LaTeX' code to be directly printed on the console and save the output # to an object, which can be printed later at demand. nnd_oversize <- nndiagram_oversize(input=3, hidden=c(6,6,6), size.cutoff=5, suppress=TRUE) cat(paste(nnd_oversize,"\n"))
# A neural network with 3 neurons on input layer, 6 neurons on each of 3 hidden layers, and 1 neuron # on output layer. All other arguments are default, so no layer is considered to be over-sized. nndiagram_oversize(input=3, hidden=c(6,6,6)) # Same as the first example but cutoff value of size is designed to be 5, so all hidden layers are # considered to be over-sized. nndiagram_oversize(input=3, hidden=c(6,6,6), size.cutoff=5) # Same as the second example but labels of input neurons are designed to be letters from a to c. nndiagram_oversize(input=3, hidden=c(6,6,6), size.cutoff=5, input.label=letters[1:3]) # Suppress the output of 'LaTeX' code to be directly printed on the console and save the output # to an object, which can be printed later at demand. nnd_oversize <- nndiagram_oversize(input=3, hidden=c(6,6,6), size.cutoff=5, suppress=TRUE) cat(paste(nnd_oversize,"\n"))