我从事命名实体识别(在火车上,每个单词都有一个标签)
标签数量为6
我运行模型
from keras.models import Sequential
from keras.layers import Dense, LSTM, InputLayer, Bidirectional, TimeDistributed, Embedding, Activation
from keras.optimizers import Adam
from keras import initializers
model = Sequential()
model.add(InputLayer(input_shape=(MAX_LENGTH, )))
model.add(Embedding(len(word2index), 128))
model.add(Conv1D(filters=32, kernel_size=2, activation='relu'))
model.add(Bidirectional(LSTM(256, return_sequences=True)))
# model.add(AttentionLayer(300,True,name='word_attention'))
model.add(TimeDistributed(Dense(len(tag2index))))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer=Adam(0.001),
metrics=['accuracy'])
model.summary()
该模型为the following。
def to_categorical(sequences, categories):
cat_sequences = []
for s in sequences:
cats = []
for item in s:
cats.append(np.zeros(categories))
cats[-1][item] = 1.0
cat_sequences.append(cats)
return np.array(cat_sequences)
cat_train_tags_y = to_categorical(train_tags_y, len(tag2index))
model.fit(train_sentences_X, cat_train_tags_y, batch_size=128, epochs=20, validation_split=0.2)
当我执行fit命令时,将显示以下错误:
ValueError: Error when checking target: expected activation_9 to have shape (74, 6) but got array with shape (75, 6)
最佳答案
卷积层减小了其输入的空间尺寸。
由于使用的是kernel_size=2
,因此将数据长度缩短为original_length - 1
(从75到74)。
解决方案是在卷积层中使用padding='same'
,这样系统会自动添加填充,因此最终长度与输入相同。
关于python - ValueError:检查目标时出错:预期activation_9具有形状(74,6),但数组具有形状(75,6),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59577985/