Giter Site home page Giter Site logo

keras-crf's Introduction

keras-crf

Python package PyPI version Python

A more elegant and convenient CRF built on tensorflow-addons.

Python Compatibility is limited to tensorflow/addons, you can check the compatibility from it's home page.

Installation

pip install keras-crf

Usage

Here is an example to show you how to build a CRF model easily:

import tensorflow as tf

from keras_crf import CRFModel

# build backbone model, you can use large models like BERT
sequence_input = tf.keras.layers.Input(shape=(None,), dtype=tf.int32, name='sequence_input')
outputs = tf.keras.layers.Embedding(21128, 128)(sequence_input)
outputs = tf.keras.layers.Dense(256)(outputs)
base = tf.keras.Model(inputs=sequence_input, outputs=outputs)

# build CRFModel, 5 is num of tags
model = CRFModel(base, 5)

# no need to specify a loss for CRFModel, model will compute crf loss by itself
model.compile(
    optimizer=tf.keras.optimizers.Adam(3e-4)
    metrics=['acc'],
    )
model.summary()

# you can now train this model
model.fit(dataset, epochs=10, callbacks=None)

The model summary:

Model: "crf_model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
sequence_input (InputLayer)     [(None, None)]       0                                            
__________________________________________________________________________________________________
embedding (Embedding)           (None, None, 128)    2704384     sequence_input[0][0]             
__________________________________________________________________________________________________
dense (Dense)                   (None, None, 256)    33024       embedding[0][0]                  
__________________________________________________________________________________________________
crf (CRF)                       [(None, None), (None 1320        dense[0][0]                      
__________________________________________________________________________________________________
decode_sequence (Lambda)        (None, None)         0           crf[0][0]                        
__________________________________________________________________________________________________
potentials (Lambda)             (None, None, 5)      0           crf[0][1]                        
__________________________________________________________________________________________________
sequence_length (Lambda)        (None,)              0           crf[0][2]                        
__________________________________________________________________________________________________
kernel (Lambda)                 (5, 5)               0           crf[0][3]                        
==================================================================================================
Total params: 2,738,728
Trainable params: 2,738,728
Non-trainable params: 0
__________________________________________________________________________________________________

keras-crf's People

Contributors

luozhouyang avatar veelion avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.