• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Text classification
2
3Use a TensorFlow Lite model to category a paragraph into predefined groups.
4
5Note: (1) To integrate an existing model, try
6[TensorFlow Lite Task Library](https://www.tensorflow.org/lite/inference_with_metadata/task_library/nl_classifier).
7(2) To customize a model, try
8[TensorFlow Lite Model Maker](https://www.tensorflow.org/lite/models/modify/model_maker/text_classification).
9
10## Get started
11
12<img src="images/screenshot.gif" class="attempt-right" style="max-width: 300px">
13
14If you are new to TensorFlow Lite and are working with Android, we recommend
15exploring the guide of
16[TensorFLow Lite Task Library](../../inference_with_metadata/task_library/nl_classifier)
17to integrate text classification models within just a few lines of code. You can
18also integrate the model using the
19[TensorFlow Lite Interpreter Java API](../../guide/inference#load_and_run_a_model_in_java).
20
21The Android example below demonstrates the implementation for both methods as
22[lib_task_api](https://github.com/tensorflow/examples/tree/master/lite/examples/text_classification/android/lib_task_api)
23and
24[lib_interpreter](https://github.com/tensorflow/examples/tree/master/lite/examples/text_classification/android/lib_interpreter),
25respectively.
26
27<a class="button button-primary" href="https://github.com/tensorflow/examples/tree/master/lite/examples/text_classification/android">Android
28example</a>
29
30If you are using a platform other than Android, or you are already familiar with
31the TensorFlow Lite APIs, you can download our starter text classification
32model.
33
34<a class="button button-primary" href="https://storage.googleapis.com/download.tensorflow.org/models/tflite/text_classification/text_classification_v2.tflite">Download
35starter model</a>
36
37## How it works
38
39Text classification categorizes a paragraph into predefined groups based on its
40content.
41
42This pretrained model predicts if a paragraph's sentiment is positive or
43negative. It was trained on
44[Large Movie Review Dataset v1.0](http://ai.stanford.edu/~amaas/data/sentiment/)
45from Mass et al, which consists of IMDB movie reviews labeled as either positive
46or negative.
47
48Here are the steps to classify a paragraph with the model:
49
501.  Tokenize the paragraph and convert it to a list of word ids using a
51    predefined vocabulary.
521.  Feed the list to the TensorFlow Lite model.
531.  Get the probability of the paragraph being positive or negative from the
54    model outputs.
55
56### Note
57
58*   Only English is supported.
59*   This model was trained on movie reviews dataset so you may experience
60    reduced accuracy when classifying text of other domains.
61
62## Performance benchmarks
63
64Performance benchmark numbers are generated with the tool
65[described here](https://www.tensorflow.org/lite/performance/benchmarks).
66
67<table>
68  <thead>
69    <tr>
70      <th>Model Name</th>
71      <th>Model size </th>
72      <th>Device </th>
73      <th>CPU</th>
74    </tr>
75  </thead>
76  <tr>
77    <td rowspan = 3>
78      <a href="https://storage.googleapis.com/download.tensorflow.org/models/tflite/text_classification/text_classification_v2.tflite">Text Classification</a>
79    </td>
80    <td rowspan = 3>
81      0.6 Mb
82    </td>
83    <td>Pixel 3 (Android 10) </td>
84    <td>0.05ms*</td>
85  </tr>
86   <tr>
87     <td>Pixel 4 (Android 10) </td>
88    <td>0.05ms*</td>
89  </tr>
90   <tr>
91     <td>iPhone XS (iOS 12.4.1) </td>
92    <td>0.025ms** </td>
93  </tr>
94</table>
95
96\* 4 threads used.
97
98\*\* 2 threads used on iPhone for the best performance result.
99
100## Example output
101
102| Text                                       | Negative (0) | Positive (1) |
103| ------------------------------------------ | ------------ | ------------ |
104| This is the best movie I’ve seen in recent | 25.3%        | 74.7%        |
105: years. Strongly recommend it!              :              :              :
106| What a waste of my time.                   | 72.5%        | 27.5%        |
107
108## Use your training dataset
109
110Follow this
111[tutorial](https://www.tensorflow.org/lite/models/modify/model_maker/text_classification)
112to apply the same technique used here to train a text classification model using
113your own datasets. With the right dataset, you can create a model for use cases
114such as document categorization or toxic comments detection.
115
116## Read more about text classification
117
118*   [Word embeddings and tutorial to train this model](https://www.tensorflow.org/tutorials/text/word_embeddings)
119