Python mxnet:如何调试形状不匹配的模型

Python mxnet:如何调试形状不匹配的模型,python,mxnet,Python,Mxnet,我正在尝试修改我在网上找到的一个模型(),因为我正在努力了解mxnet。我试图建立一个模型,将CNN和RNN网络并行,然后使用两者的输出来预测时间序列。然而,我遇到了这个错误 RuntimeError:简单的\u绑定错误。参数:数据:(128、96、20) softmax_标签:(128,20)运算符concat1中的错误:[15:44:09] src/operator/nn/concat.cc:66:检查失败: 形状分配(&(*in_形状)[i],dshape)不兼容的输入形状: 预期为[12

我正在尝试修改我在网上找到的一个模型(),因为我正在努力了解
mxnet
。我试图建立一个模型,将CNN和RNN网络并行,然后使用两者的输出来预测时间序列。然而,我遇到了这个错误

RuntimeError:简单的\u绑定错误。参数:数据:(128、96、20) softmax_标签:(128,20)运算符concat1中的错误:[15:44:09] src/operator/nn/concat.cc:66:检查失败: 形状分配(&(*in_形状)[i],dshape)不兼容的输入形状: 预期为[128,0],实际为[128,96300]

这是我试图修改的代码:

def rnn_cnn_model(iter_train, q, filter_list, num_filter, dropout, seasonal_period, time_interval):


# Choose cells for recurrent layers: each cell will take the output of the previous cell in the list
rcells = [mx.rnn.GRUCell(num_hidden=args.recurrent_state_size)]
skiprcells = [mx.rnn.LSTMCell(num_hidden=args.recurrent_state_size)]


input_feature_shape = iter_train.provide_data[0][1]
X = mx.symbol.Variable(iter_train.provide_data[0].name)
Y = mx.sym.Variable(iter_train.provide_label[0].name)

# reshape data before applying convolutional layer (takes 4D shape incase you ever work with images)
rnn_input = mx.sym.reshape(data=X, shape=(0, q, -1))

###############
# RNN Component
###############
stacked_rnn_cells = mx.rnn.SequentialRNNCell()
for i, recurrent_cell in enumerate(rcells):
    stacked_rnn_cells.add(recurrent_cell)
    stacked_rnn_cells.add(mx.rnn.DropoutCell(dropout))
outputs, states = stacked_rnn_cells.unroll(length=q, inputs=rnn_input, merge_outputs=False)
rnn_features = outputs[-1] #only take value from final unrolled cell for use later

input_feature_shape = iter_train.provide_data[0][1]
X = mx.symbol.Variable(iter_train.provide_data[0].name)
Y = mx.sym.Variable(iter_train.provide_label[0].name)

# reshape data before applying convolutional layer (takes 4D shape incase you ever work with images)
conv_input = mx.sym.reshape(data=X, shape=(0, 1, q, -1))

###############
# CNN Component
###############
outputs = []
for i, filter_size in enumerate(filter_list):
    # pad input array to ensure number output rows = number input rows after applying kernel
    padi = mx.sym.pad(data=conv_input, mode="constant", constant_value=0,
                      pad_width=(0, 0, 0, 0, filter_size - 1, 0, 0, 0))
    convi = mx.sym.Convolution(data=padi, kernel=(filter_size, input_feature_shape[2]), num_filter=num_filter)
    acti = mx.sym.Activation(data=convi, act_type='relu')
    trans = mx.sym.reshape(mx.sym.transpose(data=acti, axes=(0, 2, 1, 3)), shape=(0, 0, 0))
    outputs.append(trans)
cnn_features = mx.sym.Concat(*outputs, dim=2)
cnn_reg_features = mx.sym.Dropout(cnn_features, p=dropout)
c_features = mx.sym.reshape(data = cnn_reg_features, shape = (-1))
print(type(c_features))
######################
# Prediction Component
######################

print(rnn_features.infer_shape())   
neural_components = mx.sym.concat(*[rnn_features, c_features], dim=1)
neural_output = mx.sym.FullyConnected(data=neural_components, num_hidden=input_feature_shape[2])
model_output = neural_output
loss_grad = mx.sym.LinearRegressionOutput(data=model_output, label=Y)
return loss_grad, [v.name for v in iter_train.provide_data], [v.name for v in iter_train.provide_label]
我相信崩溃发生在这行代码上

neural_components = mx.sym.concat(*[rnn_features, c_features], dim=1)
以下是我为使我的维度匹配所做的尝试:

  • c_features=mx.sym.reformate(data=cnn_reg_features,shape=(-1))
  • c\u features=cnn\u reg\u features[-1]
  • c_features=cnn_reg_features[:,-1,:]
我也试着看看git问题和Google,但我看到的只是使用
inferr\u shape
的建议。我尝试将此应用于
c_功能
,但输出不清楚

data: ()
gru_i2h_weight: ()
gru_i2h_bias: ()
基本上,我想知道在这个图形构建的每个阶段,符号的形状是什么。我已经习惯了Tensorflow中的这种功能,当一个人在错误的重塑过程中误入歧途时,或者仅仅是为了通过观察模型的维度来了解模型的工作原理,它可以更容易地构建和调试图形。
mxnet
中是否没有同等机会

假设在生成这些符号时输入了
数据
,我认为推断的形状应该是可用的。最终我的问题是(1)当符号使用迭代器中的数据并且应该知道所有形状时,我如何看到符号的形状?(2) 在这种情况下调试的一般指导原则


谢谢。

调试Symbol network非常痛苦。如果您愿意,我建议切换到Gluon API-它允许更简单的调试。您正在使用的LSTNet网络在这里是使用Gluon API实现的-调试符号网络是相当痛苦的。如果您愿意,我建议切换到Gluon API-它允许更简单的调试。您正在使用的LSTNet网络在这里使用glionapi实现-