ValueError: Error when checking target: expected activation_9 to have shape (74, 6) but got array with shape (75, 6)

huangapple go评论75阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年1月3日 19:40:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定