CNTK - LSTM 重复使用前一层的输入值

CNTK - LSTM Recurrence with input values from previous layers

我正在努力用 LSTM 单元重复实施我的模型。我想使用密集层的输出作为循环序列的输入,但我不知道该怎么做。

这是我要实现的示例代码:

import cntk as C
import numpy as np

a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

x = C.input_variable(a.shape, name='Input Variable')

m = C.layers.Convolution1D(filter_shape=3,
                         num_filters=4,
                         strides=(2),
                         reduction_rank=0,
                         pad=True, name='Convolutional layer')(x)

m = C.layers.Dense(5, activation=None, name='Dense layer')(m)


m = C.layers.RecurrenceFrom(C.layers.LSTM(3,name='LSTM Layer'), name='Reccurence Layer')(m)

这是我想象中的图片 (based on this tutorial):

picture

运行 代码后控制台的输出:

>>> m = C.layers.RecurrenceFrom(C.layers.LSTM(3,name='LSTM Layer'), name='Reccurence Layer')(m)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Maros\Anaconda3\lib\site-packages\cntk\ops\functions.py", line 374, in __call__
    arg_map = self.argument_map(*args, **kwargs)
  File "C:\Users\Maros\Anaconda3\lib\site-packages\cntk\ops\functions.py", line 263, in argument_map
    raise TypeError("CNTK Function expected {} arguments, got {}".format(len(params), len(args) + len(kwargs)))
TypeError: CNTK Function expected 3 arguments, got 1

您应该使用 C.layers.Recurrence 而不是 C.layers.RecurrenceFrom。后者用于建立循环层,输入序列和动态初始值,而前者只需要序列输入。有关详细信息,请参阅 help(C.layers.Recurrence)

这是我修改后的示例代码。

import cntk as C
import numpy as np

a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

x = C.input_variable(a.shape, name='Input Variable')

m = C.layers.Convolution1D(filter_shape=3,
                         num_filters=4,
                         strides=(2),
                         reduction_rank=0,
                         pad=True, name='Convolutional layer')(x)

m = C.layers.Dense((5,1), activation=None, name='Dense layer')(m)

m = C.ops.to_sequence(m)

m = C.layers.Recurrence(C.layers.LSTM(3,name='LSTM Layer'), name='Reccurence Layer')(m)