0

我正在尝试使用 MXNetR 构建前馈神经网络。我的输入是一个有 6380 行和 180 列的数据框。我的训练和测试输出是一维向量,每个向量有 319 个元素。

我运行模型时将批大小设置为 1,输出层的神经元数量设置为 319。因此,对于每个批,我希望得到一个包含 319 个元素的向量。我的目标是最小化我的损失函数,这是我的预测输出向量和实际输出向量之间的相关性。

下面是我的代码:

    # Define the input data
    data <- mx.symbol.Variable("data")

    # Define the first fully connected layer
    fc1 <- mx.symbol.FullyConnected(data, num_hidden = 100)
    act.fun <- mx.symbol.Activation(fc1, act_type = "relu") # create a hidden layer with Rectified Linear Unit as its activation function.
    output <<- mx.symbol.FullyConnected(act.fun, num_hidden = 319)

    # Customize loss function
    label <- mx.symbol.Variable("label")
    lro <-
        mx.symbol.MakeLoss(mx.symbol.Correlation(mx.symbol.reshape(output 
    ,shape = (1,319)),label))

    model <- mx.model.FeedForward.create(symbol=lro, X=train.x, 
                                         y=train.y,
                                         eval.data = list( data = test.x, 
                                                       label = test.y),
                                         num.round=5000, 
                                         array.batch.size=1, 
                                         optimizer = "adam",
                                         learning.rate = 0.0003, 
                                         eval.metric = mx.metric.rmse,
                                         epoch.end.callback = 
                                         mx.callback.log.train.metric(20, logger))

这是我运行上面的代码时的错误:

[15:49:28] /home/cgagnon/src/q5/mxnet/dmlc-core/include/dmlc/./logging.h:304: [15:49:28] src/operator/./correlation-inl.h:176: Check failed: dshape1.ndim() == 4U (2 vs. 4) data should be a 4D tensor

Stack trace returned 10 entries:
[bt] (0) /usr/lib64/R/library/mxnet/libs/libmxnet.so(_ZN4dmlc15LogMessageFatalD1Ev+0x29) [0x7f725a8528b9]
[bt] (1) /usr/lib64/R/library/mxnet/libs/libmxnet.so(_ZNK5mxnet2op15CorrelationProp10InferShapeEPSt6vectorIN4nnvm6TShapeESaIS4_EES7_S7_+0x2a2) [0x7f725b4a8222]
[bt] (2) /usr/lib64/R/library/mxnet/libs/libmxnet.so(+0xd461f9) [0x7f725b3241f9]
[bt] (3) /usr/lib64/R/library/mxnet/libs/libmxnet.so(+0x116630f) [0x7f725b74430f]
[bt] (4) /usr/lib64/R/library/mxnet/libs/libmxnet.so(+0x1167bb2) [0x7f725b745bb2]
[bt] (5) /usr/lib64/R/library/mxnet/libs/libmxnet.so(_ZN4nnvm11ApplyPassesENS_5GraphERKSt6vectorISsSaISsEE+0x501) [0x7f725b761481]
[bt] (6) /usr/lib64/R/library/mxnet/libs/libmxnet.so(_ZN4nnvm9ApplyPassENS_5GraphERKSs+0x8e) [0x7f725b699f2e]
[bt] (7) /usr/lib64/R/library/mxnet/libs/libmxnet.so(_ZN4nnvm4pass10InferShapeENS_5GraphESt6vectorINS_6TShapeESaIS3_EESs+0x240) [0x7f725b69c520]
[bt] (8) /usr/lib64/R/library/mxnet/libs/libmxnet.so(MXSymbolInferShape+0x281) [0x7f725b6959a1]
[bt] (9) /usr/lib64/R/library/mxnet/libs/mxnet.so(_ZNK5mxnet1R6Symbol10InferShapeERKN4Rcpp6VectorILi19ENS2_15PreserveStorageEEE+0x6b9) [0x7f724cef6739]

目前,我对如何修复此错误一无所知。我一直在寻找一种方法来重塑我的数据集,使它们成为 4D 张量,但找不到任何东西。我不会为我的问题寻找一个明确的解决方案,但是任何关于我应该如何解决这个错误的建议都将不胜感激。

4

1 回答 1

1

没有数据我无法重现问题,但我认为如果您只想将数据集重塑为 4D 张量,您应该能够通过“symbol.reshape(output,shape = c(1,1,1,319 ))”。不确定它是否对您有帮助。

于 2017-06-06T21:25:49.980 回答