python - CNTK 提示 LSTM 中的动态轴

标签 python lstm cntk

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

输入:

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

  • 标签是单热值的向量

网络:

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...

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

最佳答案

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

input =  input_variable(input_dim)

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

input = input_variable(1)

那么我相信您最初的尝试应该会奏效。

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

之后定义标签
label = input_variable(num_output_classes, dynamic_axes=z.dynamic_axes)

这对我来说没有任何提示。然后我像这样提供了一些虚拟数据(假设一个小批量为 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 })

它按预期工作。

关于python - CNTK 提示 LSTM 中的动态轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41198525/

相关文章:

python - 支持 GPU 的 Python CNTK 2.0 在 Anaconda 3 4.4.0、Windows 10 Pro 64 位中似乎因 100% CPU 线程工作负载而挂起

cntk - 微软放弃CNTK了吗?

python - 使用 tkinter 变量查询 MySQL 数据库

python - 从 SQLAlchemy 执行 PL/SQL 函数(或过程)

python - 如何将数据集拆分为训练集和验证集以保持类之间的比率?

python - Tensorflow:尝试使用未初始化的值 beta1_power

artificial-intelligence - CNTK:读取输入文件时达到允许的最大错误数

Python2.7 : Ensure 3D list of objects are unique

text - 字符的序列预测?

python - LSTM InvalidArgumentError Tensorflow 2.0/Keras 转换为估计器时出现