knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

deepviz

The goal of deepviz is to visualize (simple) neural network architectures.

Travis build status

Installation

devtools::install_github("andrie/deepviz")

Load the packages

library(deepviz)
library(magrittr)

plot_model() with sequential models

Create a model

require(keras)
model <- keras_model_sequential() %>%
  layer_dense(10, input_shape = 4) %>%
  layer_dense(2, activation = "sigmoid")

Plot the model

model %>% plot_model()
htm_file <- tempfile(fileext = ".html")

model %>% plot_model() %>% 
  htmlwidgets::saveWidget(htm_file)

webshot::webshot(htm_file, file = tempfile(fileext = ".png"))

Add some more layers and plot

model <- keras_model_sequential() %>%
  layer_conv_2d(filters = 16, kernel_size = c(3, 3)) %>% 
  layer_max_pooling_2d() %>% 
  layer_dense(10, input_shape = 4) %>%
  layer_dense(10, input_shape = 4) %>%
  layer_dropout(0.25) %>% 
  layer_dense(2, activation = "sigmoid")

model %>% plot_model()
model %>% plot_model()
htm_file <- tempfile(fileext = ".html")

model %>% plot_model() %>% 
  htmlwidgets::saveWidget(htm_file)

webshot::webshot(htm_file, file = tempfile(fileext = ".png"))

plot_model() with network models

Construct a network model using the keras function API, using the example from https://keras.rstudio.com/articles/functional_api.html

model <- local({
  main_input <- layer_input(shape = c(100), dtype = 'int32', name = 'main_input')

  lstm_out <- main_input %>%
    layer_embedding(input_dim = 10000, output_dim = 512, input_length = 100) %>%
    layer_lstm(units = 32)

  auxiliary_output <- lstm_out %>%
    layer_dense(units = 1, activation = 'sigmoid', name = 'aux_output')

  auxiliary_input <- layer_input(shape = c(5), name = 'aux_input')

  main_output <- layer_concatenate(c(lstm_out, auxiliary_input)) %>%
    layer_dense(units = 64, activation = 'relu') %>%
    layer_dense(units = 64, activation = 'relu') %>%
    layer_dense(units = 64, activation = 'relu') %>%
    layer_dense(units = 1, activation = 'sigmoid', name = 'main_output')

  keras_model(
    inputs = c(main_input, auxiliary_input),
    outputs = c(main_output, auxiliary_output)
  )
})

model

Plot the model

model %>% plot_model()
htm_file <- tempfile(fileext = ".html")

model %>% plot_model() %>% 
  htmlwidgets::saveWidget(htm_file)

webshot::webshot(htm_file, file = tempfile(fileext = ".png"))

plot_deepviz()

Logistic regression:

c(4, 1) %>% 
  plot_deepviz()

One hidden layer:

c(4, 10, 1) %>% 
  plot_deepviz()

A multi-layer perceptron (two hidden layers):

c(4, 10, 10, 1) %>% 
  plot_deepviz()

Multi-class classification

c(4, 10, 10, 3) %>% 
  plot_deepviz()


andrie/deepviz documentation built on May 9, 2019, 3:58 a.m.