Skip to content
/ tf2crf Public
forked from xuxingya/tf2crf

CRF layer for tensorflow 2 keras

License

Notifications You must be signed in to change notification settings

huoyo/tf2crf

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tf2crf

  • a simple CRF layer for tensorflow 2 keras
  • support keras masking

Install

$ pip install tf2crf

Tips

tensorflow >= 2.1.0 Recommmend use the latest tensorflow-addons which is compatiable with your tf version.

Example

from tf2CRF import CRF
from tensorflow.keras.layers import Input, Embedding, Bidirectional, GRU, Dense
from tensorflow.keras.models import Model

inputs = Input(shape=(None,), dtype='int32')
output = Embedding(100, 40, trainable=True, mask_zero=True)(inputs)
output = Bidirectional(GRU(64, return_sequences=True))(output)
output = Dense(9, activation=None)(output)
crf = CRF(dtype='float32')
output = crf(output)
model = Model(inputs, output)
model.compile(loss=crf.loss, optimizer='adam', metrics=[crf.accuracy])

x = [[5, 2, 3] * 3] * 10
y = [[1, 2, 3] * 3] * 10

model.fit(x=x, y=y, epochs=2, batch_size=2)
model.save('model')

Supoort for tensorflow mixed precision training

Currently these is a bug in tensorflow-addons.text.crf, which causes a dtype error when using miex precision. This bug has been fixed in master branch, but is not released. so if you want to use mixed precision training. You need to pip install tfa-nighly instead.

Example

from tf2CRF import CRF
from tensorflow.keras.layers import Input, Embedding, Bidirectional, GRU, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.mixed_precision import experimental as mixed_precision
policy = mixed_precision.Policy('mixed_float16')
mixed_precision.set_policy(policy)

inputs = Input(shape=(None,), dtype='int32')
output = Embedding(100, 40, trainable=True, mask_zero=True)(inputs)
output = Bidirectional(GRU(64, return_sequences=True))(output)
output = Dense(9, activation=None)(output)
crf = CRF(dtype='float32')
output = crf(output)
model = Model(inputs, output)
model.compile(loss=crf.loss, optimizer='adam', metrics=[crf.accuracy])

x = [[5, 2, 3] * 3] * 10
y = [[1, 2, 3] * 3] * 10

model.fit(x=x, y=y, epochs=2, batch_size=2)
model.save('model')

About

CRF layer for tensorflow 2 keras

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%