簡體   English   中英

編織和繪制神經網絡

[英]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信息。 基本上如果你沒有指定repneuralnet:::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會話,打開新的(默認情況下, 屏幕外 )設備可能沒什么意義。

我認為問題是對於類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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM