【发布时间】:2021-07-31 09:56:06
【问题描述】:
我想用 Keras 制作命名实体识别模型。 这些是我关注的链接:
https://valueml.com/named-entity-recognition-using-lstm-in-keras/ https://djajafer.medium.com/named-entity-recognition-and-classification-with-keras-4db04e22503d
数据如下所示:
word label
0 Thousands O
1 of O
2 demonstrators O
3 have O
4 marched O
... ... ...
44187 there O
44188 accidentally O
44189 or O
44190 deliberately O
44191 . O
他们使用单词到向量,因此他们正在索引单词和标签,因此X 是我的特征(单词的索引序列),y 是我的结果(标签的索引序列):
max_len = 30
X = [[word2idx[w[0]] for w in s] for s in list_of_sentances]
X = pad_sequences(maxlen=max_len, sequences=X, padding="post", value=num_words-1)
y = [[label2idx[w[1]] for w in s] for s in list_of_sentances]
y = pad_sequences(maxlen=max_len, sequences=y, padding="post", value=label2idx["O"])
y = [to_categorical(i, num_classes=num_labels) for i in y]
这里我有另一个专栏,那就是POS。如何将POS 列的值添加到我的功能?
所以基本上,我不只想要我的 X 中的 word 值,我还想要我的 X 中的 POS 值。*(或任何其他值)
如果我有多个列,例如:
word
POS
is_capital_letter
word_length
...
如何将所有这些列添加到我的功能中?
这是我的模型: X = np.array(X) y = np.array(y)
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)
print("x_train shape", x_train.shape)
print("x_test shape", x_test.shape)
#x_train shape (750, 75)
#x_test shape (250, 75)
input_word = Input(shape=(max_len,))
model = Embedding(input_dim = vocab_len+1, output_dim = 75, input_length = max_len)(input_word)
model = SpatialDropout1D(0.25)(model)
model = Bidirectional(LSTM(units = 25, return_sequences=True, recurrent_dropout = 0.2))(model)
out = TimeDistributed(Dense(num_labels, activation = "softmax"))(model)
model = Model(input_word, out)
【问题讨论】:
标签: python machine-learning keras nlp