简体   繁体   中英

knitr and plotting neural networks

I'm trying to plot some neural network outputs, but I'm not getting any result. Plotting normal stuff like plot(iris) works fine, but there's something about the neuralnetwork() object that doesn't seem to plot the same way.

My file looks like:

---
title: "stack"
author: "stack"
date: "today"
output:
  pdf_document: default
  html_document: default
---


```{r}
library(neuralnet)
AND <- c(rep(0,3),1)
binary.data <- data.frame(expand.grid(c(0,1), c(0,1)), AND)
net <- neuralnet(AND~Var1+Var2,  binary.data, hidden=0,err.fct="ce", linear.output=FALSE)
plot(net)
```

And I get no output. Same file plots other stuff just fine. Any thoughts?

This issue has been reported and answered before in the rmarkdown repository. Here I'm only trying to explain the technical reason why it didn't work.

From the help page ?neuralnet::plot.nn :

Usage

    ## S3 method for class 'nn'
    plot(x, rep = NULL, x.entry = NULL, x.out = NULL,
        ....


Arguments

  ...

  rep   repetition of the neural network. If rep="best", the repetition
        with the smallest error will be plotted. If not stated all repetitions
        will be plotted, each in a separate window.

From the source code (v1.33):

> neuralnet:::plot.nn
function (x, rep = NULL, x.entry = NULL, x.out = NULL, radius = 0.15, 
    .... 
{
    ....
    if (is.null(rep)) {
        for (i in 1:length(net$weights)) {
            ....
            grDevices::dev.new()
            plot.nn(net, rep = i, 
                    ....
        }
    }

I have omitted the irrelvant information using .... above. Basically if you do not specify rep , neuralnet:::plot.nn will open new graphics devices to draw plots. That will break knitr 's graphics recording, because

  1. It opened graphical devices but didn't request them to turn on recording (via dev.control(displaylist = 'enable') );
  2. knitr uses its own device to record graphics by default; if users opened new devices, there is no guarantee that new plots can be saved by knitr . In general, I'd discourage manipulating graphical devices in plotting functions.

I'm not an author of the neuralnet package, but I'd suggest the authors drop dev.new() , or at least make it conditional, eg

if (interactive()) grDevices::dev.new()

I guess the intention of the dev.new() call was probably to show plots in new windows, but there is really no guarantee that users can see windows. The default graphical device of an interactive R session is a window/screen device (if available, eg x11() or quartz() ), but it is quite possible that the default device has been changed by users or package authors.

I suggest the condition interactive() because for a non-interactive R session, it probably does not make much sense to open new (by default, off-screen ) devices.

I think the issue is that for objects of class nn , plot uses a parameter rep . If rep is not defined, all repetitions are plotted in separate windows (when run outside of RMarkdown). If rep = "best" , only the plot with the smallest error is generated. So this should work:

```{r}
library(neuralnet)
AND <- c(rep(0,3),1)
binary.data <- data.frame(expand.grid(c(0,1), c(0,1)), AND)
net <- neuralnet(AND~Var1+Var2,  binary.data, hidden=0,err.fct="ce", 
linear.output=FALSE)
plot(net, rep = "best")
```

See ?plot.nn .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM