nndiagram | R Documentation |
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
)
input |
a positive integer that specifies the number of input neurons. |
hidden |
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: ccfang@uni-bonn.de
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"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.