fawda123 / NeuralNetTools

R package of generic neural network tools
https://fawda123.github.io/NeuralNetTools/
Creative Commons Zero v1.0 Universal
71 stars 20 forks source link

plotnet labels cut-off + DiagrammeR + Keras #20

Closed vnijs closed 6 years ago

vnijs commented 6 years ago

plotnet is a great function but sometimes the labels are cut-off (see example below). Reducing cex_val helps but it would be nice of the margins / padding could be adjusted as well. I tried using R's par(mar = c(0, 6, 0, 6)) but that doesn't seem to have the desired effect? Any suggestions?

image

Question 1: Have you ever considered using Diagammer for plotnet plots? In addition to making great looking pots, this would also allow for things like tooltips that could provide additional information about the model or nodes (e.g., activation function used).

Question 2: Any idea if it would feasible to generate network plots for models trained using Keras?

fawda123 commented 6 years ago

@vnijs Sorry for not getting to this sooner. I just added an argument on the dev branch that changes padding on the x-axis. The default value is one, values less than one will increase padding and values greater than one will decrease.

library(NeuralNetTools)

# create neural network
library(nnet)

names(neuraldat)[3] <- 'really_long_name_its_too_long'
mod <- nnet(Y1 ~ really_long_name_its_too_long + X2 + X3, data = neuraldat, size = 10)

# plot
par(mar = numeric(4))
plotnet(mod, pad_x = 0.3)

rplot

For your other questions, I had considered using some proper network packages for plotting neural networks. Unfortunately I made the original plotting function a few years ago before these newer packages were popular. I'd like to implement something with more functionality but there hasn't been much demand relative to how much additional work it would take.

I'm not familiar with Keras but there is a way to plot networks manually with plotnet if you know the network weights. This is shown in the examples in the help file. Alternatively, I could write a method for plotting Keras objects if there is such a thing through the R interface, e.g., a neural network object of class Keras with weights as an attribute. If you think this is possible you can open another issue and I'll investigate.

vnijs commented 6 years ago

Thanks @fawda123. Very useful!