You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Update README.md
* Update README.md
* Update README.md
* Update lu_solve_omp_offload_optimized.F90
* Update openmp_sample.f90 (#2371)
* fixed HF load_dataset
* Update README.md to reflect changes in compiler output. (#2402)
This update contains a rewritten README.md file for the
`guided_matrix_mult_InvalidContexts` sample. It also adds two screenshots
(png files) that accompany the rewritten README.me file. The previous
version of this README.md did not include screenshots
---------
Co-authored-by: Jimmy Wei <[email protected]>
Co-authored-by: Paul Fischer <[email protected]>
Copy file name to clipboardExpand all lines: AI-and-Analytics/Features-and-Functionality/INC_QuantizationAwareTraining_TextClassification/INC_QuantizationAwareTraining_TextClassification.ipynb
Copy file name to clipboardExpand all lines: AI-and-Analytics/Features-and-Functionality/INC_QuantizationAwareTraining_TextClassification/INC_QuantizationAwareTraining_TextClassification.py
# # Fine-tuning text classification model with Intel® Neural Compressor (INC) Quantization Aware Training
15
-
#
15
+
#
16
16
# This code sample will show you how to fine tune BERT text model for text multi-class classification task using Quantization Aware Training provided as part of Intel® Neural Compressor (INC).
17
-
#
17
+
#
18
18
# Before we start, please make sure you have installed all necessary libraries to run this code sample.
19
19
20
-
# ## Loading model
21
-
#
22
-
# We decided to use really small model for this code sample which is `prajjwal1/bert-tiny` but please feel free to use different model changing `model_id` to other name form Hugging Face library or your local model path (if it is compatible with Hugging Face API).
23
-
#
20
+
# ## Loading model
21
+
#
22
+
# We decided to use really small model for this code sample which is `prajjwal1/bert-tiny` but please feel free to use different model changing `model_id` to other name form Hugging Face library or your local model path (if it is compatible with Hugging Face API).
23
+
#
24
24
# Keep in mind that using bigger models like `bert-base-uncased` can improve the final result of the classification after fine-tuning process but it is also really resources and time consuming.
# The directory where the quantized model will be saved
36
36
save_dir="quantized_model"
37
37
38
38
39
39
# ## Dataset
40
-
#
41
-
# We are using `emotion` [dataset form Hugging Face](https://huggingface.co/datasets/dair-ai/emotion). This dataset has 2 different configurations - **split** and **unsplit**.
42
-
#
40
+
#
41
+
# We are using `emotion` [dataset form Hugging Face](https://huggingface.co/datasets/dair-ai/emotion). This dataset has 2 different configurations - **split** and **unsplit**.
42
+
#
43
43
# In this code sample we are using split configuration. It contains in total 20 000 examples split into train (16 000 texts), test (2 000 texts) and validation (2 000 text) datasets. We decided to use split dataset instead of unsplit configuration as it contains over 400 000 texts which is overkill for fine-tuning.
44
-
#
44
+
#
45
45
# After loading selected dataset we will take a look at first 10 rows of train dataset. You can always change the dataset for different one, just remember to change also number of labels parameter provided when loading the model.
# Dataset contains 6 different labels represented by digits from 0 to 5. Every digit symbolizes different emotion as followed:
57
-
#
57
+
#
58
58
# * 0 - sadness
59
59
# * 1 - joy
60
60
# * 2 - love
61
61
# * 3 - anger
62
62
# * 4 - fear
63
63
# * 5 - surprise
64
-
#
64
+
#
65
65
# In the cell below we conducted few computations on training dataset to better understand how the data looks like. We are analyzing only train dataset as the test and validation datasets have similar data distribution.
66
-
#
67
-
# As you can see, distribution opf classed in dataset is not equal. Having in mind that the train, test and validation distributions are similar this is not a problem for our case.
66
+
#
67
+
# As you can see, distribution opf classed in dataset is not equal. Having in mind that the train, test and validation distributions are similar this is not a problem for our case.
# **Tokenization** is a way of separating a piece of text into smaller units called tokens. Here, tokens can be either words, characters etc. It means that tokenizer breaks unstructured data (natural language text) into chunks of information that can be considered as discrete elements. The tokens can be used later in a vector representation of that document.
97
-
#
98
-
# In other words tokenization change an text document into a numerical data structure suitable for machine and deep learning.
99
-
#
93
+
#
94
+
# Next step is to tokenize the dataset.
95
+
#
96
+
# **Tokenization** is a way of separating a piece of text into smaller units called tokens. Here, tokens can be either words, characters etc. It means that tokenizer breaks unstructured data (natural language text) into chunks of information that can be considered as discrete elements. The tokens can be used later in a vector representation of that document.
97
+
#
98
+
# In other words tokenization change an text document into a numerical data structure suitable for machine and deep learning.
99
+
#
100
100
# To do that, we created function that takes every text from dataset and tokenize it with maximum token length being 128. After that we can se how the structure of the dataset change.
# Before we start fine-tuning, let's see how the model in current state performs against validation dataset.
114
-
#
115
+
#
115
116
# First, we need to prepare metrics showing model performance. We decided to use accuracy as a performance measure in this specific task. As the model was not created for this specific task, we can assume that the accuracy will not be high.
# * dataset - in our case this is validation dataset,
136
138
# * metrics - as specified before, in our case accuracy,
137
139
# * label mapping - to map label names with corresponding digits.
138
-
#
140
+
#
139
141
# After the evaluation, we just show the results, which are as expected not the best. At this point model is not prepared for emotion classification task.
# Now, we can move to fine-tuning with quantization. But first, please review the definition of quantization and quantization aware training.
161
-
#
170
+
#
162
171
# **Quantization** is a systematic reduction of the precision of all or several layers within the model. This means, a higher-precision type, such as the single precision floating-point (FP32) is converted into a lower-precision type, such as FP16 (16 bits) or INT8 (8 bits).
163
-
#
172
+
#
164
173
# **Quantization Aware Training** replicates inference-time quantization, resulting in a model that downstream tools may utilize to generate actually quantized models. In other words, it provides quantization to the model during training (or fine-tuning like in our case) based on provided quantization configuration.
165
-
#
174
+
#
166
175
# Having that in mind, we can provide configuration for the Quantization Aware Training form Intel® Neural Compressor.
# The next step is to create trainer for our model. We will use Intel® Neural Compressor optimize trainer form `optimum.intel` package.
179
188
# We need to provide all necessary parameters to the trainer:
180
-
#
189
+
#
181
190
# * initialized model and tokenizer
182
191
# * configuration for quantization aware training
183
192
# * training arguments that includes: directory where model will be saved, number of epochs
184
193
# * datasets for training and evaluation
185
194
# * prepared metrics that allow us to see the progress in training
186
-
#
195
+
#
187
196
# For purpose of this code sample, we decided to train model by just 2 epochs, to show you how the quantization aware training works and that the fine-tuning really improves the results of the classification. If you wan to receive better accuracy results, yoy can easily incise the number of epochs up to 5 and observe how model learns. Keep in mind that the process may take some time - the more epochs you will use, the training time will be longer.
# After the training we should evaluate our model using `evaluate()` method on prepared trainer. It will show results for prepared before evaluation metrics - evaluation accuracy and loss. Additionally we will have information about evaluation time, samples and steps per second and number of epochs model was trained by.
234
+
#
235
+
# After the training we should evaluate our model using `evaluate()` method on prepared trainer. It will show results for prepared before evaluation metrics - evaluation accuracy and loss. Additionally we will have information about evaluation time, samples and steps per second and number of epochs model was trained by.
# After the training it is important to save the model. One again we will use prepared trainer and other method - `save_model()`. Our model will be saved in the location provided before.
235
-
# After that, to use this model in the future you just need load it similarly as at the beginning, using dedicated Intel® Neural Compressor optimized method `INCModelForSequenceClassification.from_pretrained(...)`.
246
+
# After that, to use this model in the future you just need load it similarly as at the beginning, using dedicated Intel® Neural Compressor optimized method `INCModelForSequenceClassification.from_pretrained(...)`.
# In this code sample we use BERT-tiny and emotion dataset to create text classification model using Intel® Neural Compressor Quantization Aware Training. We encourage you to experiment with this code sample changing model and datasets to make text models for different classification tasks.
257
+
# In this code sample we use BERT-tiny and emotion dataset to create text classification model using Intel® Neural Compressor Quantization Aware Training. We encourage you to experiment with this code sample changing model and datasets to make text models for different classification tasks.
0 commit comments