0

I am trying to find a tool to replicate the following neural network graph:

enter image description here

The nodes and edges can be easily drawn with igraph. However, I am not sure whether igraph can be used to replicate the other parts of the figure. Could you please help me?

PaulS
  • 21,159
  • 2
  • 9
  • 26
  • 1
    You're interested in doing this in R specifically, correct? It looks like you could get pretty close with base R's `text` and `arrows` functions, but it would take a lot of knowing the location of various elements. – Dubukay Dec 17 '21 at 17:43
  • Thanks, @Dubukay, yes, I would like to draw the neural network in R or using one of the R packages. It is easy to draw the nodes and the edges with `igraph` (a R package). So, I think that using only pure R, it will be too complicated. – PaulS Dec 17 '21 at 17:54

2 Answers2

2

If you are looking for such a solution like this example: You can find it here: https://hub.packtpub.com/training-and-visualizing-a-neural-network-with-r/

#install.packages("neuralnet")

library(neuralnet)
data(iris)
ind <- sample(2, nrow(iris), replace = TRUE, prob=c(0.7, 0.3))
trainset = iris[ind == 1,] 
testset = iris[ind == 2,]

trainset$setosa = trainset$Species == "setosa"
trainset$virginica = trainset$Species == "virginica"
trainset$versicolor = trainset$Species == "versicolor"

network = neuralnet(versicolor + virginica + setosa~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, trainset, hidden=3)
network
neuralnet(formula = versicolor + virginica + setosa ~ Sepal.Length +     Sepal.Width + Petal.Length + Petal.Width, data = trainset,     hidden = 3)
network$result.matrix
head(network$generalized.weights[[1]])

plot(network)

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
2

I think DiagrammeR can be a nicer variant.

If you want to produce it into R-enviroment. But you need some amount of time to learn this package.

library(DiagrammeR)

grViz("digraph G1 {
  graph [layout=neato overlap = true]     
  I0 [pos='1,3.25!' shape=plaintext label='input layer' fontsize=20]
  I1 [pos='1,2.5!'  style=radial]  
  I2 [pos='1,1!'    style=radial]
  I3 [pos='1,-0.5!' style=radial]
  I7 [pos='0,2.5!'  shape=plaintext label='input 1']
  I8 [pos='0,1!'    shape=plaintext label='input 2']
  I9 [pos='0,-0.5!' shape=plaintext label='input 3']
  H0 [pos='3,3.25!' shape=plaintext label='hidden layer 1' fontsize=20]
  H1 [pos='3,2.5!' style=radial]     
  H2 [pos='3,1!'    style=radial]
  H3 [pos='3,-0.5!' style=radial]
  O0 [pos='5,3.25!' shape=plaintext label='output layer' fontsize=20]
  O1 [pos='5,0!'  style=radial]
  O2 [pos='5,2!'  style=radial] 
  O7 [pos='6,0!' shape=plaintext label='output']
  O8 [pos='6,2!' shape=plaintext label='output']
  
  I7 -> I1 
  I8 -> I2
  I9 -> I3
  I1 -> H1 [label='w=0.8']
  I1 -> {H2 H3}
  I2 -> {H1 H2 H3}
  I3 -> {H1 H2 H3}
  {H1 H2 H3} -> O1
  {H1 H2 H3} -> O2
  O1 -> O7
  O2 -> O8
  
}")

enter image description here

I don't know how to make a 'vertical arrow to arrows', but I think we can do it via label...

manro
  • 3,529
  • 2
  • 9
  • 22
  • Thanks, @manro, once again, for your help! I believe that by using fictitious nodes, one can draw the thetas and the vertical arrows that are missing. – PaulS Dec 18 '21 at 10:01
  • 1
    @PaulSmith No problems :) Look to answer N2 (https://stackoverflow.com/questions/50268757/is-it-possible-to-draw-arrow-from-node-to-nothing), we can draw from "nowhere". If you find out, how to make a vertical arrow from void, I'll glad to see it ;) – manro Dec 18 '21 at 10:33
  • 1
    Yes, @manro, one can draw a vertical arrow from void: Just add these two lines to your code: `O3 [pos='5,1!' shape=plaintext label='']` and `O3 -> O1`. – PaulS Dec 18 '21 at 11:11