Shape error when passed custom LSTM

六月ゝ 毕业季﹏ 提交于 2021-02-10 14:21:01

问题


I have been trying to custom a LSTM layer for further improvement. But an error which seems like normal raised at pooling layer after my custom LSTM.

My environment is:

  • win 10
  • keras 2.2.0
  • python 3.6

Traceback (most recent call last):
File "E:/PycharmProjects/dialogResearch/dialog/classifier.py", line 60, in
model = build_model(word_dict, args.max_len, args.max_sents, args.embedding_dim)
File "E:\PycharmProjects\dialogResearch\dialog\model\keras_himodel.py", line 177, in build_model
l_dense = TimeDistributed(Dense(200))(l_lstm)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\topology.py", line 592, in call
self.build(input_shapes[0])
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\layers\wrappers.py", line 162, in build
assert len(input_shape) >= 3
AssertionError

The code of my custom LSTM is:

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]

The model is:

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

Thanks for your help.


回答1:


I presume the error is happening because the shape returned by compute_output_shape when return_sequences=True is incorrect. I would try the following:

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


来源:https://stackoverflow.com/questions/51440054/shape-error-when-passed-custom-lstm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!