{"id":17998790,"url":"https://github.com/howl-anderson/tf_crf_layer","last_synced_at":"2025-03-26T06:31:19.514Z","repository":{"id":57474838,"uuid":"196967226","full_name":"howl-anderson/tf_crf_layer","owner":"howl-anderson","description":"CRF(Conditional Random Field) Layer for TensorFlow 1.X with many powerful functions","archived":false,"fork":false,"pushed_at":"2020-01-03T07:20:21.000Z","size":510,"stargazers_count":14,"open_issues_count":4,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-21T08:48:34.740Z","etag":null,"topics":["conditional-random-fields","crf","keras","keras-layer","keras-tensorflow","tensorflow","tensorflow-layers"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/howl-anderson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-15T09:25:06.000Z","updated_at":"2025-01-17T13:10:42.000Z","dependencies_parsed_at":"2022-09-10T02:23:08.847Z","dependency_job_id":null,"html_url":"https://github.com/howl-anderson/tf_crf_layer","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/howl-anderson%2Ftf_crf_layer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/howl-anderson%2Ftf_crf_layer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/howl-anderson%2Ftf_crf_layer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/howl-anderson%2Ftf_crf_layer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/howl-anderson","download_url":"https://codeload.github.com/howl-anderson/tf_crf_layer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245603657,"owners_count":20642862,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["conditional-random-fields","crf","keras","keras-layer","keras-tensorflow","tensorflow","tensorflow-layers"],"created_at":"2024-10-29T22:07:28.317Z","updated_at":"2025-03-26T06:31:18.959Z","avatar_url":"https://github.com/howl-anderson.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tf_crf_layer\n一个用于 TensorFlow 1.x 版本的 CRF keras layer\n\nNOTE: tensorflow-addons 包含适用于 TensorFlow 2.0 版本的 CRF keras layer\n\n## Functions\n### Vanilla CRF\n\nOrdinal liner chain CRF function.\n\n* Support START/END transfer probability learning.\n    * Which TensorFlow's `tf.contrib.crf` do not support\n\n```python\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Embedding, Bidirectional, LSTM\n\nfrom tf_crf_layer.layer import CRF\nfrom tf_crf_layer.loss import crf_loss\nfrom tf_crf_layer.metrics import crf_accuracy\n\nvocab = 3000\nEMBED_DIM = 300\nBiRNN_UNITS = 48\nclass_labels_number = 9\nEPOCHS = 10\n\ntrain_x, train_y = read_train_data()\ntest_x, test_y = read_test_data()\n\nmodel = Sequential()\nmodel.add(Embedding(len(vocab), EMBED_DIM, mask_zero=True)) \nmodel.add(Bidirectional(LSTM(BiRNN_UNITS // 2, return_sequences=True)))\nmodel.add(CRF(class_labels_number))\n\nmodel.compile('adam', loss=crf_loss, metrics=[crf_accuracy])\nmodel.fit(train_x, train_y, epochs=EPOCHS, validation_data=[test_x, test_y])\n```\n\nsee [conll2000_chunking_crf](examples/conll2000_chunking_crf.py) for a real application of vanilla CRF\n\n### CRF with static transfer constraint\n\nUser can pass a static transfer constraint to limit hidden states transfer probability.\nMainly used to greatly (not absolutely) reduce the probability of illegal hidden states sequence.\nSuch like  B -\u003e B in BILUO tag schema.\n\nStatic transfer constraint is first introduced by AllenNLP. I also learned this technology from it.\n\n```python\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Embedding, Bidirectional, LSTM\nfrom tf_crf_layer.crf_static_constraint_helper import allowed_transitions\n\nfrom tf_crf_layer.layer import CRF\nfrom tf_crf_layer.loss import crf_loss\nfrom tf_crf_layer.metrics import crf_accuracy\n\nvocab = 3000\nEMBED_DIM = 300\nBiRNN_UNITS = 48\nclass_labels_number = 9\nEPOCHS = 10\n\ntag_decoded_labels = get_tag_decoded_labels()\ntrain_x, train_y = read_train_data()\ntest_x, test_y = read_test_data()\n\nconstraints = allowed_transitions(\"BIO\", tag_decoded_labels)\n\nmodel = Sequential()\nmodel.add(Embedding(len(vocab), EMBED_DIM, mask_zero=True)) \nmodel.add(Bidirectional(LSTM(BiRNN_UNITS // 2, return_sequences=True)))\nmodel.add(CRF(class_labels_number, transition_constraint=constraints))\n\nmodel.compile('adam', loss=crf_loss, metrics=[crf_accuracy])\nmodel.fit(train_x, train_y, epochs=EPOCHS, validation_data=[test_x, test_y])\n```\n\n### CRF with dynamic transfer constraint\n\nDynamic transfer constraint is different from static transfer constraint that business logical may require apply some transfer constraint at running time.\nFor example, user have a intent or domain classifier which work pretty well.\nUser need implement a CRF based NER extractor. But not every entity of NER are illegal for that domain.\nFor example, location entity is illegal for music domain.\nBut user do not know such information at compile time, such information can only be get at running time from other component.\n\n```python\nfrom tensorflow.python.keras.models import Model\nfrom tensorflow.python.keras.layers import Embedding, Bidirectional, LSTM, Input\nfrom tf_crf_layer.crf_dynamic_constraint_helper import generate_constraint_table\n\nfrom tf_crf_layer.layer import CRF\nfrom tf_crf_layer.loss import crf_loss\nfrom tf_crf_layer.metrics import crf_accuracy\n\nvocab = 3000\nEMBED_DIM = 300\nBiRNN_UNITS = 48\nclass_labels_number = 9\nMAX_LEN = 24\nintent_number = 2\nEPOCHS = 10\n\ntag_decoded_labels = get_tag_decoded_labels()\ntrain_x, train_y = read_train_data()\ntest_x, test_y = read_test_data()\nconstraint_mapping = get_constraint_mapping()  # maping from intent to entity\n\nconstraint_table = generate_constraint_table(constraint_mapping, tag_decoded_labels)\n\nraw_input = Input(shape=(MAX_LEN,))\nembedding_layer = Embedding(vocab, EMBED_DIM, mask_zero=True)(raw_input)\nbilstm_layer = Bidirectional(LSTM(BiRNN_UNITS // 2, return_sequences=True))(embedding_layer)\n\ncrf_layer = CRF(\n    units=class_labels_number,\n    transition_constraint_matrix=constraint_table\n)\n\ndynamic_constraint_input = Input(shape=(intent_number,))\n\noutput_layer = crf_layer([bilstm_layer, dynamic_constraint_input])\n\nmodel = Model([raw_input, dynamic_constraint_input], output_layer)\n\n# print model summary\nmodel.summary()\n\nmodel.compile('adam', loss=crf_loss, metrics=[crf_accuracy])\nmodel.fit(train_x, train_y, epochs=EPOCHS, validation_data=[test_x, test_y])\n```\n\n# TODO\n* Add more metric according to http://www.davidsbatista.net/blog/2018/05/09/Named_Entity_Evaluation/","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhowl-anderson%2Ftf_crf_layer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhowl-anderson%2Ftf_crf_layer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhowl-anderson%2Ftf_crf_layer/lists"}