通过自定义 LSTM 时的形状错误

Posted

技术标签:

【中文标题】通过自定义 LSTM 时的形状错误【英文标题】:Shape error when passed custom LSTM 【发布时间】:2018-12-28 14:51:23 【问题描述】:

我一直在尝试自定义 LSTM 层以进一步改进。但是在我的自定义 LSTM 之后,池化层出现了一个看起来很正常的错误。

我的环境是:

赢10 keras 2.2.0 python 3.6

回溯(最近一次通话最后一次): 文件“E:/PycharmProjects/dialogResearch/dialog/classifier.py”,第 60 行,在 模型 = build_model(word_dict, args.max_len, args.max_sents, args.embedding_dim) 文件“E:\PycharmProjects\dialogResearch\dialog\model\keras_himodel.py”, 第 177 行,在 build_model l_dense = TimeDistributed(Dense(200))(l_lstm) 文件“C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\topology.py”, 第 592 行,在 调用 self.build(input_shapes[0]) 文件“C:\ProgramData\Anaconda3\lib\site-packages\keras\layers\wrappers.py”, 第 162 行,在构建中 断言 len(input_shape) >= 3 断言错误

我的自定义LSTM的代码是:

class CustomLSTM(Layer):
    def __init__(self, output_dim, return_sequences, **kwargs):
        self.init = initializers.get('normal')
        # self.input_spec = [InputSpec(ndim=3)]
        self.output_dim = output_dim
        self.return_sequences = return_sequences
        super(CustomLSTM, self).__init__(**kwargs)

    def build(self, input_shape):
        assert len(input_shape) == 3
        self.original_shape = input_shape

        self.Wi = self.add_weight('Wi', (input_shape[-1], self.output_dim), initializer=self.init, trainable=True)
        self.Wf = self.add_weight('Wf', (input_shape[-1], self.output_dim), initializer=self.init, trainable=True)
        self.Wo = self.add_weight('Wo', (input_shape[-1], self.output_dim), initializer=self.init, trainable=True)
        self.Wu = self.add_weight('Wu', (input_shape[-1], self.output_dim), initializer=self.init, trainable=True)

        self.Ui = self.add_weight('Ui', (self.output_dim, self.output_dim), initializer=self.init, trainable=True)
        self.Uf = self.add_weight('Uf', (self.output_dim, self.output_dim), initializer=self.init, trainable=True)
        self.Uo = self.add_weight('Uo', (self.output_dim, self.output_dim), initializer=self.init, trainable=True)
        self.Uu = self.add_weight('Uu', (self.output_dim, self.output_dim), initializer=self.init, trainable=True)

        self.bi = self.add_weight('bi', (self.output_dim,), initializer=self.init, trainable=True)
        self.bf = self.add_weight('bf', (self.output_dim,), initializer=self.init, trainable=True)
        self.bo = self.add_weight('bo', (self.output_dim,), initializer=self.init, trainable=True)
        self.bu = self.add_weight('bu', (self.output_dim,), initializer=self.init, trainable=True)

        super(CustomLSTM, self).build(input_shape)

    def step_op(self, step_in, states):
        i = K.softmax(K.dot(step_in, self.Wi) + K.dot(states[0], self.Ui) + self.bi)
        f = K.softmax(K.dot(step_in, self.Wf) + K.dot(states[0], self.Uf) + self.bf)
        o = K.softmax(K.dot(step_in, self.Wo) + K.dot(states[0], self.Uo) + self.bo)
        u = K.tanh(K.dot(step_in, self.Wu) + K.dot(states[0], self.Uu) + self.bu)
        c = i * u + f * states[1]
        h = o * K.tanh(c)
        return h, [h, c]

    def call(self, x, mask=None):
        init_states = [K.zeros((K.shape(x)[0], self.output_dim)),
                       K.zeros((K.shape(x)[0], self.output_dim))] 
        outputs = K.rnn(self.step_op, x, init_states)
        if self.return_sequences:
            return outputs[1]
        else:
            return outputs[0]

    def compute_output_shape(self, input_shape):
        return input_shape[0], input_shape[-1]

型号是:

def build_model(words, max_len, max_sents, embedding_dim):
    sentence_input = Input(shape=(max_len,), dtype='int32')
    embedding_layer = Embedding(len(words) + 1,
                                embedding_dim,
                                input_length=max_len,
                                trainable=True)
    embedded_sequences = embedding_layer(sentence_input)
    l_lstm = CustomLSTM(200, return_sequences=True)(embedded_sequences)
    print(l_lstm.get_shape())
    l_dense = TimeDistributed(Dense(200))(l_lstm)
    l_att = AttLayer()(l_dense)
    sentEncoder = Model(sentence_input, l_att)

    review_input = Input(shape=(max_sents, max_len), dtype='int32')
    review_encoder = TimeDistributed(sentEncoder)(review_input)
    l_lstm_sent = CustomLSTM(200, return_sequences=True)(review_encoder)
    l_dense_sent = TimeDistributed(Dense(200))(l_lstm_sent)
    l_att_sent = AttLayer()(l_dense_sent)
    preds = Dense(3, activation='softmax')(l_att_sent)
    model = Model(review_input, preds)
    optimizer = Nadam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004)
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=[precision, recall, f1, 'acc'])
    return model

感谢您的帮助。

【问题讨论】:

【参考方案1】:

我认为发生错误是因为compute_output_shapereturn_sequences=True 返回的形状不正确。我会尝试以下方法:

def compute_output_shape(self, input_shape):
    if self.return_sequences:
        return input_shape
    return (input_shape[0], input_shape[-1])

【讨论】:

你写的代码也让我了解这个函数是如何工作的

以上是关于通过自定义 LSTM 时的形状错误的主要内容,如果未能解决你的问题,请参考以下文章

通过自定义形状裁剪 UIImage

自定义 CNN 给出错误的输出形状

用户未经授权时的 Laravel 护照自定义错误消息和状态码

C# WCF -- 通过自定义 AuthorizationManager 自定义错误授权失败

在自定义数据集上微调 MobileNet 时出现形状错误

自定义Button形状(圆形椭圆)