【问题标题】:R neuralNet: "non-conformable arguments"R 神经网络:“不一致的论点”
【发布时间】:2013-07-07 05:36:00
【问题描述】:

啊!尝试使用我的神经网络 compute 时,我不断收到以下错误:

> net.compute <- compute(net, matrix.train2)
Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments

我不知道问题出在哪里。下面我将为您提供来自我的矩阵的示例数据和格式,然后我将向您展示我正在尝试运行的代码。

  • matrix.train1 用于训练网络

    > matrix.train1
        (Intercept) survived pclass sexmale    age sibsp parch     fare embarkedC embarkedQ embarkedS
    1             1        0      3       1  22.00     1     0   7.2500         0         0             1
    2             1        1      1       0  38.00     1     0  71.2833         1         0         0
    3             1        1      3       0  26.00     0     0   7.9250         0         0         1
    4             1        1      1       0  35.00     1     0  53.1000         0         0         1
    5             1        0      3       1  35.00     0     0   8.0500         0         0         1
    6             1        0      3       1 999.00     0     0   8.4583         0         1         0
    7             1        0      1       1  54.00     0     0  51.8625         0         0         1
    8             1        0      3       1   2.00     3     1  21.0750         0         0         1
    9             1        1      3       0  27.00     0     2  11.1333         0         0         1
    10            1        1      2       0  14.00     1     0  30.0708         1         0         0
    11            1        1      3       0   4.00     1     1  16.7000         0         0         1
    
  • matrix.train2 是用于测试模型的训练数据切片

    > matrix.train2
        (Intercept) pclass sexmale    age sibsp parch     fare embarkedC embarkedQ embarkedS
    1             1      1       1  49.00     1     1 110.8833         1         0         0
    2             1      3       1  42.00     0     0   7.6500         0         0         1
    3             1      1       0  18.00     1     0 227.5250         1         0         0
    4             1      1       1  35.00     0     0  26.2875         0         0         1
    5             1      3       0  18.00     0     1  14.4542         1         0         0
    6             1      3       1  25.00     0     0   7.7417         0         1         0
    7             1      3       1  26.00     1     0   7.8542         0         0         1
    8             1      2       1  39.00     0     0  26.0000         0         0         1
    9             1      2       0  45.00     0     0  13.5000         0         0         1
    10            1      1       1  42.00     0     0  26.2875         0         0         1
    11            1      1       0  22.00     0     0 151.5500         0         0         1
    

两个矩阵之间唯一真正的区别是matrix.train2 不包含survived 列。

这是我尝试运行的 R 代码:

#Build a matrix from training data 
matrix.train1 <- model.matrix(
  ~ survived + pclass + sex + age + sibsp + parch + fare + embarked, 
  data=train1
)

library(neuralnet)

#Train the neural net
net <- neuralnet(
  survived ~ pclass + sexmale + age + sibsp + parch + fare + embarkedC + 
  embarkedQ + embarkedS, data=matrix.train1, hidden=10, threshold=0.01
)

#Build a matrix from test data
matrix.train2 <- model.matrix(
  ~ pclass + sex + age + sibsp + parch + fare + embarked, 
  data=train2
)

#Apply neural net to test matrix 
net.results <- compute(
  net, matrix.train2
)

Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments

谁能告诉我我在这里做错了什么?

谢谢!


目前基于 cmets 的更新:

  1. 使用“Predicting class for new data using neuralnet”的解决方案似乎不起作用。

    > net.compute <- compute(net, matrix.train2[,1:10])
    Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments
    
  2. 我通过model.matrix 手动将我的train1 和train2 数据帧放入矩阵,因为如果我不这样做,我会收到以下错误:

    > Error in neurons[[i]] %*% weights[[i]] : 
    requires numeric/complex matrix/vector arguments
    

注意:有关我为什么使用model.matrix 的更多详细信息,请参阅以下主题:“Working with neuralnet in R for the first time: get “requires numeric/complex matrix/vector arguments” but don't know how to correct”。

【问题讨论】:

  • 你用的是什么包?
  • 我正在使用neuralnet 包。如果重要的话,我使用的 IDE 是 RStudio。

标签: r machine-learning artificial-intelligence neural-network survival-analysis


【解决方案1】:

您似乎需要删除预测变量。试试这个:

nn_pred<-compute(nn,test[,3:11])

【讨论】:

    【解决方案2】:

    我也尝试过使用神经网络包。我想如果你不是

    net.results <- compute(
      net, matrix.train2
    

    做

    net.result <- compute(  
         net, matrix.train2[,c("pclass",   
         "sexmale", "age", "sibsp", "parch",   
         "fare","embarkedC","embarkedQ","embaredS")])
    

    它应该工作。变量的名称需要与model.list$variables的顺序完全一致,所以你也可以输入

    net.result <- compute(  
         net, matrix.train2[, net.result$model.list$variables])
    

    我希望这会有所帮助。原因是 - 我认为 - 神经网络在找出您的网络中哪些变量以及矩阵中的哪些变量时存在问题......所以您需要明确地匹配它们。

    【讨论】:

    • 谢谢。为我解决了。
    【解决方案3】:

    我没有使用神经网络 ack​​age,但除非它在做一些奇怪的事情,否则你不应该像那样调用 model.matrix。 neuralnet 有一个公式接口,所以它会为你调用model.matrix。你只需要给它训练数据框train1。

    这也适用于预测测试数据。不要创建模型矩阵;只需将数据框 train2 传递给它。

    【讨论】:

    • 我最初并没有调用model.matrix,但是当我直接传入数据框时,我得到了requires numeric/complex matrix/vector arguments 错误。有关我为什么使用 model.matrix 的更多详细信息,请参阅stackoverflow.com/questions/17457028/…
    • 天哪,那神经网络代码是不是很丑陋。这不能回答您的问题,但也许您应该尝试在包nnet 中使用nnet(基本R 安装的一部分)。它只处理单隐藏层 MLP,并使用准牛顿而不是反向传播,但至少它知道如何处理因子。
    • 我所说的“处理因素”是指标准model.frame/model.matrix 方法为您提供的一切:处理缺失值、对数据进行子集化、轻松对新数据进行预测(无需不必担心列的顺序)等等。
    • 尽管这并没有直接回答我的问题,但它确实让我转向了nnet,这反过来又让我看到了caret 包,这太棒了。我之前听说过caret,但还没有调查过。看起来棒极了。谢谢!
    【解决方案4】:

    试试这个问题的答案,predicting class for new data using neuralnet

    【讨论】:

    • 还是没有运气。 &gt; net.compute &lt;- compute(net, matrix.train2[,1:10]) Error in neurons[[i]] %*% weights[[i]] : non-conformable arguments
    • 当您为 train2 使用模型矩阵时:matrix.train2 &lt;- model.matrix( ~ pclass + sex + age + sibsp + parch + fare + embarked, data=train2) 您没有指定登船C、登船Q、登船S。也许这就是您不断收到错误的原因,因为它需要比较明显不同的数据。为什么不尝试使用 +embarkedC + embarkedQ + embarkedS 修改运行您的实际代码
    • 我希望它是那么简单! “embarked”向量具有三个级别:C、Q 和 S。当我运行 model.matrix 函数时,该函数会自动将每个级别切片为自己的变量,以便它们遵循二进制格式。因此,我们得到的不是“embarked = C、Q 或 S”,而是 boardedC = 1/0、boardingQ = 1/0、boardingS = 1/0。这是函数的自动部分,所以我在运行 model.matrix 时不需要指定它,但是当我使用矩阵进行训练时,我必须考虑新变量。
    【解决方案5】:

    将neuralnet火车改成这个

    t <- neuralnet(
    survived ~ pclass + sexmale + age + sibsp + parch + fare + embarkedC + 
    embarkedQ + embarkedS)
    

    将预测变量更改为

    NN_pred<-compute(t,test[,1:9])
    

    它应该与模型中的数据具有相同的顺序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 2012-04-05
      • 2015-06-30
      相关资源
      最近更新 更多