gpt4 book ai didi

machine-learning - 通过自定义 LSTM 时出现形状错误

转载 作者:行者123 更新时间:2023-11-30 09:17:30 25 4
gpt4 key购买 nike

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

我的环境是:

  • 赢得 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

我的自定义 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

感谢您的帮助。

最佳答案

我认为发生错误是因为 return_sequences=Truecompute_output_shape 返回的形状不正确。我会尝试以下操作:

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

关于machine-learning - 通过自定义 LSTM 时出现形状错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51440054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com