{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Introduction\n#### In this code, I introduce how to train sentence classifier to solve problem that which sentence include dataset name?\n#### For implementation, I use [Flair](https://github.com/flairNLP/flair) that is easy NLP tool for using [Task-aware representation of sentences (TARS)](https://kishaloyhalder.github.io/pdfs/tars_coling2020.pdf).\n#### Using TARS, we can train a high-performance classifier with very few sentences (Few-Shot).","metadata":{}},{"cell_type":"code","source":"!pip install --upgrade git+https://github.com/zalandoresearch/flair.git","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from flair.data import Corpus\nfrom flair.datasets import SentenceDataset\nfrom flair.trainers import ModelTrainer\nfrom flair.models.text_classification_model import TARSClassifier\nfrom flair.data import Sentence","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Make small corpus for sentence classification.\n#### In this corpus, \"include\" means dataset name include in sentence.\n#### The sentence in the corpus picked up from training json files.","metadata":{}},{"cell_type":"code","source":"train = SentenceDataset(\n    [\n        Sentence('In this study we employ base years and follow-up data of national probability samples of high school students in the US,\\\n                    namely the National Longitudinal Study (NLS) of the High School Class of 1972 (base year, fourth and fifth follow-up) and\\\n                    the National Education Longitudinal Study (NELS) of the Eighth Grade Class of 1988 (second and fourth follow-up) . ').add_label('include_or_not', 'include'),\n        \n        Sentence('These symptoms are also of interest as potential manifestations of underlying disease at the earliest stages of AD prior\\\n                    to a diagnosis Data used in the preparation of this article were obtained from the Alzheimers Disease Neuroimaging Initiative\\\n                    (ADNI) database (www.loni.ucla.edu ADNI) . ').add_label('include_or_not', 'include'),\n        \n        Sentence('The case for common standards was crystallized in a report published by the National Governors Association (NGA), the Council of\\\n                    Chief State School Officers (CCSSO), and Achieve (NGA et al. 2008) . Authored by an International Benchmarking Advisory Group,\\\n                    chaired by then-Governor Janet Na-politano (AZ), then-Governor Sonny Perdue (GA), and Craig R. Barrett, the chairman of the Intel\\\n                    Corporation board, the report drew heavily on research using data from the Programme for International Student Assessment (PISA)\\\n                    and Trends in International Mathematics and Science Study (TIMSS) . ').add_label('include_or_not', 'include'),\n        \n        Sentence('During this stage of the process, then, one particular set of inferences, among the differing ones that could be drawn from research\\\n                    and indicator data, were selected and framed in such a way as to persuade key policy audiences that common standards held the potential\\\n                    to rectify pressing educational and economic problems . ').add_label('include_or_not', 'not'),\n        \n        Sentence('He concluded that the TC-related heating is approximately 1.4 AE 0.7 PW (1 PW = 10 15 W), which may account for a substantial portion of\\\n                    the OHT carried by the meridional overturning circulation . ').add_label('include_or_not', 'not'),\n        \n        Sentence('Seeing these conditions required special attention so as not to cause a worse impact in the following year . ').add_label('include_or_not', 'not')\n    ])\n\ntest = SentenceDataset(\n    [\n        Sentence('Then, we used MCI and AD cases from the Alzheimers disease Neuroimaging Initiative (ADNI) to evaluate the performance in estimating\\\n                    both the functional deficits, such cognitive scores, and diagnostic categories (NC, MCI, or AD) of these patients. ').add_label('include_or_not', 'include'),\n        \n        Sentence('Soft-bottom habitats are one of the most widespread habitats on Earth and one with many keystone species and ecosystem engineers that\\\n                    play critical roles in biogeochemical cycles, energy transfer to important commercial fisheries, and provision of other essential\\\n                    ecosystem services (Snelgrove 1999) .').add_label('include_or_not', 'not')\n    ])\n\n# make a corpus with train and test split\ncorpus = Corpus(train=train, test=test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Few-Shot Training using TARS","metadata":{}},{"cell_type":"code","source":"tars = TARSClassifier.load('tars-base')\n\ntars.add_and_switch_to_new_task(\"INCLUDE_NOT\", label_dictionary=corpus.make_label_dictionary())\n\ntrainer = ModelTrainer(tars, corpus)\n\ntrainer.train(base_path='resources/taggers/include_not',\n              learning_rate=0.02,\n              mini_batch_size=1,\n              max_epochs=10,\n              train_with_dev=True,\n              )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test the final model","metadata":{}},{"cell_type":"code","source":"tars = TARSClassifier.load('resources/taggers/include_not/final-model.pt')\n\n# Prepare a test sentence that includes dataset name\nsentence = Sentence('The data for this study is taken from the National Education Longitudinal Study (NELS) .')\n\ntars.predict(sentence)\nprint(sentence)\n\n# Prepare a test sentence that NOT includes dataset name\nsentence = Sentence('Other family background indicators include socioeconomic status, and whether another language besides English is spoken in the home .')\n\ntars.predict(sentence)\nprint(sentence)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}