CNTK - 使用先前图层

时间:2018-04-12 16:01:39

标签: lstm recurrence cntk

我正在努力用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

2 个答案:

答案 0 :(得分:0)

您应该使用C.layers.Recurrence代替C.layers.RecurrenceFrom。后者用于建立具有序列输入和动态初始值的重复层,而前者仅需要序列输入。有关详细信息,请参阅help(C.layers.Recurrence)

答案 1 :(得分:0)

以下是更改后的示例代码。

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)