CNTK python API:CNN + LSTM-Network的动态轴构造

时间:2017-03-30 15:41:09

标签: python regression conv-neural-network lstm cntk

我正在使用CNTK Python API处理视频/图像序列输入的回归问题。 输入数据是(minibatch_size x sequence_length x channel x height x width)-tensor,其中(此时)固定minibatch_size = 1,sequence_length = 3且仅有1个通道。输出数据是具有完全相同序列长度的(minibatch_size x sequence_length x num_landmarks)张量。然后,CNN + LSTM网络分析一个视频帧并输出26个地标。 我的网络定义如下:

def create_network_definition(model_func):

    # define dynamic axes
    batch_axis = Axis.default_batch_axis()
    input_seq_axis = Axis('inputAxis')
    label_seq_axis = Axis('labelAxis')

    # construct input variables
    input_dynamic_axes = [batch_axis, input_seq_axis]
    input_var = input_variable(shape=(video_channel_count, video_height, video_width), dynamic_axes=input_dynamic_axes, dtype=np.float32)

    label_dynamic_axes = [batch_axis, label_seq_axis]
    label_var = input_variable(shape=(num_landmarks), dynamic_axes=label_dynamic_axes, dtype=np.float32)

    # instantiate 2D Convolution network
    z = model_func(input_var, out_dims = num_landmarks)

    # loss and metric
    loss = squared_error(z, label_var)
    error = sqrt(constant(1/num_landmarks) * loss)

    return {
        'input'  : input_var,
        'label'  : label_var,
        'loss'   : loss,
        'error'  : error, 
        'output' : z
    }

这会引发以下错误:

Training so far unspecified number of parameters in 7 parameter tensors.
Traceback (most recent call last):
  File "Conv2D_300VW_v1.py", line 490, in <module>
    gen_heartbeat      = False)
  File "Conv2D_300VW_v1.py", line 457, in do_training_and_validation
    trainer = create_trainer(network, epoch_size, max_epochs, minibatch_size, progress_printer)
  File "Conv2D_300VW_v1.py", line 357, in create_trainer
    learner  = momentum_sgd(network['output'].parameters, lr_schedule, mm_schedule) #, l2_regularization_weight=l2_reg_weight)
  File "C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\cntk-py35\lib\site-packages\cntk\internal\swig_helper.py", line 63, in wrapper
    result = f(*args, **kwds)
  File "C:\local\Anaconda3-4.1.1-Windows-x86_64\envs\cntk-py35\lib\site-packages\cntk\learners\__init__.py", line 499, in momentum_sgd
    additional_options)
ValueError: Cannot create an NDArrayView using a view shape '[? x 26]' that has unknown dimensions for any of its axes.

[CALL STACK]
    > CNTK::NDArrayView::  DeepClone
    - CNTK::NDArrayView::NDArrayView<double>
    - CNTK::NDArrayView::  NDArrayView
    - CNTK::Value::  GetSequenceStartsAndLengths
    - CNTK::Value::  GetSequenceStartsAndLengths
    - CNTK::Variable::operator std::shared_ptr<CNTK::Function>
    - CNTK::Value::  GetSequenceStartsAndLengths
    - RtlRunOnceExecuteOnce
    - InitOnceExecuteOnce
    - _crtInitOnceExecuteOnce
    - CNTK::Value::  GetSequenceStartsAndLengths
    - CNTK::Variable::  Value
    - CNTK::Internal::  SaveAsLegacyModel
    - CNTK::  AdamLearner
    - CNTK::Internal::  ZeroesWithDynamicAxesLike
    - CNTK::Internal::  ZeroesWithDynamicAxesLike

我怀疑学习者无法从数据中获取批量大小和序列大小。但为什么?如何正确实现动态轴?我在CNTK文档中找不到任何提示。

我也尝试同时设置dynamic_axes=input_dynamic_axesinput_seq_axis = Axis.default_dynamic_axis(),但没有任何改变。

1 个答案:

答案 0 :(得分:0)

你能分享完整的例子吗?此外,输入和标签动态轴需要相同。如下:

input_dynamic_axes = [batch_axis,input_seq_axis]
label_dynamic_axes = [batch_axis,input_seq_axis]