[我正在尝试使用fastText构建一个定制的单词嵌入模型,该模型将我的数据(句子列表)表示为矢量,以便将其“馈送”到Keras CNN进行滥用语言检测。
我标记的数据存储在这样的列表中:
data = [['is',
'this',
'a',
'news',
'if',
'you',
'have',
'no',
'news',
'than',
'shutdown',
'the',
'channel'],
['if',
'interest',
'rate',
'will',
'hike',
'by',
'fed',
'then',
'what',
'is',
'the',
'effect',
'on',
'nifty']]
我目前正在像这样应用fastText模型:
model = fastText(data, size=100, window=5, min_count=5, workers=16, sg=0, negative=5)
然后我执行:
model = FastText(sentences, min_count=1)
documents = []
for document in textList:
word_vectors = []
for word in document:
word_vectors.append(model.wv[word])
documents.append(np.concatenate(word_vectors)
document_matrix = np.concatenate(documents)
显然,document_matrix不适合作为我的Keras模型的输入:
from keras.models import Sequential
from keras import layers
from keras.layers import Dense, Activation
model = Sequential()
model.add(layers.Conv1D(filters=250, kernel_size = 4, padding = 'same', input_shape=( 1,)))
model.add(layers.GlobalMaxPooling1D())
model.add(layers.Dense(250, activation='relu'))
model.add(layers.Dense(3, activation='sigmoid'))
我受困于如何使嵌入的输出适合Keras的输入的想法。
非常感谢,你们是最棒的!
丽莎
您可以使用model[YOURKEYWORD]
从word2vec模型中获取每个单词表示形式。您的word2vec模型中可能不存在某些单词嵌入,因此您可以在代码中使用try-catch
。