Skip to main content
 首页 » 编程设计

python之使用keras在分类问题中结合结构化数据和文本数据

2024年12月31日8zlslch

以下代码是使用词嵌入来预测标签的一个非常简单的示例(见下文)。该示例取自 here .

from numpy import array 
from keras.preprocessing.text import one_hot 
from keras.preprocessing.sequence import pad_sequences 
from keras.models import Sequential 
from keras.layers import Dense 
from keras.layers import Flatten 
from keras.layers.embeddings import Embedding 
 
# define documents 
docs = ['Well done!', 
        'Good work', 
        'Great effort', 
        'nice work', 
        'Excellent!', 
        'Weak', 
        'Poor effort!', 
        'not good', 
        'poor work', 
        'Could have done better.'] 
 
# define class labels 
labels = array([1,1,1,1,1,0,0,0,0,0]) 
 
# integer encode the documents 
vocab_size = 50 
encoded_docs = [one_hot(d, vocab_size) for d in docs] 
print(encoded_docs) 
 
# pad documents to a max length of 4 words 
max_length = 4 
padded_docs = pad_sequences(encoded_docs, maxlen=max_length, padding='post') 
print(padded_docs) 
 
# define the model 
model = Sequential() 
model.add(Embedding(vocab_size, 8, input_length=max_length)) 
model.add(Flatten()) 
model.add(Dense(1, activation='sigmoid')) 
 
# compile the model 
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc']) 
 
# summarize the model 
print(model.summary()) 
 
# fit the model 
model.fit(padded_docs, labels, epochs=50, verbose=0) 
 
# evaluate the model 
loss, accuracy = model.evaluate(padded_docs, labels, verbose=0) 
print('Accuracy: %f' % (accuracy*100)) 

假设我们有这样的结构化数据:

hours_of_revision = [10, 5, 7, 3, 100, 0, 1, 0.5, 4, 0.75] 

这里每个条目都与每一行对齐,很好地表明一个人应该花更多的时间来修改以获得好的分数(-:

只是想知道,是否可以将其合并到模型中以使用文本和结构化数据?

请您参考如下方法:

是的,这可以通过 Keras 的函数式 API 实现。您所需要的只是 hours_of_revision 的额外输入,它在进入最终分类器之前与文本数据的嵌入连接。

首先缩放附加数据:

# additional data 
hours_of_revision = [10, 5, 7, 3, 100, 0, 1, 0.5, 4, 0.75] 
 
import numpy as np 
# Scale the data 
mean = np.mean(hours_of_revision) 
std = np.std(hours_of_revision) 
hours_of_revision = (hours_of_revision - mean)/std 

使用函数式 API 构建模型:

# Build model 
from keras.layers import Input, Embedding, Flatten, Dense, Concatenate 
from keras.models import Model 
# Two input layers 
integer_input = Input((max_length, )) 
revision_input = Input((1,)) 
 
# Embedding layer for the words 
embedding = Embedding(vocab_size, 8, input_length=max_length)(integer_input) 
embedding_flat = Flatten()(embedding) 
 
# Concatenate embedding with revision 
combined_data = Concatenate()([embedding_flat, revision_input]) 
output = Dense(1, activation='sigmoid')(combined_data) 
 
# compile the model - pass a list of input tensors 
model = Model(inputs=[integer_input, revision_input], outputs=output) 
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc']) 
 
# fit the model - pass list of input data 
model.fit([padded_docs, hours_of_revision], labels, epochs=50, verbose=0) 

有关如何将函数式 API 用于多输入/多输出模型的更多示例,请查看 Keras docs .