使用神经网络包保存神经网络图时遇到问题 - R

2024-06-23

我正在使用neuralnetR 中的包,但是将绘图保存到磁盘时遇到问题。

data(iris)
attach(iris)
library(neuralnet)

nn <- neuralnet(as.numeric(Species) ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data = iris)

png("test.png")
plot(nn)
dev.off()

已尝试参考manual http://cran.r-project.org/web/packages/neuralnet/neuralnet.pdf,在该部分plot.nn()它说:

file      a character string naming the plot to write to. If not stated, 
the plot will not be saved.

然而,执行以下操作不会保存有效的绘图:

png("test.png")
plot(nn, file = "test.png")
dev.off()

平台:

Mac OSX 10.7.3

R版本:

platform       x86_64-apple-darwin11.3.0    
arch           x86_64                       
os             darwin11.3.0                 
system         x86_64, darwin11.3.0         
status                                      
major          2                            
minor          15.1                         
year           2012                         
month          06                           
day            22                           
svn rev        59600                        
language       R                            
version.string R version 2.15.1 (2012-06-22)
nickname       Roasted Marshmallows  

注意:R是通过macports安装的


与 Frauke Guenther(维护者)交谈后neuralnet),她解释说问题在于neuralnet能够同时绘制多个神经网络。

The plot(..., file = "")应该已将文件保存到磁盘,但似乎无法正常运行,很快就会对包本身进行修复,但与此同时,她好心地提供了一个快速的解决方案plot.nn()修复允许将绘图保存到磁盘的问题。

plot.nn <-
function (x, rep = NULL, x.entry = NULL, x.out = NULL, radius = 0.15, 
    arrow.length = 0.2, intercept = TRUE, intercept.factor = 0.4, 
    information = TRUE, information.pos = 0.1, col.entry.synapse = "black", 
    col.entry = "black", col.hidden = "black", col.hidden.synapse = "black", 
    col.out = "black", col.out.synapse = "black", col.intercept = "blue", 
    fontsize = 12, dimension = 6, show.weights = TRUE, file = NULL, 
    ...) 
{
    net <- x
    if (is.null(net$weights)) 
        stop("weights were not calculated")
    if (!is.null(file) && !is.character(file)) 
        stop("'file' must be a string")
    if (is.null(rep)) {
        for (i in 1:length(net$weights)) {
            if (!is.null(file)) 
                file.rep <- paste(file, ".", i, sep = "")
            else file.rep <- NULL
            #dev.new()
            plot.nn(net, rep = i, x.entry, x.out, radius, arrow.length, 
                intercept, intercept.factor, information, information.pos, 
                col.entry.synapse, col.entry, col.hidden, col.hidden.synapse, 
                col.out, col.out.synapse, col.intercept, fontsize, 
                dimension, show.weights, file.rep, ...)
        }
    }
    else {
        if (is.character(file) && file.exists(file)) 
            stop(sprintf("%s already exists", sQuote(file)))
        result.matrix <- t(net$result.matrix)
        if (rep == "best") 
            rep <- as.integer(which.min(result.matrix[, "error"]))
        if (rep > length(net$weights)) 
            stop("'rep' does not exist")
        weights <- net$weights[[rep]]
        if (is.null(x.entry)) 
            x.entry <- 0.5 - (arrow.length/2) * length(weights)
        if (is.null(x.out)) 
            x.out <- 0.5 + (arrow.length/2) * length(weights)
        width <- max(x.out - x.entry + 0.2, 0.8) * 8
        radius <- radius/dimension
        entry.label <- net$model.list$variables
        out.label <- net$model.list$response
        neuron.count <- array(0, length(weights) + 1)
        neuron.count[1] <- nrow(weights[[1]]) - 1
        neuron.count[2] <- ncol(weights[[1]])
        x.position <- array(0, length(weights) + 1)
        x.position[1] <- x.entry
        x.position[length(weights) + 1] <- x.out
        if (length(weights) > 1) 
            for (i in 2:length(weights)) {
                neuron.count[i + 1] <- ncol(weights[[i]])
                x.position[i] <- x.entry + (i - 1) * (x.out - 
                  x.entry)/length(weights)
            }
        y.step <- 1/(neuron.count + 1)
        y.position <- array(0, length(weights) + 1)
        y.intercept <- 1 - 2 * radius
        information.pos <- min(min(y.step) - 0.1, 0.2)
        if (length(entry.label) != neuron.count[1]) {
            if (length(entry.label) < neuron.count[1]) {
                tmp <- NULL
                for (i in 1:(neuron.count[1] - length(entry.label))) {
                  tmp <- c(tmp, "no name")
                }
                entry.label <- c(entry.label, tmp)
            }
        }
        if (length(out.label) != neuron.count[length(neuron.count)]) {
            if (length(out.label) < neuron.count[length(neuron.count)]) {
                tmp <- NULL
                for (i in 1:(neuron.count[length(neuron.count)] - 
                  length(out.label))) {
                  tmp <- c(tmp, "no name")
                }
                out.label <- c(out.label, tmp)
            }
        }
        grid.newpage()
        pushViewport(viewport(name = "plot.area", width = unit(dimension, 
            "inches"), height = unit(dimension, "inches")))
        for (k in 1:length(weights)) {
            for (i in 1:neuron.count[k]) {
                y.position[k] <- y.position[k] + y.step[k]
                y.tmp <- 0
                for (j in 1:neuron.count[k + 1]) {
                  y.tmp <- y.tmp + y.step[k + 1]
                  result <- calculate.delta(c(x.position[k], 
                    x.position[k + 1]), c(y.position[k], y.tmp), 
                    radius)
                  x <- c(x.position[k], x.position[k + 1] - result[1])
                  y <- c(y.position[k], y.tmp + result[2])
                  grid.lines(x = x, y = y, arrow = arrow(length = unit(0.15, 
                    "cm"), type = "closed"), gp = gpar(fill = col.hidden.synapse, 
                    col = col.hidden.synapse, ...))
                  if (show.weights) 
                    draw.text(label = weights[[k]][neuron.count[k] - 
                      i + 2, neuron.count[k + 1] - j + 1], x = c(x.position[k], 
                      x.position[k + 1]), y = c(y.position[k], 
                      y.tmp), xy.null = 1.25 * result, color = col.hidden.synapse, 
                      fontsize = fontsize - 2, ...)
                }
                if (k == 1) {
                  grid.lines(x = c((x.position[1] - arrow.length), 
                    x.position[1] - radius), y = y.position[k], 
                    arrow = arrow(length = unit(0.15, "cm"), 
                      type = "closed"), gp = gpar(fill = col.entry.synapse, 
                      col = col.entry.synapse, ...))
                  draw.text(label = entry.label[(neuron.count[1] + 
                    1) - i], x = c((x.position - arrow.length), 
                    x.position[1] - radius), y = c(y.position[k], 
                    y.position[k]), xy.null = c(0, 0), color = col.entry.synapse, 
                    fontsize = fontsize, ...)
                  grid.circle(x = x.position[k], y = y.position[k], 
                    r = radius, gp = gpar(fill = "white", col = col.entry, 
                      ...))
                }
                else {
                  grid.circle(x = x.position[k], y = y.position[k], 
                    r = radius, gp = gpar(fill = "white", col = col.hidden, 
                      ...))
                }
            }
        }
        out <- length(neuron.count)
        for (i in 1:neuron.count[out]) {
            y.position[out] <- y.position[out] + y.step[out]
            grid.lines(x = c(x.position[out] + radius, x.position[out] + 
                arrow.length), y = y.position[out], arrow = arrow(length = unit(0.15, 
                "cm"), type = "closed"), gp = gpar(fill = col.out.synapse, 
                col = col.out.synapse, ...))
            draw.text(label = out.label[(neuron.count[out] + 
                1) - i], x = c((x.position[out] + radius), x.position[out] + 
                arrow.length), y = c(y.position[out], y.position[out]), 
                xy.null = c(0, 0), color = col.out.synapse, fontsize = fontsize, 
                ...)
            grid.circle(x = x.position[out], y = y.position[out], 
                r = radius, gp = gpar(fill = "white", col = col.out, 
                  ...))
        }
        if (intercept) {
            for (k in 1:length(weights)) {
                y.tmp <- 0
                x.intercept <- (x.position[k + 1] - x.position[k]) * 
                  intercept.factor + x.position[k]
                for (i in 1:neuron.count[k + 1]) {
                  y.tmp <- y.tmp + y.step[k + 1]
                  result <- calculate.delta(c(x.intercept, x.position[k + 
                    1]), c(y.intercept, y.tmp), radius)
                  x <- c(x.intercept, x.position[k + 1] - result[1])
                  y <- c(y.intercept, y.tmp + result[2])
                  grid.lines(x = x, y = y, arrow = arrow(length = unit(0.15, 
                    "cm"), type = "closed"), gp = gpar(fill = col.intercept, 
                    col = col.intercept, ...))
                  xy.null <- cbind(x.position[k + 1] - x.intercept - 
                    2 * result[1], -(y.tmp - y.intercept + 2 * 
                    result[2]))
                  if (show.weights) 
                    draw.text(label = weights[[k]][1, neuron.count[k + 
                      1] - i + 1], x = c(x.intercept, x.position[k + 
                      1]), y = c(y.intercept, y.tmp), xy.null = xy.null, 
                      color = col.intercept, alignment = c("right", 
                        "bottom"), fontsize = fontsize - 2, ...)
                }
                grid.circle(x = x.intercept, y = y.intercept, 
                  r = radius, gp = gpar(fill = "white", col = col.intercept, 
                    ...))
                grid.text(1, x = x.intercept, y = y.intercept, 
                  gp = gpar(col = col.intercept, ...))
            }
        }
        if (information) 
            grid.text(paste("Error: ", round(result.matrix[rep, 
                "error"], 6), "   Steps: ", result.matrix[rep, 
                "steps"], sep = ""), x = 0.5, y = information.pos, 
                just = "bottom", gp = gpar(fontsize = fontsize + 
                  2, ...))
        popViewport()
        if (!is.null(file)) {
            weight.plot <- recordPlot()
            save(weight.plot, file = file)
        }
    }
}
calculate.delta <-
function (x, y, r) 
{
    delta.x <- x[2] - x[1]
    delta.y <- y[2] - y[1]
    x.null <- r/sqrt(delta.x^2 + delta.y^2) * delta.x
    if (y[1] < y[2]) 
        y.null <- -sqrt(r^2 - x.null^2)
    else if (y[1] > y[2]) 
        y.null <- sqrt(r^2 - x.null^2)
    else y.null <- 0
    c(x.null, y.null)
}
draw.text <-
function (label, x, y, xy.null = c(0, 0), color, alignment = c("left", 
    "bottom"), ...) 
{
    x.label <- x[1] + xy.null[1]
    y.label <- y[1] - xy.null[2]
    x.delta <- x[2] - x[1]
    y.delta <- y[2] - y[1]
    angle = atan(y.delta/x.delta) * (180/pi)
    if (angle < 0) 
        angle <- angle + 0
    else if (angle > 0) 
        angle <- angle - 0
    if (is.numeric(label)) 
        label <- round(label, 5)
    pushViewport(viewport(x = x.label, y = y.label, width = 0, 
        height = , angle = angle, name = "vp1", just = alignment))
    grid.text(label, x = 0, y = unit(0.75, "mm"), just = alignment, 
        gp = gpar(col = color, ...))
    popViewport()
}

将上面的代码粘贴到 R 交互式提示中,当您希望保存绘图时,请执行以下操作:

png("test.png")
plot(nn)
dev.off()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用神经网络包保存神经网络图时遇到问题 - R 的相关文章

  • 修改x轴刻度标签

    我正在尝试更改由生成的箱线图的 x 轴刻度标签ggplot2 x 轴是一个分类变量 HabFac 我想要的是将其刻度更改为 6 个化学品 A E 下面是我的代码 raw data read table Read data p TT ggpl
  • 在 dfm 中查找非英语标记并将其删除

    在 dfm 中如何检测非英语单词并将其删除 dftest lt data frame id 1 3 text c Holla this is a spanish word English online here Bonjour commen
  • 使用 ggplot2 进行分面 qqplots

    假设我有以下数据 datapoints1 data frame categ c rep 1 n rep 2 n vals1 c rt n 1 2 rnorm n 3 4 datapoints2 data frame categ c rep
  • 随着数据帧变大,如何防止 rbind() 变得非常慢?

    我有一个只有 1 行的数据框 为此 我开始使用 rbind 添加行 df mydataframe with only one row for i in 1 20000 df lt rbind df newrow 随着我的成长 这变得非常缓慢
  • R:异步并行lapply

    迄今为止我发现使用并行的最简单方法lapply在 R 中是通过以下示例代码 library parallel library pbapply cl lt makeCluster 10 clusterExport cl cl clusterE
  • 将outer() 与多变量函数一起使用

    假设你有一个函数f lt function x y z 您将如何向一个参数传递一个常量 但让其他参数发生变化 换句话说 我想做这样的事情 output lt outer x y f x y z 2 这段代码不会评估 但是有没有办法做到这一点
  • 两个数据框之间逐元素的百分比变化

    我有 2 个数据框 它们具有相同数量的匹配列和行 例如 df 2010 lt data frame col1 c Connecticut Delaware District of Columbia Florida Georgia col2
  • 使用 data.table::fwrite() 写入 .txt 文件 — is.list(x) 不为 TRUE

    我试图替换基本 R 函数write table with data table fwrite 加快写入速度 但该函数抱怨说is list x is not TRUE 我提供的输入有什么问题fwrite 期望 但是write table 默默
  • 按组划分的模态值(最常见)的简明 R data.table 语法

    用于查找每个 id 最常见类别的高效且优雅的 data table 语法是什么 我保留一个指示 NA 位置的布尔向量 用于其他目的 dt data table id rep 1 2 7 category c x y NA print dt
  • 更改分配新变量的默认环境

    我经常想在全局环境下的一个环境中创建很多变量 这可以通过以下方式轻松完成envir论证sys source 如果由正在获取的文件创建的所有变量都应该进入单个环境 但我通常使用创建变量集的文件 一组应该进入一个环境 另一组应该进入另一个环境
  • 在 R 中组合/合并列

    我可能在这个问题上遗漏了一些很容易的东西 我在任何地方都找不到正确的答案 我真的需要继续前进 所以我过度简化了我的数据 eventID lt c 2 4 Time lt c 09 32 09 43 df1 lt data frame eve
  • R 绘制一些 unicode 字符,但不绘制其他字符

    我们的系统管理员刚刚将我们的操作系统升级到 SLES12SP1 我重新安装了 Rv3 2 3 并尝试绘图 我用cairo pdf并尝试绘制 x 标签为的图 u0298即太阳能符号 但它不起作用 标签只是空白 例如 cairo pdf Rpl
  • 求R中3列中每一行的最大值

    我需要计算 3 列中每行的最大值 一个表可以是 x c 1 2 3 4 5 y c 2 3 3 1 1 z c 4 3 2 1 1 df lt data frame x y z 我需要得到 x y z max 1 1 2 4 4 2 2 3
  • 按组渐进串联列[重复]

    这个问题在这里已经有答案了 假设我有这个输入 ID date 1 date 2 str 1 1 2010 07 04 2008 01 20 A 2 2 2015 07 01 2011 08 31 C 3 3 2015 03 06 2013
  • 我们可以有更多错误(消息)吗?

    在 R 中 如果函数使用变量 有没有办法弹出错误消息 未在函数体内声明 即我希望有人标记这种类型的函数 aha lt function p return p n 看 如果某个地方碰巧有一个 n 变量 aha p 2 会给我一个 答案 因为
  • 在ggplot2中设置base_size时重叠轴标签

    我正在改变base size via theme set 当我在屏幕上查看结果图时 它看起来很棒 但是 当我将其另存为 pdf 时 x 轴标签有点太接近轴编号 一件小事 theme set theme bw base size 9 不会造成
  • 使用同一变量的多个子集创建新数据框

    我想创建一个新的数据框 其中的列是由不同变量分割的同一变量的子集 例如 我想创建一个新的变量子集 b 其中列由不同变量 year 的子集分割 set seed 88 df lt data frame year rep 1996 1998 3
  • 通过环境.yml 文件使用 conda 安装 R 包

    通常我会创建 conda 环境 例如 conda env create f environment yml conda activate env name 通常我使用 Python 工作 典型的environment yml简单的文件可能看
  • 在 ggplot2 中隐藏单个几何图例

    我将相同的变量 颜色 映射到两个不同几何图形中的颜色 我希望它们要么出现在单独的图例中 DHJ 和 EFI 要么最好完全跳过第二个图例 对于 E F 和 I 目前 R 将两者混合在一起 并给我一个图例 其中按字母顺序列出了 DEFHIJ 所
  • 在 R 中提取栅格的最快方法(提高我的可重现代码的时间)

    我想知道我是否已最大化提取栅格中某个点周围缓冲区域平均值的速度 本地的性能可以进一步提高吗 I use parallel mclapply已经 我知道我可以通过在集群上设置和运行它来获得进一步的收益 使用集群或获得更多的CPU不是我正在寻找

随机推荐

  • C 中的分号和逗号 [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 为什么这些程序可以工作 为什么我没有
  • 如何动态更改 WPF 中 DataGridTextColumn 绑定上的转换器?

    我真的不明白 WPF 和 XAML 并且继承了一些写得很糟糕的代码 所以我可能会扼杀它 但这里是 我继承了一个绑定到 Person 对象列表 在代码后面 的 DataGrid 其中必要的 DataGridTextColumn 在 XAML
  • 延迟连接可枚举的列表

    我想编写一个类似于 List concat 1 的函数 它接受可枚举的列表并将连接的列表作为连续流发出 它会像这样工作 iex gt 1 3 gt Stream map 1 gt Enum to list 1 2 3 iex gt 1 3
  • 不传播的原因是什么(es 6传播运算符)javascript错误对象[重复]

    这个问题在这里已经有答案了 我正在努力传播 JavascriptError对象 标准内置对象 我得到空对象作为输出 let error new Error error console log error output 没有传播Error对象
  • laravel 根据条件隐藏字段[重复]

    这个问题在这里已经有答案了 我有一个名为 Vote actions 的数据库和模型 如下所示 id group id user id 动作类型 匿名 布尔值 用户可以要求匿名 这将使布尔值变为 true 如果是这种情况 我想将返回模型中的
  • iOS 隐藏默认键盘并打开自定义键盘

    我有一个UITextview 当用户点击UITextview我需要隐藏默认键盘 为此我所做的 myTextView setEditable NO 所以键盘没有显示 这里我创建了一个自定义视图UIButton 我需要展示这个UIView当用户
  • NHibernate:多对一和惰性

    关于 NHibernate 1 2 webapp C aspnet 3 5 和延迟加载 我不明白 也找不到一些明确的答案 多对一是否支持延迟加载 假设我们有一些Person对象和一个Squeletton目的 这两个对象都很大 具有很多属性
  • ASP.NET MVC FileResult 正在损坏文件

    我一直在尝试让我的 ASP NET MVC 网站将一些数据导出为 Excel 文件 几个小时以来 我一直以为 NPOI 只是在生产垃圾 所以我转向了 EPPlus 我在 LINQPad 中对其进行了测试 它创建了一个正常工作的 XLSX 文
  • 从 Python 脚本使用 POST 发送文件

    有没有办法从 Python 脚本使用 POST 发送文件 From https requests readthedocs io en latest user quickstart post a multipart encoded file
  • React Native NetInfo 总是返回离线状态

    我目前正在尝试在我的 React Native 应用程序中实现一些功能 如果设备离线 我将使用本地存储的信息 如果设备在线 则执行提取 读完这篇文章后我使用了 NetInfo当网络关闭时 如何处理 React Native 中的网络故障 h
  • 我想制作具有3D效果的幻灯片菜单,如下图所示

    我可以使用此代码制作滑块 但我不知道如何制作3D import android os Bundle import android support v4 app Fragment import android support v7 app A
  • 评论投票数据库结构的最佳实践

    我正在开发一个 PHP 应用程序 它有几个可以评论的对象 每个评论都可以进行投票 用户可以给它 1 或 1 如 Digg 或 Reddit 现在我计划建立一个 投票 表 其中包含 user id 及其投票信息 这似乎工作正常 问题是 每个对
  • jQuery 克隆() 问题

    这是我完整的 html 日期和时间表 div class addmore box date div class row div class col xs 6 col sm 4 col md 4 div div div
  • AudioSet 和 Tensorflow 理解

    With AudioSet https research google com audioset index html发布并为那些进行合理研究分析的人提供了一个全新的研究领域 最近几天我一直在努力深入研究如何分析和解码这些数据 数据在 tf
  • Rails 3.2.3 - Bundler 找不到 gem“railties”的兼容版本

    我一直在关注 Michael Hartl 的更新版本 Ruby on Rails 教程 http ruby railstutorial org ruby on rails tutorial book fig 3aheroku app 31我
  • 重置 JDBC Kafka 连接器以从头开始提取行?

    Kafka 连接器可以利用主键和时间戳来确定需要处理哪些行 我正在寻找一种重置连接器的方法 以便它从一开始就进行处理 因为要求是在分布式模式下运行 所以最简单的做法是将连接器名称更新为新值 这将提示在 connect offsets 主题中
  • 如何克隆 Angular UI 树中的节点?

    如何克隆 Angular UI 树中所有子节点的节点 现在我使用事件点击 ng click newSubItem this where newSubItem是函数 scope newSubItem function scope var no
  • Xcode 9:如何安装 ios 10 sdk

    鉴于目前 Xcode 9 是测试版 而今天的主要兴趣是了解 iOS 11 这个问题无疑很奇怪 在 Xcode 9 beta 中工作时 有没有办法将 iOS 10 作为基础 sdk Apple 是否需要像在 Xc8 中为以前的操作系统打包 X
  • 如何同时执行 typescript watch 和运行服务器?

    我正在用nodejs开发我的项目 我发现如果我需要编码和测试api 我会运行两个控制台 一个是执行typescript watch 另一个是执行server 我觉得这样太麻烦了我发现 github 上的其他开发人员已经编写了脚本packag
  • 使用神经网络包保存神经网络图时遇到问题 - R

    我正在使用neuralnetR 中的包 但是将绘图保存到磁盘时遇到问题 data iris attach iris library neuralnet nn lt neuralnet as numeric Species Sepal Len