{"id":13534979,"url":"https://github.com/HighCWu/keras-bert-tpu","last_synced_at":"2025-04-02T00:31:35.280Z","repository":{"id":62573954,"uuid":"160133665","full_name":"HighCWu/keras-bert-tpu","owner":"HighCWu","description":"Implementation of BERT that could load official pre-trained models for feature extraction and prediction on TPU","archived":false,"fork":false,"pushed_at":"2019-02-24T01:13:48.000Z","size":11024,"stargazers_count":17,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-09-25T11:26:03.026Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HighCWu.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":"2018-12-03T04:42:58.000Z","updated_at":"2024-04-18T02:27:06.000Z","dependencies_parsed_at":"2022-11-03T18:33:55.366Z","dependency_job_id":null,"html_url":"https://github.com/HighCWu/keras-bert-tpu","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/HighCWu%2Fkeras-bert-tpu","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HighCWu%2Fkeras-bert-tpu/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HighCWu%2Fkeras-bert-tpu/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HighCWu%2Fkeras-bert-tpu/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HighCWu","download_url":"https://codeload.github.com/HighCWu/keras-bert-tpu/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222784453,"owners_count":17037192,"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":[],"created_at":"2024-08-01T08:00:47.949Z","updated_at":"2025-04-02T00:31:35.269Z","avatar_url":"https://github.com/HighCWu.png","language":"Python","funding_links":[],"categories":["other resources for BERT:","Other Resources"],"sub_categories":["Other"],"readme":"# Keras BERT TPU\n\n[![Travis](https://travis-ci.org/HighCWu/keras-bert-tpu.svg?branch=master)](https://travis-ci.org/HighCWu/keras-bert-tpu)\n[![Coverage](https://coveralls.io/repos/github/HighCWu/keras-bert-tpu/badge.svg?branch=master)](https://coveralls.io/github/HighCWu/keras-bert-tpu)\n\nThis is a fork of [CyberZHG/keras_bert](https://github.com/CyberZHG/keras-bert) which supports Keras BERT on TPU.\n\nImplementation of the [BERT](https://arxiv.org/pdf/1810.04805.pdf). Official pre-trained models could be loaded for feature extraction and prediction.\n## Colab Demo\n\n[HighCWu/keras-bert-tpu](https://colab.research.google.com/github/HighCWu/keras-bert-tpu/blob/master/demo/load_model/load_and_predict.ipynb)\n\n\n## Install\n\n```bash\npip install keras-bert-tpu\n```\n\n## Usage\n\n### Load Official Pre-trained Models\n\nIn [feature extraction demo](./demo/load_model/load_and_extract.py), you should be able to get the same extraction result as the official model. And in [prediction demo](./demo/load_model/load_and_predict.py), the missing word in the sentence could be predicted.\n\n### Train \u0026 Use\n\n```python\nfrom keras_bert import get_base_dict, get_model, gen_batch_inputs\n\n\n# A toy input example\nsentence_pairs = [\n    [['all', 'work', 'and', 'no', 'play'], ['makes', 'jack', 'a', 'dull', 'boy']],\n    [['from', 'the', 'day', 'forth'], ['my', 'arm', 'changed']],\n    [['and', 'a', 'voice', 'echoed'], ['power', 'give', 'me', 'more', 'power']],\n]\n\n\n# Build token dictionary\ntoken_dict = get_base_dict()  # A dict that contains some special tokens\nfor pairs in sentence_pairs:\n    for token in pairs[0] + pairs[1]:\n        if token not in token_dict:\n            token_dict[token] = len(token_dict)\ntoken_list = list(token_dict.keys())  # Used for selecting a random word\n\n\n# Build \u0026 train the model\nmodel = get_model(\n    token_num=len(token_dict),\n    head_num=5,\n    transformer_num=12,\n    embed_dim=25,\n    feed_forward_dim=100,\n    seq_len=20,\n    pos_num=20,\n    dropout_rate=0.05,\n)\nmodel.summary()\n\ndef _generator():\n    while True:\n        yield gen_batch_inputs(\n            sentence_pairs,\n            token_dict,\n            token_list,\n            seq_len=20,\n            mask_rate=0.3,\n            swap_sentence_rate=1.0,\n        )\n\nmodel.fit_generator(\n    generator=_generator(),\n    steps_per_epoch=1000,\n    epochs=100,\n    validation_data=_generator(),\n    validation_steps=100,\n    callbacks=[\n        keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)\n    ],\n)\n\n\n# Use the trained model\ninputs, output_layer = get_model(  # `output_layer` is the last feature extraction layer (the last transformer)\n    token_num=len(token_dict),\n    head_num=5,\n    transformer_num=12,\n    embed_dim=25,\n    feed_forward_dim=100,\n    seq_len=20,\n    pos_num=20,\n    dropout_rate=0.05,\n    training=False,  # The input layers and output layer will be returned if `training` is `False`\n)\n```\n\n### Custom Feature Extraction\n\n```python\ndef _custom_layers(x, trainable=True):\n    return keras.layers.LSTM(\n        units=768,\n        trainable=trainable,\n        name='LSTM',\n    )(x)\n\nmodel = get_model(\n    token_num=200,\n    embed_dim=768,\n    custom_layers=_custom_layers,\n)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FHighCWu%2Fkeras-bert-tpu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FHighCWu%2Fkeras-bert-tpu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FHighCWu%2Fkeras-bert-tpu/lists"}