我正在尝试使用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张量但却找不到。我不会为我的问题寻找明确的解决方案,但是对于我应该如何处理这个错误的任何建议都将不胜感激。
答案 0 :(得分:1)
如果没有数据我无法重现问题,但我认为如果您只想将数据集重塑为4D张量,您应该可以通过 &#34; symbol.reshape(输出,形状= c(1,1,1,319))&#34;。 不确定它是否对你有帮助。