R neuralNet:“不一致的论点”

时间:2013-07-09 14:41:35

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

哎呀!尝试使用我的神经网络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

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

谢谢!


到目前为止基于评论的更新:

  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. 我是通过train1手动将train2model.matrix数据框放入矩阵中,因为如果不这样做,我会收到以下错误:

    > Error in neurons[[i]] %*% weights[[i]] : 
    requires numeric/complex matrix/vector arguments
    
  3. 注意:请参阅以下主题,详细了解我使用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”。

5 个答案:

答案 0 :(得分:9)

看起来您需要删除预测变量。试试这个:

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

答案 1 :(得分:5)

我也尝试了神经网络包。我想如果你代替

// receive output from somewhere else
pad := "%-16s%s"
raw, err := yaml.Marshal(output)
sArr := strings.Split(raw, "\n")
for _, s := range sArr {
    fmt.Fprintln(c.StdOut, fmt.Sprintf(pad, s, ""))
} 

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

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

答案 2 :(得分:1)

我没有使用过神经网络,但除非它做了一些奇怪的事情,否则你不应该像这样调用model.matrixneuralnet有一个公式界面,因此它会为您调用model.matrix。您只需要为其提供培训数据框train1

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

答案 3 :(得分:0)

尝试使用此问题的答案,predicting class for new data using neuralnet

答案 4 :(得分:0)

neuralnet火车更改为此

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

将预测变量更改为

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

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