【问题标题】:CNTK Complaining about Dynamic Axis in LSTMCNTK 抱怨 LSTM 中的动态轴
【发布时间】:2016-12-17 12:20:28
【问题描述】:

我正在尝试在 CNTK(使用 Python)中实现 LSTM 来对序列进行分类。

输入:

  • 特征是固定长度的数字序列(时间序列)

  • 标签是 one-hot 值的向量

网络:

input = input_variable(input_dim)
label = input_variable(num_output_classes)
h = Recurrence(LSTM(lstm_dim)) (input)
final_output = C.sequence.last(h)
z = Dense(num_output_classes) (final_output)
loss = C.cross_entropy_with_softmax(z, label)

输出: 序列匹配标签的概率

所有尺寸都是固定的,所以我认为我不需要任何动态轴并且没有指定任何。

但是,CNTK 不高兴,我明白了:

return cross_entropy_with_softmax(output_vector, target_vector, axis, name)
RuntimeError: Currently if an operand of a elementwise operation has any dynamic axes, those must match the dynamic axes of the other operands

如果(根据一些示例)我用动态轴定义标签

label = input_variable(num_output_classes, dynamic_axes=[C.Axis.default_batch_axis()])

它不再抱怨这个,而是进一步:

tf = np.split(training_features,num_minibatches)
tl = np.split(training_labels, num_minibatches)

for i in range(num_minibatches*num_passes): # multiply by the 
    features = np.ascontiguousarray(tf[i%num_minibatches])
    labels = np.ascontiguousarray(tl[i%num_minibatches])

    # Specify the mapping of input variables in the model to actual minibatch data to be trained with
    trainer.train_minibatch({input : features, label : labels})

但死于此错误:

  File "C:\Users\Dev\Anaconda3\envs\cntk-py34\lib\site-packages\cntk\cntk_py.py", line 1745, in train_minibatch
    return _cntk_py.Trainer_train_minibatch(self, *args)
RuntimeError: Node '__v2libuid__Plus561__v2libname__Plus552' (Plus operation): DataFor: FrameRange's dynamic axis is inconsistent with matrix: {numTimeSteps:1, numParallelSequences:100, sequences:[{seqId:0, s:0, begin:0, end:1}, {seqId:1, s:1, begin:0, end:1}, {seqId:2, s:2, begin:0, end:1}, {seqId:3, s:3, begin:0, end:1}, {seq...

我需要做什么来解决这个问题?

【问题讨论】:

    标签: python lstm cntk


    【解决方案1】:

    如果我正确理解这一点,您就有了一维输入的序列。如果是这样,那么你的问题就出在这一行

    input =  input_variable(input_dim)
    

    声明了一系列 input_dim 维度向量。如果你把它改成

    input = input_variable(1)
    

    那么我相信你的初步尝试应该会奏效。

    更新:上述内容本身是不够的,因为获取序列的最后一个元素的操作会创建一个输出,其动态轴与创建标签的默认动态轴不同。一个简单的解决方法是在定义输出 z 这样之后定义标签

    label = input_variable(num_output_classes, dynamic_axes=z.dynamic_axes)
    

    这对我来说没有任何抱怨。然后我输入了一些像这样的虚拟数据(假设 minibatch 为 4,序列长度为 5 和 3 类)

    x = np.arange(20.0, dtype=np.float32).reshape(4,5,1)
    y = np.array([1,0,0,0,1,0,0,0,1,0,0,1], dtype=np.float32).reshape(4,1,3)
    loss.eval({input: x, label:y })
    

    它按预期工作。

    【讨论】:

    • RuntimeError: 当前,如果元素操作的操作数有任何动态轴,则必须与其他操作数的动态轴匹配
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多