编织和绘图神经网络 [英] knitr and plotting neural networks

查看:106
本文介绍了编织和绘图神经网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制一些神经网络输出,但没有得到任何结果.绘制plot(iris)之类的常规内容可以很好地工作,但是neuralnetwork()对象的某些地方似乎并没有以相同的方式绘制.

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.

我的文件如下:

---
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?

推荐答案

已报告此问题 ,然后在 rmarkdown 存储库中回答.在这里,我仅试图解释其无效的技术原因.

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.

从帮助页面?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):

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

我已经使用上面的....省略了无关紧要的信息.基本上,如果您未指定rep,则neuralnet:::plot.nn将打开 new 图形设备以绘制图.这将破坏针织机的图形记录,因为

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

  1. 它打开了图形设备,但没有要求它们打开录音(通过dev.control(displaylist = 'enable'));
  2. 编织器默认情况下使用其自己的设备来记录图形;如果用户打开了新设备,则不能保证 knitr 可以保存新图.通常,我不建议在绘图功能中使用图形设备.
  1. It opened graphical devices but didn't request them to turn on recording (via dev.control(displaylist = 'enable'));
  2. 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.

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

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()

我猜想dev.new()调用的目的可能是在新窗口中显示图,但实际上并不能保证用户可以看到窗口. interactive R会话的默认图形设备是窗口/屏幕设备(如果可用,例如x11()quartz()),但是用户很可能已更改了默认设备.或打包作者.

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.

我建议使用条件interactive(),因为对于非交互式R会话,打开新(默认为屏幕外)设备可能没有多大意义.

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.

这篇关于编织和绘图神经网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆