{"id":13606281,"url":"https://github.com/amaiya/ktrain","last_synced_at":"2025-04-29T18:53:11.908Z","repository":{"id":40381092,"uuid":"169442310","full_name":"amaiya/ktrain","owner":"amaiya","description":"ktrain is a Python library that makes deep learning and AI more accessible and easier to apply","archived":false,"fork":false,"pushed_at":"2025-02-06T20:19:28.000Z","size":113307,"stargazers_count":1255,"open_issues_count":0,"forks_count":269,"subscribers_count":32,"default_branch":"master","last_synced_at":"2025-04-10T00:12:27.043Z","etag":null,"topics":["computer-vision","deep-learning","graph-neural-networks","keras","machine-learning","nlp","python","tabular-data","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/amaiya.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-06T17:01:39.000Z","updated_at":"2025-04-07T12:29:48.000Z","dependencies_parsed_at":"2024-06-16T03:28:40.394Z","dependency_job_id":"7365c85a-8843-4e5c-a646-0e4b7c79f0da","html_url":"https://github.com/amaiya/ktrain","commit_stats":{"total_commits":2548,"total_committers":18,"mean_commits":"141.55555555555554","dds":"0.22370486656200939","last_synced_commit":"a3dcf08fce9e30b8c837cf97c5610e7b09f8360c"},"previous_names":[],"tags_count":171,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amaiya%2Fktrain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amaiya%2Fktrain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amaiya%2Fktrain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amaiya%2Fktrain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amaiya","download_url":"https://codeload.github.com/amaiya/ktrain/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251565557,"owners_count":21609972,"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":["computer-vision","deep-learning","graph-neural-networks","keras","machine-learning","nlp","python","tabular-data","tensorflow"],"created_at":"2024-08-01T19:01:07.745Z","updated_at":"2025-04-29T18:53:11.887Z","avatar_url":"https://github.com/amaiya.png","language":"Jupyter Notebook","readme":"### [Overview](#overview) | [Tutorials](#tutorials) | [Examples](#examples) |  [Installation](#installation) | [FAQ](https://github.com/amaiya/ktrain/blob/master/FAQ.md) | [API Docs](https://amaiya.github.io/ktrain/index.html) |  [How to Cite](#how-to-cite)\n[![PyPI Status](https://badge.fury.io/py/ktrain.svg)](https://badge.fury.io/py/ktrain) [![ktrain python compatibility](https://img.shields.io/pypi/pyversions/ktrain.svg)](https://pypi.python.org/pypi/ktrain) [![license](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/amaiya/ktrain/blob/master/LICENSE) [![Downloads](https://static.pepy.tech/badge/ktrain)](https://pepy.tech/project/ktrain)\n\u003c!--[![Twitter URL](https://img.shields.io/twitter/url/https/twitter.com/ktrain_ai.svg?style=social\u0026label=Follow%20%40ktrain_ai)](https://twitter.com/ktrain_ai)--\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://github.com/amaiya/ktrain/raw/master/ktrain_logo_200x100.png\" width=\"200\"/\u003e\n\u003c/p\u003e\n\n# Welcome to ktrain\n\u003e a \"Swiss Army knife\" for machine learning\n\n\n\n### News and Announcements\n- **2024-02-20**\n  - **ktrain 0.41.x** is released and removes the `ktrain.text.qa.generative_qa` module.  Our [OnPrem.LLM](https://github.com/amaiya/onprem) package should be used for Generative Question-Answering tasks. See [example notebook](https://amaiya.github.io/onprem/examples_rag.html).\n----\n\n### Overview\n\n**ktrain** is a lightweight wrapper for the deep learning library [TensorFlow Keras](https://www.tensorflow.org/guide/keras/overview) (and other libraries) to help build, train, and deploy neural networks and other machine learning models.  Inspired by ML framework extensions like *fastai* and *ludwig*, **ktrain** is designed to make deep learning and AI more accessible and easier to apply for both newcomers and experienced practitioners. With only a few lines of code, **ktrain** allows you to easily and quickly:\n\n- employ fast, accurate, and easy-to-use pre-canned models for  `text`, `vision`, `graph`, and `tabular` data:\n  - `text` data:\n     - **Text Classification**: [BERT](https://arxiv.org/abs/1810.04805), [DistilBERT](https://arxiv.org/abs/1910.01108), [NBSVM](https://www.aclweb.org/anthology/P12-2018), [fastText](https://arxiv.org/abs/1607.01759), and other models \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/IMDb-BERT.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Text Regression**: [BERT](https://arxiv.org/abs/1810.04805), [DistilBERT](https://arxiv.org/abs/1910.01108), Embedding-based linear text regression, [fastText](https://arxiv.org/abs/1607.01759), and other models \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/text_regression_example.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Sequence Labeling (NER)**:  Bidirectional LSTM with optional [CRF layer](https://arxiv.org/abs/1603.01360) and various embedding schemes such as pretrained [BERT](https://huggingface.co/transformers/pretrained_models.html) and [fasttext](https://fasttext.cc/docs/en/crawl-vectors.html) word embeddings and character embeddings \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/CoNLL2002_Dutch-BiLSTM.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Ready-to-Use NER models for English, Chinese, and Russian** with no training required \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/shallownlp-examples.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Sentence Pair Classification**  for tasks like paraphrase detection \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/MRPC-BERT.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Unsupervised Topic Modeling** with [LDA](http://www.jmlr.org/papers/volume3/blei03a/blei03a.pdf)  \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/20newsgroups-topic_modeling.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Document Similarity with One-Class Learning**:  given some documents of interest, find and score new documents that are thematically similar to them using [One-Class Text Classification](https://en.wikipedia.org/wiki/One-class_classification) \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/20newsgroups-document_similarity_scorer.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Document Recommendation Engines and Semantic Searches**:  given a text snippet from a sample document, recommend documents that are semantically-related from a larger corpus  \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/20newsgroups-recommendation_engine.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Text Summarization**:  summarize long documents - no training required \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/text_summarization.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Extractive Question-Answering**:  ask a large text corpus questions and receive exact answers using BERT \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/question_answering_with_bert.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Generative Question-Answering**:  ask a large text corpus questions and receive answers with citations using local or OpenAI models \u003csub\u003e\u003csup\u003e[[example notebook](https://amaiya.github.io/onprem/examples_rag.html)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Easy-to-Use Built-In Search Engine**:  perform keyword searches on large collections of documents \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/question_answering_with_bert.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Zero-Shot Learning**:  classify documents into user-provided topics **without** training examples \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/zero_shot_learning_with_nli.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Language Translation**:  translate text from one language to another \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/language_translation_example.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Text Extraction**: Extract text from PDFs, Word documents, etc. \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/text_extraction_example.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Speech Transcription**: Extract text from audio files \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/develop/examples/text/speech_transcription_example.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Universal Information Extraction**:  extract any kind of information from documents by simply phrasing it in the form of a question \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/qa_information_extraction.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Keyphrase Extraction**:  extract keywords from documents \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/develop/examples/text/keyword_extraction_example.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n     - **Sentiment Analysis**: easy-to-use wrapper to pretrained sentiment analysis \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/develop/examples/text/sentiment_analysis_example.ipynb)]\u003c/sup\u003e\n     - **Generative AI with GPT**: Provide instructions to a lightweight ChatGPT-like model running on your own own machine to solve various tasks. \u003csub\u003e\u003csup\u003e[[example notebook](https://amaiya.github.io/onprem/examples.html)]\u003c/sup\u003e\n  - `vision` data:\n    - **image classification** (e.g., [ResNet](https://arxiv.org/abs/1512.03385), [Wide ResNet](https://arxiv.org/abs/1605.07146), [Inception](https://www.cs.unc.edu/~wliu/papers/GoogLeNet.pdf)) \u003csub\u003e\u003csup\u003e[[example notebook](https://colab.research.google.com/drive/1WipQJUPL7zqyvLT10yekxf_HNMXDDtyR)]\u003c/sup\u003e\u003c/sub\u003e\n    - **image regression** for predicting numerical targets from photos (e.g., age prediction) \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/vision/utk_faces_age_prediction-resnet50.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n    - **image captioning** with a pretrained model \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/develop/examples/vision/image_captioning_example.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n    - **object detection** with a pretrained model \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/develop/examples/vision/object_detection_example.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n  - `graph` data:\n    - **node classification** with graph neural networks ([GraphSAGE](https://cs.stanford.edu/people/jure/pubs/graphsage-nips17.pdf)) \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/graphs/pubmed_node_classification-GraphSAGE.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n    - **link prediction** with graph neural networks ([GraphSAGE](https://cs.stanford.edu/people/jure/pubs/graphsage-nips17.pdf)) \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/graphs/cora_link_prediction-GraphSAGE.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n  - `tabular` data:\n    - **tabular classification** (e.g., Titanic survival prediction) \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/tutorials/tutorial-08-tabular_classification_and_regression.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n    - **tabular regression** (e.g., predicting house prices) \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/tabular/HousePricePrediction-MLP.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n    - **causal inference** using meta-learners \u003csub\u003e\u003csup\u003e[[example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/develop/examples/tabular/causal_inference_example.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n\n- estimate an optimal learning rate for your model given your data using a Learning Rate Finder\n- utilize learning rate schedules such as the [triangular policy](https://arxiv.org/abs/1506.01186), the [1cycle policy](https://arxiv.org/abs/1803.09820), and [SGDR](https://arxiv.org/abs/1608.03983) to effectively minimize loss and improve generalization\n- build text classifiers for any language (e.g., [Arabic Sentiment Analysis with BERT](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/ArabicHotelReviews-AraBERT.ipynb), [Chinese Sentiment Analysis with NBSVM](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/ChineseHotelReviews-nbsvm.ipynb))\n- easily train NER models for any language (e.g., [Dutch NER](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/CoNLL2002_Dutch-BiLSTM.ipynb) )\n- load and preprocess text and image data from a variety of formats\n- inspect data points that were misclassified and [provide explanations](https://eli5.readthedocs.io/en/latest/) to help improve your model\n- leverage a simple prediction API for saving and deploying both models and data-preprocessing steps to make predictions on new raw data\n- built-in support for exporting models to [ONNX](https://onnx.ai/) and  [TensorFlow Lite](https://www.tensorflow.org/lite) (see [example notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/develop/examples/text/ktrain-ONNX-TFLite-examples.ipynb) for more information)\n\n\n\n### Tutorials\nPlease see the following tutorial notebooks for a guide on how to use **ktrain** on your projects:\n* Tutorial 1:  [Introduction](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/tutorials/tutorial-01-introduction.ipynb)\n* Tutorial 2:  [Tuning Learning Rates](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/tutorials/tutorial-02-tuning-learning-rates.ipynb)\n* Tutorial 3: [Image Classification](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/tutorials/tutorial-03-image-classification.ipynb)\n* Tutorial 4: [Text Classification](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/tutorials/tutorial-04-text-classification.ipynb)\n* Tutorial 5: [Learning from Unlabeled Text Data](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/tutorials/tutorial-05-learning_from_unlabeled_text_data.ipynb)\n* Tutorial 6: [Text Sequence Tagging](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/tutorials/tutorial-06-sequence-tagging.ipynb) for Named Entity Recognition\n* Tutorial 7: [Graph Node Classification](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/tutorials/tutorial-07-graph-node_classification.ipynb) with Graph Neural Networks\n* Tutorial 8: [Tabular Classification and Regression](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/tutorials/tutorial-08-tabular_classification_and_regression.ipynb)\n* Tutorial A1: [Additional tricks](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/tutorials/tutorial-A1-additional-tricks.ipynb), which covers topics such as previewing data augmentation schemes, inspecting intermediate output of Keras models for debugging, setting global weight decay, and use of built-in and custom callbacks.\n* Tutorial A2: [Explaining Predictions and Misclassifications](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/tutorials/tutorial-A2-explaining-predictions.ipynb)\n* Tutorial A3: [Text Classification with Hugging Face Transformers](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/develop/tutorials/tutorial-A3-hugging_face_transformers.ipynb)\n* Tutorial A4: [Using Custom Data Formats and Models: Text Regression with Extra Regressors](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/tutorials/tutorial-A4-customdata-text_regression_with_extra_regressors.ipynb)\n\n\nSome blog tutorials and other guides about **ktrain** are shown below:\n\n\u003e [**ktrain: A Lightweight Wrapper for Keras to Help Train Neural Networks**](https://towardsdatascience.com/ktrain-a-lightweight-wrapper-for-keras-to-help-train-neural-networks-82851ba889c)\n\n\n\u003e [**BERT Text Classification in 3 Lines of Code**](https://towardsdatascience.com/bert-text-classification-in-3-lines-of-code-using-keras-264db7e7a358)\n\n\u003e [**Text Classification with Hugging Face Transformers in  TensorFlow 2 (Without Tears)**](https://medium.com/@asmaiya/text-classification-with-hugging-face-transformers-in-tensorflow-2-without-tears-ee50e4f3e7ed)\n\n\u003e [**Build an Open-Domain Question-Answering System With BERT in 3 Lines of Code**](https://towardsdatascience.com/build-an-open-domain-question-answering-system-with-bert-in-3-lines-of-code-da0131bc516b)\n\n\u003e [**Finetuning BERT using ktrain for Disaster Tweets Classification**](https://medium.com/analytics-vidhya/finetuning-bert-using-ktrain-for-disaster-tweets-classification-18f64a50910b) by Hamiz Ahmed\n\n\u003e [**Indonesian NLP Examples with ktrain**](https://github.com/ilos-vigil/ktrain-assessment-study) by Sandy Khosasi\n\n\n\n\n\n\n\n\n\n### Examples\n\nUsing **ktrain** on **Google Colab**?  See these Colab examples:\n-  **text classification:** [a simple demo of Multiclass Text Classification with BERT](https://colab.research.google.com/drive/1AH3fkKiEqBpVpO5ua00scp7zcHs5IDLK)\n-  **text classification:** [a simple demo of Multiclass Text Classification with Hugging Face Transformers](https://colab.research.google.com/drive/1YxcceZxsNlvK35pRURgbwvkgejXwFxUt)\n- **sequence-tagging (NER):** [NER example using `transformer` word embeddings](https://colab.research.google.com/drive/1whrnmM7ElqbaEhXf760eiOMiYk5MNO-Z?usp=sharing)\n- **question-answering:** [End-to-End Question-Answering](https://colab.research.google.com/drive/1tcsEQ7igx7lw_R0Pfpmsg9Wf3DEXyOvk?usp=sharing) using the 20newsgroups dataset.\n-  **image classification:** [image classification with Cats vs. Dogs](https://colab.research.google.com/drive/1WipQJUPL7zqyvLT10yekxf_HNMXDDtyR)\n\n\n\nTasks such as text classification and image classification can be accomplished easily with\nonly a few lines of code.\n\n#### Example: Text Classification of [IMDb Movie Reviews](https://ai.stanford.edu/~amaas/data/sentiment/) Using [BERT](https://arxiv.org/pdf/1810.04805.pdf) \u003csub\u003e\u003csup\u003e[[see notebook](https://github.com/amaiya/ktrain/blob/master/examples/text/IMDb-BERT.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n```python\nimport ktrain\nfrom ktrain import text as txt\n\n# load data\n(x_train, y_train), (x_test, y_test), preproc = txt.texts_from_folder('data/aclImdb', maxlen=500,\n                                                                     preprocess_mode='bert',\n                                                                     train_test_names=['train', 'test'],\n                                                                     classes=['pos', 'neg'])\n\n# load model\nmodel = txt.text_classifier('bert', (x_train, y_train), preproc=preproc)\n\n# wrap model and data in ktrain.Learner object\nlearner = ktrain.get_learner(model,\n                             train_data=(x_train, y_train),\n                             val_data=(x_test, y_test),\n                             batch_size=6)\n\n# find good learning rate\nlearner.lr_find()             # briefly simulate training to find good learning rate\nlearner.lr_plot()             # visually identify best learning rate\n\n# train using 1cycle learning rate schedule for 3 epochs\nlearner.fit_onecycle(2e-5, 3)\n```\n\n\n#### Example: Classifying Images of [Dogs and Cats](https://www.kaggle.com/c/dogs-vs-cats) Using a Pretrained [ResNet50](https://arxiv.org/abs/1512.03385) model \u003csub\u003e\u003csup\u003e[[see notebook](https://colab.research.google.com/drive/1WipQJUPL7zqyvLT10yekxf_HNMXDDtyR)]\u003c/sup\u003e\u003c/sub\u003e\n```python\nimport ktrain\nfrom ktrain import vision as vis\n\n# load data\n(train_data, val_data, preproc) = vis.images_from_folder(\n                                              datadir='data/dogscats',\n                                              data_aug = vis.get_data_aug(horizontal_flip=True),\n                                              train_test_names=['train', 'valid'],\n                                              target_size=(224,224), color_mode='rgb')\n\n# load model\nmodel = vis.image_classifier('pretrained_resnet50', train_data, val_data, freeze_layers=80)\n\n# wrap model and data in ktrain.Learner object\nlearner = ktrain.get_learner(model=model, train_data=train_data, val_data=val_data,\n                             workers=8, use_multiprocessing=False, batch_size=64)\n\n# find good learning rate\nlearner.lr_find()             # briefly simulate training to find good learning rate\nlearner.lr_plot()             # visually identify best learning rate\n\n# train using triangular policy with ModelCheckpoint and implicit ReduceLROnPlateau and EarlyStopping\nlearner.autofit(1e-4, checkpoint_folder='/tmp/saved_weights')\n```\n\n#### Example: Sequence Labeling for [Named Entity Recognition](https://www.kaggle.com/abhinavwalia95/entity-annotated-corpus/version/2) using a randomly initialized [Bidirectional LSTM CRF](https://arxiv.org/abs/1603.01360) model \u003csub\u003e\u003csup\u003e[[see notebook](https://github.com/amaiya/ktrain/blob/master/examples/text/CoNLL2003-BiLSTM_CRF.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n```python\nimport ktrain\nfrom ktrain import text as txt\n\n# load data\n(trn, val, preproc) = txt.entities_from_txt('data/ner_dataset.csv',\n                                            sentence_column='Sentence #',\n                                            word_column='Word',\n                                            tag_column='Tag',\n                                            data_format='gmb',\n                                            use_char=True) # enable character embeddings\n\n# load model\nmodel = txt.sequence_tagger('bilstm-crf', preproc)\n\n# wrap model and data in ktrain.Learner object\nlearner = ktrain.get_learner(model, train_data=trn, val_data=val)\n\n\n# conventional training for 1 epoch using a learning rate of 0.001 (Keras default for Adam optmizer)\nlearner.fit(1e-3, 1)\n```\n\n\n#### Example: Node Classification on [Cora Citation Graph](https://linqs-data.soe.ucsc.edu/public/lbc/cora.tgz) using a [GraphSAGE](https://arxiv.org/abs/1706.02216) model \u003csub\u003e\u003csup\u003e[[see notbook](https://github.com/amaiya/ktrain/blob/master/examples/graphs/cora_node_classification-GraphSAGE.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n```python\nimport ktrain\nfrom ktrain import graph as gr\n\n# load data with supervision ratio of 10%\n(trn, val, preproc)  = gr.graph_nodes_from_csv(\n                                               'cora.content', # node attributes/labels\n                                               'cora.cites',   # edge list\n                                               sample_size=20,\n                                               holdout_pct=None,\n                                               holdout_for_inductive=False,\n                                              train_pct=0.1, sep='\\t')\n\n# load model\nmodel=gr.graph_node_classifier('graphsage', trn)\n\n# wrap model and data in ktrain.Learner object\nlearner = ktrain.get_learner(model, train_data=trn, val_data=val, batch_size=64)\n\n\n# find good learning rate\nlearner.lr_find(max_epochs=100) # briefly simulate training to find good learning rate\nlearner.lr_plot()               # visually identify best learning rate\n\n# train using triangular policy with ModelCheckpoint and implicit ReduceLROnPlateau and EarlyStopping\nlearner.autofit(0.01, checkpoint_folder='/tmp/saved_weights')\n```\n\n\n#### Example: Text Classification with [Hugging Face Transformers](https://github.com/huggingface/transformers) on [20 Newsgroups Dataset](https://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html) Using [DistilBERT](https://arxiv.org/abs/1910.01108) \u003csub\u003e\u003csup\u003e[[see notebook](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/tutorials/tutorial-A3-hugging_face_transformers.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n```python\n# load text data\ncategories = ['alt.atheism', 'soc.religion.christian','comp.graphics', 'sci.med']\nfrom sklearn.datasets import fetch_20newsgroups\ntrain_b = fetch_20newsgroups(subset='train', categories=categories, shuffle=True)\ntest_b = fetch_20newsgroups(subset='test',categories=categories, shuffle=True)\n(x_train, y_train) = (train_b.data, train_b.target)\n(x_test, y_test) = (test_b.data, test_b.target)\n\n# build, train, and validate model (Transformer is wrapper around transformers library)\nimport ktrain\nfrom ktrain import text\nMODEL_NAME = 'distilbert-base-uncased'\nt = text.Transformer(MODEL_NAME, maxlen=500, class_names=train_b.target_names)\ntrn = t.preprocess_train(x_train, y_train)\nval = t.preprocess_test(x_test, y_test)\nmodel = t.get_classifier()\nlearner = ktrain.get_learner(model, train_data=trn, val_data=val, batch_size=6)\nlearner.fit_onecycle(5e-5, 4)\nlearner.validate(class_names=t.get_classes()) # class_names must be string values\n\n# Output from learner.validate()\n#                        precision    recall  f1-score   support\n#\n#           alt.atheism       0.92      0.93      0.93       319\n#         comp.graphics       0.97      0.97      0.97       389\n#               sci.med       0.97      0.95      0.96       396\n#soc.religion.christian       0.96      0.96      0.96       398\n#\n#              accuracy                           0.96      1502\n#             macro avg       0.95      0.96      0.95      1502\n#          weighted avg       0.96      0.96      0.96      1502\n```\n\n\u003c!--\n#### Example: NER With [BioBERT](https://arxiv.org/abs/1901.08746) Embeddings\n```python\n# NER with BioBERT embeddings\nimport ktrain\nfrom ktrain import text as txt\nx_train= [['IL-2', 'responsiveness', 'requires', 'three', 'distinct', 'elements', 'within', 'the', 'enhancer', '.'], ...]\ny_train=[['B-protein', 'O', 'O', 'O', 'O', 'B-DNA', 'O', 'O', 'B-DNA', 'O'], ...]\n(trn, val, preproc) = txt.entities_from_array(x_train, y_train)\nmodel = txt.sequence_tagger('bilstm-bert', preproc, bert_model='monologg/biobert_v1.1_pubmed')\nlearner = ktrain.get_learner(model, train_data=trn, val_data=val, batch_size=128)\nlearner.fit(0.01, 1, cycle_len=5)\n```\n--\u003e\n\n#### Example: Tabular Classification for [Titanic Survival Prediction](https://www.kaggle.com/c/titanic) Using an MLP  \u003csub\u003e\u003csup\u003e[[see notebook](https://github.com/amaiya/ktrain/blob/master/examples/tabular/tabular_classification_and_regression_example.ipynb)]\u003c/sup\u003e\u003c/sub\u003e\n```python\nimport ktrain\nfrom ktrain import tabular\nimport pandas as pd\ntrain_df = pd.read_csv('train.csv', index_col=0)\ntrain_df = train_df.drop(['Name', 'Ticket', 'Cabin'], 1)\ntrn, val, preproc = tabular.tabular_from_df(train_df, label_columns=['Survived'], random_state=42)\nlearner = ktrain.get_learner(tabular.tabular_classifier('mlp', trn), train_data=trn, val_data=val)\nlearner.lr_find(show_plot=True, max_epochs=5) # estimate learning rate\nlearner.fit_onecycle(5e-3, 10)\n\n# evaluate held-out labeled test set\ntst = preproc.preprocess_test(pd.read_csv('heldout.csv', index_col=0))\nlearner.evaluate(tst, class_names=preproc.get_classes())\n```\n\n\n\n\n\n\n\n#### Additional examples can be found [here](https://github.com/amaiya/ktrain/tree/master/examples).\n\n\n\n### Installation\n\n1. Make sure pip is up-to-date with: `pip install -U pip`\n\n2. [Install TensorFlow 2](https://www.tensorflow.org/install) if it is not already installed (e.g., `pip install tensorflow`). \n\n3. Install *ktrain*: `pip install ktrain`\n\n4. If using `tensorflow\u003e=2.16`:\n    - Install **tf_keras**: `pip install tf_keras`\n    - Set the environment variable `TF_USE_LEGACY_KERAS` to true before importing **ktrain**\n\n\nThe above should be all you need on Linux systems and cloud computing environments like Google Colab and AWS EC2.  If you are using **ktrain** on a **Windows computer**, you can follow these\n[more detailed instructions](https://github.com/amaiya/ktrain/blob/master/FAQ.md#how-do-i-install-ktrain-on-a-windows-machine) that include some extra steps.\n\n\n#### Notes about TensorFlow Versions\n- As of `tensorflow\u003e=2.11`, you must only use legacy optimizers such as `tf.keras.optimizers.legacy.Adam`.  The newer `tf.keras.optimizers.Optimizer` base class is not supported at this time.  For instance, when using TensorFlow 2.11 and above, please use `tf.keras.optimzers.legacy.Adam()` instead of the string `\"adam\"` in `model.compile`. **ktrain** does this automatically when using out-of-the-box models (e.g., models from the `transformers` library).\n- As mentioned above, due to breaking changes in TensorFlow 2.16, you will need to install the `tf_keras` package and also set the environment variable `TF_USE_LEGACY_KERAS=True` before importing **ktrain** (e.g., add `export TF_USE_LEGACY_KERAS=1` in `.bashrc` or add `os.environ['TF_USE_LEGACY_KERAS']=\"1\"` at top of your code, etc.).\n\n#### Additional Notes About Installation\n\n- Some optional, extra libraries used for some operations can be installed as needed. (Notice that **ktrain** is using forked versions of the `eli5` and `stellargraph` libraries in order to support TensorFlow2.)\n```python\n# for graph module:\npip install https://github.com/amaiya/stellargraph/archive/refs/heads/no_tf_dep_082.zip\n# for text.TextPredictor.explain and vision.ImagePredictor.explain:\npip install https://github.com/amaiya/eli5-tf/archive/refs/heads/master.zip\n# for tabular.TabularPredictor.explain:\npip install shap\n# for text.zsl (ZeroShotClassifier), text.summarization, text.translation, text.speech:\npip install torch\n# for text.speech:\npip install librosa\n# for tabular.causal_inference_model:\npip install causalnlp\n# for text.summarization.core.LexRankSummarizer:\npip install sumy\n# for text.kw.KeywordExtractor\npip install textblob\n# for text.generative_ai\npip install onprem\n```\n- **ktrain** purposely pins to a lower version of **transformers** to include support for older versions of TensorFlow.  If you need a newer version of `transformers`, it is usually safe for you to upgrade `transformers`, as long as you do it **after** installing **ktrain**.\n\n- As of v0.30.x, TensorFlow installation is optional and only required if training neural networks.  Although **ktrain** uses TensorFlow for neural network training, it also includes a variety of useful pretrained PyTorch models and sklearn models, which\ncan be used out-of-the-box **without** having TensorFlow installed, as summarized in this table:\n\n\n| Feature  | TensorFlow |  PyTorch | Sklearn\n| --- | :-: | :-: | :-: |\n| [training](https://towardsdatascience.com/ktrain-a-lightweight-wrapper-for-keras-to-help-train-neural-networks-82851ba889c) any neural network (e.g., text or image classification)  |  ✅  | ❌  | ❌  |\n| [End-to-End Question-Answering](https://nbviewer.org/github/amaiya/ktrain/blob/master/examples/text/question_answering_with_bert.ipynb) (pretrained)             |  ✅  | ✅  | ❌  |\n| [QA-Based Information Extraction](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/qa_information_extraction.ipynb) (pretrained)      |  ✅  | ✅  | ❌  |\n| [Zero-Shot Classification](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/zero_shot_learning_with_nli.ipynb) (pretrained)   |  ❌  | ✅  | ❌  |\n| [Language Translation](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/language_translation_example.ipynb) (pretrained)      |  ❌  | ✅  | ❌  |\n| [Summarization](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/examples/text/text_summarization_with_bart.ipynb) (pretrained)             |  ❌  | ✅  | ❌  |\n| [Speech Transcription](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/develop/examples/text/speech_transcription_example.ipynb) (pretrained)     |  ❌  | ✅  |❌   |\n| [Image Captioning](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/develop/examples/vision/image_captioning_example.ipynb) (pretrained)     |  ❌  | ✅  |❌   |\n| [Object Detection](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/develop/examples/vision/object_detection_example.ipynb) (pretrained)     |  ❌  | ✅  |❌   |\n| [Sentiment Analysis](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/develop/examples/text/sentiment_analysis_example.ipynb) (pretrained)     |  ❌  | ✅  |❌   |\n| [GenerativeAI](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/develop/examples/text/generative_ai_example.ipynb) (sentence-transformers)     |  ❌  | ✅  |❌   |\n| [Topic Modeling](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/master/tutorials/tutorial-05-learning_from_unlabeled_text_data.ipynb) (sklearn)  |  ❌  | ❌  | ✅  |\n| [Keyphrase Extraction](https://nbviewer.jupyter.org/github/amaiya/ktrain/blob/develop/examples/text/keyword_extraction_example.ipynb) (textblob/nltk/sklearn)   |  ❌  | ❌  | ✅  |\n\nAs noted above, end-to-end question-answering and information extraction in **ktrain** can be used with either TensorFlow (using `framework='tf'`) or PyTorch (using `framework='pt'`).\n\n\n\n\u003c!--\npip install pdoc3==0.9.2\npdoc3 --html -o docs ktrain\ndiff -qr docs/ktrain/ /path/to/repo/ktrain/docs\n--\u003e\n\n### How to Cite\n\nPlease cite the [following paper](https://arxiv.org/abs/2004.10703) when using **ktrain**:\n```\n@article{maiya2020ktrain,\n    title={ktrain: A Low-Code Library for Augmented Machine Learning},\n    author={Arun S. Maiya},\n    year={2020},\n    eprint={2004.10703},\n    archivePrefix={arXiv},\n    primaryClass={cs.LG},\n    journal={arXiv preprint arXiv:2004.10703},\n}\n\n```\n\n\n\u003c!--\n### Requirements\n\nThe following software/libraries should be installed:\n\n- [Python 3.6+](https://www.python.org/) (tested on 3.6.7)\n- [Keras](https://keras.io/)  (tested on 2.2.4)\n- [TensorFlow](https://www.tensorflow.org/)  (tested on 1.10.1)\n- [scikit-learn](https://scikit-learn.org/stable/) (tested on 0.20.0)\n- [matplotlib](https://matplotlib.org/) (tested on 3.0.0)\n- [pandas](https://pandas.pydata.org/) (tested on 0.24.2)\n- [keras_bert](https://github.com/CyberZHG/keras-bert/tree/master/keras_bert)\n- [fastprogress](https://github.com/fastai/fastprogress)\n--\u003e\n\n\n\n----\n**Creator:  [Arun S. Maiya](http://arun.maiya.net)**\n\n**Email:** arun [at] maiya [dot] net\n\n","funding_links":[],"categories":["Jupyter Notebook","Deep Learning Framework","其他_机器学习与深度学习","机器学习框架"],"sub_categories":["High-Level DL APIs"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famaiya%2Fktrain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famaiya%2Fktrain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famaiya%2Fktrain/lists"}