问题
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?
回答1:
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
- It opened graphical devices but didn't request them to turn on recording (via
dev.control(displaylist = 'enable')
); - 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, e.g.
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, e.g. 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.
回答2:
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
.
来源:https://stackoverflow.com/questions/43795530/knitr-and-plotting-neural-networks