{"id":16194700,"url":"https://github.com/hironsan/keras-crf-layer","last_synced_at":"2025-08-11T21:31:39.791Z","repository":{"id":63603296,"uuid":"102264487","full_name":"Hironsan/keras-crf-layer","owner":"Hironsan","description":"Implementation of CRF layer in Keras.","archived":false,"fork":false,"pushed_at":"2017-09-19T11:12:47.000Z","size":11,"stargazers_count":74,"open_issues_count":2,"forks_count":31,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-12-02T02:04:43.079Z","etag":null,"topics":["crf","deep-learning","keras","machine-learning","natural-language-processing"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Hironsan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-03T12:54:38.000Z","updated_at":"2024-02-29T04:50:02.000Z","dependencies_parsed_at":"2022-11-22T07:21:04.816Z","dependency_job_id":null,"html_url":"https://github.com/Hironsan/keras-crf-layer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hironsan%2Fkeras-crf-layer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hironsan%2Fkeras-crf-layer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hironsan%2Fkeras-crf-layer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hironsan%2Fkeras-crf-layer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Hironsan","download_url":"https://codeload.github.com/Hironsan/keras-crf-layer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229608528,"owners_count":18098039,"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":["crf","deep-learning","keras","machine-learning","natural-language-processing"],"created_at":"2024-10-10T08:24:37.642Z","updated_at":"2024-12-13T19:59:53.806Z","avatar_url":"https://github.com/Hironsan.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Keras-CRF-Layer\nThe Keras-CRF-Layer module implements a linear-chain CRF layer for learning to predict tag sequences.\nThis variant of the CRF is factored into unary potentials for every element in the sequence and binary potentials for every transition between output tags.\n\n## Usage\nBelow is an example of the API, which learns a CRF for some random data.\nThe linear layer in the example can be replaced by any neural network.\n\n```python\nimport numpy as np\nfrom keras.layers import Embedding, Input\nfrom keras.models import Model\n\nfrom crf import CRFLayer\n\n# Hyperparameter settings.\nvocab_size = 20\nn_classes = 11\nbatch_size = 2\nmaxlen = 2\n\n# Random features.\nx = np.random.randint(1, vocab_size, size=(batch_size, maxlen))\n\n# Random tag indices representing the gold sequence.\ny = np.random.randint(n_classes, size=(batch_size, maxlen))\ny = np.eye(n_classes)[y]\n\n# All sequences in this example have the same length, but they can be variable in a real model.\ns = np.asarray([maxlen] * batch_size, dtype='int32')\n\n# Build an example model.\nword_ids = Input(batch_shape=(batch_size, maxlen), dtype='int32')\nsequence_lengths = Input(batch_shape=[batch_size, 1], dtype='int32')\n\nword_embeddings = Embedding(vocab_size, n_classes)(word_ids)\ncrf = CRFLayer()\npred = crf(inputs=[word_embeddings, sequence_lengths])\nmodel = Model(inputs=[word_ids, sequence_lengths], outputs=[pred])\nmodel.compile(loss=crf.loss, optimizer='sgd')\n\n# Train first 1 batch.\nmodel.train_on_batch([x, s], y)\n\n# Save the model\nmodel.save('model.h5')\n```\n\n### Model loading                                                                                                       \nWhen you want to load a saved model that has a crf output, then loading\nthe model with 'keras.models.load_model' won't work properly because\nthe reference of the loss function to the transition parameters is lost. To\nfix this, you need to use the parameter 'custom_objects' as follows: \n\n```python\nfrom keras.models import load_model\n\nfrom crf import create_custom_objects\n\nmodel = load_model('model.h5', custom_objects=create_custom_objects())\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhironsan%2Fkeras-crf-layer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhironsan%2Fkeras-crf-layer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhironsan%2Fkeras-crf-layer/lists"}