Keras上的多维Y_列车

2024-10-03 02:41:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个x_train和y_train的语料库,经过这样的处理:

input_sequences = []
labels = []

indexCA = 0

for line in corpusMSA:
    lineCA = corpusCA[indexCA].split() # Save CA Line
    token_list = tokenizer.texts_to_sequences([line])[0] # Tokenize line
    for i in range(1, len(token_list)):
        n_gram_sequence = token_list[:i+1] # Generate ngrams (n=2)
        n_gram_label = lineCA[:i+1]
        input_sequences.append(n_gram_sequence)
        labels.append(n_gram_label)
    indexCA+=1

# pad sequences 
max_sequence_len = max([len(x) for x in input_sequences])
input_sequences = np.array(pad_sequences(input_sequences, maxlen=max_sequence_len, padding='pre'))

max_labels_len = max([len(x) for x in labels])
labels = np.array(pad_sequences(labels, maxlen=max_labels_len, padding='pre'))

# create predictors and label
xs = input_sequences
ys = tf.keras.utils.to_categorical(labels, num_classes=16)

两个数据集的原始形状都是(1098360,14),但在使用utils.to_category()方法后,y_列形状变成(1098360,14,16)

我有两个双向LSTM层:

model.add(Embedding(total_words, 100, input_length=max_sequence_len))
model.add(Bidirectional(LSTM(256, return_sequences=True)))
model.add(Bidirectional(LSTM(128)))
model.add(Dense(16, activation='softmax'))
adam = Adam(lr=0.01)
model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=20, batch_size=size_batch, verbose=1, callbacks=[tensorboard])

我有一个错误:当使用as losscategorical_crossentropy时,为shape(None,16)的输出传递了一个shape(1098360,14,16)的目标数组。此损失期望目标与输出具有相同的形状

我怎样才能告诉我的模型输出的形状是(无,14,16)


Tags: inaddforinputlabelsmodellenline
1条回答
网友
1楼 · 发布于 2024-10-03 02:41:36

y_train在调用to_categorical之前,似乎已经是一个向量,因此不需要使用to_categorical。但是,如果在多标签分类的情况下,该向量包含多个类,则需要使用to_categorical,然后使用np.sum(axis=1)
最终结果如下:
y_train = to_categorical(y_train, num_classes=16).sum(axis=1)

相关问题 更多 >