如何在keras中用RNN层的特殊状态向量填充密集层

时间:2019-08-20 10:02:00

标签: keras keras-layer

我想知道我们是否可以在Keras中用选定数量的较低RNN状态向量填充密集层。换句话说,我想知道是否可以在Keras中实现基于手动意图的模型。

Illustrative image of the desired model

我创建了一个DynamicSelection层:

class DynamicSelection(Layer):

    def __init__(self, **kwargs):
        super(DynamicSelection, self).__init__(**kwargs)

    def call(self, inputs, **kwargs):
        results = []
        for idx in range(focusedElems):
            if inputs[1][idx] == -1:
                results = results + inputs[0][idx]
            else:
                results = results + ([0] * wordRnnUnitNum)
        return results

    def compute_output_shape(self, input_shape):
        return (focusedElems* wordRnnUnitNum, )

并用它来创建我的模型:

tokens = Input((tokenNum,))
embeddingLayer = Embedding(vocabularySize, tokenDim, trainable=trainable)(tokens)
rnnLayer = GRU(unitNum)(embeddingLayer)
selectedTimeSteps = Input((setlectedtimeStepNum,))
dynamicSelector = DynamicSelection()([rnnLayer, inputIdxs])
denseLayer = Dense(dense1UnitNumber,activation='relu')(dynamicSelector)
softmaxLayer = Dense(4, activation='softmax')(denseLayer)
model = Model(inputs=[inputIdxs, inputToken], outputs=softmaxLayer)

但是我得到以下异常:

dynamicSelector = DynamicSelection()([rnnLayer, inputIdxs])
  File "/Users/halsaied/anaconda2/lib/python2.7/site-packages/keras/engine/base_layer.py", line 503, in __call__
    arguments=user_kwargs)
  File "/Users/halsaied/anaconda2/lib/python2.7/site-packages/keras/engine/base_layer.py", line 571, in _add_inbound_node
    output_tensors[i]._keras_shape = output_shapes[i]
AttributeError: 'int' object has no attribute '_keras_shape'

0 个答案:

没有答案