2017-05-05 55 views
4

我想绘制一些神经网络输出,但我没有得到任何结果。绘制正常的东西,如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) 
``` 

而且我没有得到任何输出。相同的文件绘制其他的东西很好。有什么想法吗?

+0

试试''netnet :: plot(net)'看看会发生什么。我一直在使用'rbokeh'和'DT',并且在块内有类似的问题,但在另一个窗口的脚本中完全没有问题。它感觉就像某种方式的基本功能是如此。这可能不是你正在发生的事情,但它为我工作,值得一试。 – sconfluentus

+0

最初我认为问题是没有'neuralnet :: plot',但我看到运行大块而不是编织会产生一个阴谋。奇怪的确如此。 – neilfws

+1

此前已有报道:https://github.com/rstudio/rmarkdown/issues/856 –

回答

7

此问题has been reported并在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的图形记录,因为

  1. 它打开图形设备,但没有要求他们打开记录(通过dev.control(displaylist = 'enable'));
  2. knitr默认使用自己的设备记录图形;如果用户打开新设备,则不能保证可以通过knitr节省新的地块。一般来说,我不打算在绘图功能中操纵图形设备。

我不是neuralnet包的作者,但我建议作者降dev.new(),或者至少使其有条件的,例如

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

我猜dev.new()调用的意图很可能显示在新窗口中地块,但实在是没有保证用户可以看到窗口。 R会话的默认图形设备是窗口/屏幕设备(如果可用,例如x11()quartz()),但用户或程序包作者更改默认设备的可能性很大。

我建议条件interactive(),因为对于非交互式R会话,打开新的(默认情况下,off-screen)设备可能没有多大意义。

1

我认为问题是对于类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