面对R中mxnet回归的困难

时间:2017-05-01 14:36:45

标签: r deep-learning mxnet

我正在关注本教程http://mxnet.io/tutorials/r/fiveMinutesNeuralNetwork.html#regression 一切都有效,但是当我改变时:

fc1 <- mx.symbol.FullyConnected(data, num_hidden=1)

fc1 <- mx.symbol.FullyConnected(data, num_hidden=2)

在错误日志的堆栈中,我认为这可能是最有趣的:

    Error in exec$update.arg.arrays(arg.arrays, match.name, skip.null) : 
    [20:22:59] src/ndarray/ndarray.cc:239: Check failed: from.shape() == to->shape() 
 shape mismatchfrom.shape = (20,) to.shape=(20,2)

如何诊断此问题?

这是sessionInfo()的输出:

R version 3.3.3 RC (2017-02-27 r72279)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.5 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] mlbench_2.1-1 mxnet_0.9.5  

loaded via a namespace (and not attached):
 [1] igraph_1.0.1       Rcpp_0.12.10       rstudioapi_0.6     magrittr_1.5       munsell_0.4.3      colorspace_1.3-2  
 [7] viridisLite_0.2.0  R6_2.2.0           brew_1.0-6         stringr_1.2.0      plyr_1.8.4         dplyr_0.5.0       
[13] visNetwork_1.0.3   Rook_1.1-1         tools_3.3.3        grid_3.3.3         gtable_0.2.0       DBI_0.6           
[19] influenceR_0.1.0   DiagrammeR_0.9.0   htmltools_0.3.5    lazyeval_0.2.0     digest_0.6.12      assertthat_0.1    
[25] tibble_1.2         gridExtra_2.2.1    RColorBrewer_1.1-2 ggplot2_2.2.1      codetools_0.2-8    htmlwidgets_0.8   
[31] viridis_0.4.0      rgexf_0.15.3       stringi_1.1.3      scales_0.4.1       XML_3.98-1.6       jsonlite_1.3   

1 个答案:

答案 0 :(得分:2)

问题是在fc1 <- mx.symbol.FullyConnected(data, num_hidden=1)行之下,教程对输出lro <- mx.symbol.LinearRegressionOutput(fc1)使用线性回归。

LinearRegressionOutput用于计算输入符号与提供给它的标签之间的l2损失。它假定每个例子有1个标签,并且通过2个标签会打破它。在我的情况下,它与您的消息有点不同,可能是因为版本的差异:

Error in symbol$infer.shape(list(...)) : 
  Error in operator linearregressionoutput5: Shape inconsistent, Provided=(20,), inferred shape=(20,2)

修复此问题取决于您想要实现的目标。如果您正在解决分类任务并希望接收两个类的概率,那么您需要使用Softmax:

fc1 <- mx.symbol.FullyConnected(data, num_hidden=2)
lro <- mx.symbol.SoftmaxOutput(fc1)