更新到带CRF的keras(基于keras的序列标注)

作者:luozhipeng   发表日期:2017-8-20  浏览:2,555次

更新Kears以便可以使用ChainCRF


pip3 uninstall keras
git clone https://github.com/phipleg/keras.git

cd keras

checkout crf
python setup.py build
python setup.py install

 

样例1:

import numpy as np

from keras import backend as K
from keras.models import Sequential
from keras.layers import Dense, ChainCRF, TimeDistributed

nb_classes=3
X = np.random.randn(10,5,11)
y = np.random.randint(0,nb_classes,size=(10,5))
y = np.expand_dims(y, -1)
y_mask = np.random.randint(0,2,size=(10,))
X.shape, y.shape, y_mask.shape

model = Sequential()
model.add(TimeDistributed(Dense(nb_classes),
 input_shape=(5,11), name="temporal_dense"))
crf = ChainCRF()
model.add(crf)
model.summary()

model.compile(loss=crf.sparse_loss, optimizer='sgd')
model.fit(X, y, sample_weight=y_mask, nb_epoch=1)

 

样例2:

n_words = 10000
maxlen = 32
(X_train, y_train), (X_test, y_test) = load_treebank(nb_words=n_words, maxlen=maxlen)

n_samples, n_steps, n_classes = y_train.shape

model = Sequential()
model.add(Embedding(n_words, 128, input_length=maxlen, dropout=0.2))
model.add(LSTM(64, return_sequences=True))
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(n_classes)))
model.add(Dropout(0.2))
crf = ChainCRF()
model.add(crf)
model.compile(loss=crf.loss, optimizer='rmsprop', metrics=['accuracy'])

 

 

样例3:

vocab_size = 20
n_classes = 11
model = Sequential()
model.add(Embedding(vocab_size, n_classes))
layer = crf.ChainCRF()
model.add(layer)
model.compile(loss=layer.sparse_loss, optimizer='sgd')

batch_size, maxlen = 2, 5
x = np.random.randint(1, vocab_size, size=(batch_size, maxlen))
y = np.random.randint(n_classes, size=(batch_size, maxlen))
y = np.expand_dims(y, 2)
model.train_on_batch(x, y)

本文固定链接: http://www.luozhipeng.com/?p=1242
转载请注明: luozhipeng 2017-8-20 于 罗志鹏的BLOG 发表

上一篇: :下一篇
返回顶部