Neural network Example

The diagrammeR package may give simple and easy neural network plot.

Example

It can plot neural network that consists of Input layer (x1, x2 and x3), one two hidden layers (y1, y2 and y3), (m1, m2, m3) and two ouptput layers (o1 and o2) ** as follows**.

library(DiagrammeR)

grViz("digraph G1 {
  
  graph [layout=neato overlap = true]     
  
  x0 [pos='1,3.25!' shape=plaintext label='input layer' fontsize=20]
  x1 [pos='1,2.5!'  color=blue width=.75 height=.75]     
  x2 [pos='1,1!'    color=blue width=.75 height=.75]
  x3 [pos='1,-0.5!' color=blue width=.75 height=.75]
  x7 [pos='0,2.5!'  shape=plaintext label='input 1']
  x8 [pos='0,1!'    shape=plaintext label='input 2']
  x9 [pos='0,-0.5!' shape=plaintext label='input 3']
  
  y0 [pos='3,3.25!' shape=plaintext label='hidden layer 1' fontsize=20]
  y1 [pos='3,2.5!'  color=red width=.75 height=.75]     
  y2 [pos='3,1!'    color=red width=.75 height=.75]
  y3 [pos='3,-0.5!' color=red width=.75 height=.75]
  
  m0 [pos='5,3.25!' shape=plaintext label='hidden layer 2' fontsize=20]
  m1 [pos='5,2.5!'  color=red width=.75 height=.75]     
  m2 [pos='5,1!'    color=red width=.75 height=.75]
  m3 [pos='5,-0.5!' color=red width=.75 height=.75]
  
  z0 [pos='7,2.75!' shape=plaintext label='output layer' fontsize=20]
  z1 [pos='7,1.5!' color=green width=.75 height=.75]
  z2 [pos='7,.5!' color=green width=.75 height=.75]
  z7 [pos='8,1.5!' shape=plaintext label='o1']
  z8 [pos='8,.5!' shape=plaintext label='o2']
  
  x7 -> x1
  x8 -> x2
  x9 -> x3
  
  x1 -> {y1 y2 y3}
  x2 -> {y1 y2 y3}
  x3 -> {y1 y2 y3}
  
  y1 -> {m1 m2 m3}
  y2 -> {m1 m2 m3}
  y3 -> {m1 m2 m3}
  
  
  {m1 m2 m3} -> z1
  {m1 m2 m3} -> z2
  
  z1 -> z7
  z2 -> z8
  
}")