英文:
ValueError: Error when checking target: expected activation_9 to have shape (74, 6) but got array with shape (75, 6)
问题
以下是您提供的内容的中文翻译:
我在进行命名实体识别(在训练中,每个词都有一个标签)
标签的数量是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()
该模型在这里 。
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)
。
英文:
I work on Named entity recognition (in the train each word has a label)
the number of labels is 6
i run the model
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 model is 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)
When i execute the fit command the following error diplays: ValueError: Error when checking target: expected activation_9 to have shape (74, 6) but got array with shape (75, 6)
答案1
得分: 1
卷积层会降低它们输入的空间维度。
由于您使用了kernel_size=2
,因此您会将数据的长度缩短为original_length - 1
(从75缩短到74)。
解决方法是在您的卷积层中使用padding='same'
,这样系统会自动添加填充,以使最终长度与输入相同。
英文:
Convolutional layers reduce the spatial dimension of their inputs.
Since you're using a kernel_size=2
, you are shortening the length of your data to original_length - 1
(from 75 to 74).
The solution is to use padding='same'
in your convolutional layer, this way the system automatically adds a padding so the final length is the same as the input.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论