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

时间:2019-04-26 20:46:54

标签: python mxnet

在努力了解mxnet时,我正在尝试修改在线找到的模型(https://github.com/apache/incubator-mxnet/tree/master/example/multivariate_time_series)。我正在尝试建立一个同时使用CNN和RNN网络的模型,然后使用两者的输出来预测时间序列。但是,我遇到了这个错误

  

RuntimeError:simple_bind错误。参数:数据:(128,96,20)   softmax_label:(128,20)运算符concat1中的错误:[15:44:09]   src / operator / nn / concat.cc:66:检查失败:   shape_assign(&(* in_shape)[i],dshape)不兼容的输入形状:   预期[128,0],得到[128,96,300]

这是我尝试对其进行修改的代码:

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.reshape(data = cnn_reg_features, shape = (-1))
  • c_features = cnn_reg_features[-1]
  • c_features = cnn_reg_features[:, -1, :]

我也尝试着查看git问题和周围的Google,但我所看到的只是使用infer_shape的建议。我尝试将其应用于c_features,但输出结果对我来说不清楚

data: ()
gru_i2h_weight: ()
gru_i2h_bias: ()

基本上,我想在每个阶段都知道该图的建立是什么形状。我在Tensorflow中已经习惯了此功能,当一个人因进行错误的重塑而误入歧途时,或者通过查看模型的尺寸来了解模型的工作原理时,它使创建和调试图形变得更加容易。 mxnet中没有同等的机会吗?

鉴于生产这些data_iter时会馈入symbols,所以我认为推断的形状应该可用。最终,我的问题是(1)当符号使用迭代器中的数据时如何看到符号的形状,并且应该知道所有形状? (2)在这种情况下进行调试的一般准则?

谢谢。

0 个答案:

没有答案