gpt4 book ai didi

r - knitr 和绘制神经网络

转载 作者:行者123 更新时间:2023-12-02 04:27:27 27 4
gpt4 key购买 nike

我正在尝试绘制一些神经网络输出,但没有得到任何结果。绘制诸如 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)
```

我没有得到任何输出。同一文件可以很好地绘制其他内容。

最佳答案

本期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,
....
}
}

我使用上面的 .... 省略了不相关的信息。基本上,如果您不指定 repneuralnet:::plot.nn 将打开图形设备来绘制绘图。这会破坏 knitr 的图形记录,因为

  1. 它打开了图形设备,但没有请求它们打开录制功能(通过 dev.control(displaylist = 'enable'));
  2. knitr默认使用自己的设备来录制图形;如果用户打开新设备,则不能保证knitr可以保存新绘图。一般来说,我不鼓励在绘图函数中操作图形设备。

我不是neuralnet包的作者,但我建议作者放弃dev.new(),或者至少让它成为有条件的,例如

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

我猜 dev.new() 调用的目的可能是在新窗口中显示绘图,但实际上并不能保证用户可以看到窗口。交互式 R session 的默认图形设备是窗口/屏幕设备(如果可用,例如 x11()quartz()),但默认设备很可能已被用户或软件包作者更改。

我建议使用条件interactive(),因为对于非交互式 R session ,打开新的可能没有多大意义(默认情况下,离屏) )设备。

关于r - knitr 和绘制神经网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43795530/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com