[英]knitr and plotting neural networks
我试图绘制一些神经网络输出,但我没有得到任何结果。 绘制正常的东西,如plot(iris)
工作正常,但有一些关于neuralnetwork()
对象似乎没有以相同的方式绘制。
我的文件看起来像:
---
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)
```
我没有输出。 同样的文件绘制其他东西就好了。 有什么想法吗?
之前已在rmarkdown存储库中报告并回答了此问题。 在这里,我只是想解释为什么它不起作用的技术原因。
从帮助页面?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.
从源代码(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,
....
}
}
我在上面使用....
省略了irrelvant信息。 基本上如果你没有指定rep
, neuralnet:::plot.nn
将打开新的图形设备来绘制图。 这将打破knitr的图形记录,因为
dev.control(displaylist = 'enable')
); 我不是neuralnet包的作者,但我建议作者降dev.new()
或者至少使其有条件的,如
if (interactive()) grDevices::dev.new()
我想dev.new()
调用的意图可能是在新窗口中显示图,但实际上并不能保证用户可以看到窗口。 交互式 R会话的默认图形设备是窗口/屏幕设备(如果可用,例如x11()
或quartz()
),但很可能默认设备已被用户或包作者更改。
我建议条件为interactive()
因为对于非交互式R会话,打开新的(默认情况下, 屏幕外 )设备可能没什么意义。
我认为问题是对于类nn
对象, plot
使用参数rep
。 如果未定义rep
,则所有重复都绘制在单独的窗口中(当在RMarkdown之外运行时)。 如果rep = "best"
,则仅生成具有最小误差的图。 所以这应该工作:
```{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")
```
见?plot.nn
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.