Skip to content

Commit 950bd35

Browse files
Text classification notebook refactor
Minor comments in image classification
1 parent 0d15174 commit 950bd35

File tree

2 files changed

+74
-45
lines changed

2 files changed

+74
-45
lines changed

notebooks/image_classification.ipynb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,9 +622,7 @@
622622
"cell_type": "markdown",
623623
"metadata": {},
624624
"source": [
625-
"Now, we are ready to upload production data.\n",
626-
"Production data can be uploaded asynchronously, that means that we can upload each data category whenever, it is available without waiting for the others.\n",
627-
"This is specially true for *target* data that usually are available with an amount of delay."
625+
"Now, we are ready to upload production data."
628626
]
629627
},
630628
{
@@ -643,6 +641,15 @@
643641
"client.wait_job_completion(job_id=job_id)\n",
644642
"print(f'Job {job_id} completed')"
645643
]
644+
},
645+
{
646+
"cell_type": "markdown",
647+
"metadata": {},
648+
"source": [
649+
"\n",
650+
"Notice that production data can be uploaded asynchronously, which means that we can upload each data category whenever it is available, without waiting for the others.\n",
651+
"This is specially true for *target* data, that usually are available with an amount of delay.\n"
652+
]
646653
}
647654
],
648655
"metadata": {

notebooks/text_classification.ipynb

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
"source": [
77
"# Text classification\n",
88
"\n",
9-
"This notebook shows how to use ML cube Platform with text data.\n",
10-
"We use a Huggingface dataset and trained model for Sentiment classification.\n",
11-
"The dataset contains train, validation and test sets, we use train as reference dataset while validation and test as production data.\n",
12-
"Of course, in a real scenario all those dataset will be part of historical/reference data and production will come from the production environment after the deployment of the algorithm.\n",
9+
"This notebook shows how to use the ML cube Platform with text data.\n",
10+
"We utilize a Huggingface dataset and a pre-trained model for Sentiment classification. We load the validation data and split the dataset in two parts, using the first as reference data and the second as production data. \n",
1311
"\n",
12+
"In a real-world scenario, the training and validation datasets would be considered historical/reference data, while production data would come from the production environment after the model's deployment."
13+
]
14+
},
15+
{
16+
"cell_type": "markdown",
17+
"metadata": {},
18+
"source": [
1419
"**With this example you will learn:**\n",
1520
"- how to create a text classification task\n",
1621
"- how to define a data schema\n",
@@ -21,9 +26,9 @@
2126
"\n",
2227
"**Requirements**\n",
2328
"\n",
24-
"In order to properly run this notebook the Python environment has those requirements.\n",
29+
"These are the dependencies your Python environment is required to have in order to properly run this notebook.\n",
2530
"```\n",
26-
"ml3-platform-sdk>=0.0.15\n",
31+
"ml3-platform-sdk>=0.0.22\n",
2732
"transformers[torch]==4.41.2\n",
2833
"torch==2.2.0\n",
2934
"datasets==2.15.0\n",
@@ -44,7 +49,16 @@
4449
},
4550
{
4651
"cell_type": "code",
47-
"execution_count": null,
52+
"execution_count": 1,
53+
"metadata": {},
54+
"outputs": [],
55+
"source": [
56+
"from tqdm import tqdm"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 2,
4862
"metadata": {},
4963
"outputs": [],
5064
"source": [
@@ -72,7 +86,7 @@
7286
},
7387
{
7488
"cell_type": "code",
75-
"execution_count": null,
89+
"execution_count": 3,
7690
"metadata": {},
7791
"outputs": [],
7892
"source": [
@@ -101,27 +115,37 @@
101115
},
102116
{
103117
"cell_type": "code",
104-
"execution_count": null,
118+
"execution_count": 4,
105119
"metadata": {},
106120
"outputs": [],
107121
"source": [
108-
"dataset = load_dataset('cardiffnlp/tweet_eval', name='sentiment', )"
122+
"complete_dataset = load_dataset('cardiffnlp/tweet_eval', name='sentiment', split='validation[:50%]')"
109123
]
110124
},
111125
{
112126
"cell_type": "code",
113-
"execution_count": null,
127+
"execution_count": 5,
114128
"metadata": {},
115129
"outputs": [],
116130
"source": [
117-
"def sample_dataset(dataset, fraction=0.05, seed=42):\n",
131+
"def sample_dataset(dataset, reference_portion=0.5, first_production_portion=0.5, seed=42):\n",
118132
" sampled_dataset = DatasetDict()\n",
119-
" for split in dataset.keys():\n",
120-
" sampled_dataset[split] = dataset[split].train_test_split(test_size=fraction, seed=seed)['test']\n",
133+
" \n",
134+
" # Split the dataset into reference and production\n",
135+
"\n",
136+
" split = dataset.train_test_split(test_size=reference_portion, seed=seed)\n",
137+
"\n",
138+
" sampled_dataset['reference'] = split['train']\n",
139+
"\n",
140+
" split_2 = split['test'].train_test_split(test_size=first_production_portion, seed=seed)\n",
141+
"\n",
142+
" sampled_dataset['first_production'] = split_2['train']\n",
143+
" sampled_dataset['second_production'] = split_2['test']\n",
144+
"\n",
121145
" return sampled_dataset\n",
122146
"\n",
123147
"# Perform the sampling\n",
124-
"dataset = sample_dataset(dataset)"
148+
"dataset = sample_dataset(complete_dataset)"
125149
]
126150
},
127151
{
@@ -130,7 +154,7 @@
130154
"metadata": {},
131155
"outputs": [],
132156
"source": [
133-
"len(dataset['train']['text']), len(dataset['validation']['text']), len(dataset['test']['text'])"
157+
"len(dataset['reference']['text']), len(dataset['first_production']['text']), len(dataset['second_production']['text'])"
134158
]
135159
},
136160
{
@@ -160,7 +184,7 @@
160184
},
161185
{
162186
"cell_type": "code",
163-
"execution_count": null,
187+
"execution_count": 9,
164188
"metadata": {},
165189
"outputs": [],
166190
"source": [
@@ -175,7 +199,7 @@
175199
"We use local data sources to upload data, hence, we need to create local files that will be shared with ML cube Platform.\n",
176200
"With text data we can upload data in json as a list of objects containing three fields: timestamp, sample-id, text.\n",
177201
"Text data can be composed of only text sequences but also with their embeddings (optional).\n",
178-
"While target and predictions can be sent as csv tabular files.\n",
202+
"On the other hand, target and predictions can be sent as csv tabular files.\n",
179203
"\n",
180204
"In ML cube Platform data are uploaded separately for each category:\n",
181205
"- **inputs:** TextData object in json format\n",
@@ -185,13 +209,13 @@
185209
"\n",
186210
"When dealing with unstructured data like text it is possible to send them in three ways:\n",
187211
"1. By sending only embeddings i.e., a numerical representation of the text sample as a vector, using `EmbeddingData`;\n",
188-
"2. By sending only unstructured text, using `TextData`. In this case ML cube Platform will create the numerical representation using internal encoders;\n",
189-
"3. By sending ustructured text along with embeddings using `TextData` with `embedding_source` attribute. This more complete option has two benefits, the first is the usage of personal embedder that usually is focused on the domain instead of a general one; the other is using text to extract additional metrics and to have full capability in the web application."
212+
"2. By sending only the raw text, using `TextData`. In this case ML cube Platform will create the numerical representation using internal encoders;\n",
213+
"3. By sending the raw text along with the embeddings, using `TextData` with the `embedding_source` attribute. This more complete option has two benefits:it allows the usage of a personal embedder, which is usually focused on the domain rather than a general one, and it enables the extraction of additional metrics from the text, providing more functionalities in the web application."
190214
]
191215
},
192216
{
193217
"cell_type": "code",
194-
"execution_count": null,
218+
"execution_count": 10,
195219
"metadata": {},
196220
"outputs": [],
197221
"source": [
@@ -342,14 +366,14 @@
342366
" starting_id,\n",
343367
" starting_timestamp,\n",
344368
") = build_data_objects(\n",
345-
" dataset['train'],\n",
369+
" dataset['reference'],\n",
346370
" sentiment_pip,\n",
347371
" embedder,\n",
348372
" model_name,\n",
349373
" model_version,\n",
350374
" training_initial_sample_id,\n",
351375
" training_initial_timestamp,\n",
352-
" 'train'\n",
376+
" 'reference'\n",
353377
")\n",
354378
"training_end_timestamp = starting_timestamp - 120"
355379
]
@@ -367,7 +391,7 @@
367391
" starting_id,\n",
368392
" starting_timestamp,\n",
369393
") = build_data_objects(\n",
370-
" dataset['validation'],\n",
394+
" dataset['first_production'],\n",
371395
" sentiment_pip,\n",
372396
" embedder,\n",
373397
" model_name,\n",
@@ -391,7 +415,7 @@
391415
" starting_id,\n",
392416
" starting_timestamp,\n",
393417
") = build_data_objects(\n",
394-
" dataset['test'],\n",
418+
" dataset['second_production'],\n",
395419
" sentiment_pip,\n",
396420
" embedder,\n",
397421
" model_name,\n",
@@ -410,18 +434,18 @@
410434
"\n",
411435
"The data schema specifies the type of data present in the task with their specific names.\n",
412436
"A data schema must contain:\n",
413-
"- *sample id* column that is used to uniquely identify each sample\n",
414-
"- *timestamp* column that is used to order samples\n",
415-
"- *input* column that specify the nature of the input. In this case TEXT\n",
416-
"- *input additional embedding* optional column for additional embedding of the text data\n",
417-
"- *target* column that specify the nature of the target. In this case categorical with three values\n",
437+
"- *sample id*, column that is used to uniquely identify each sample\n",
438+
"- *timestamp*, column that is used to order samples\n",
439+
"- *input*, column that specifies the nature of the input. In this case, it's a string, as we are dealing with text data.\n",
440+
"- *input additional embedding*, optional column for the embedding of the text data\n",
441+
"- *target*, column that specifies the nature of the target. In this case, categorical with three possible values\n",
418442
"\n",
419-
"Prediction column must not be specified because it will be automatically added during the model creation with the name like MODEL_NAME@MODEL_VERSION"
443+
"The prediction column must not be specified because it will be automatically added during the model creation, with a name like `MODEL_NAME@MODEL_VERSION`"
420444
]
421445
},
422446
{
423447
"cell_type": "code",
424-
"execution_count": null,
448+
"execution_count": 14,
425449
"metadata": {},
426450
"outputs": [],
427451
"source": [
@@ -491,13 +515,13 @@
491515
"- **optional_target:** if the target can be missing for production data.\n",
492516
" We assume that reference data being the training one always have the target.\n",
493517
" However, it is possible that ofr other historical data or for production data target is not available, enabling this option, ML cube Platform does not force its presence and it will not stop breaks the jobs.\n",
494-
"- **text_language:** in case of Text data structure it is mandatory to specify the language used in the task.\n",
495-
"- **cost_info**, this optional field allows to specify the costs of the error of the model and will be used during the retraining report computation."
518+
"- **text_language:** if the target can be missing for production data. We assume that reference data, being the training data, always have the target.\n",
519+
" However, it is possible that target is not available in other historical data or production data. By enabling the optional target option, the ML cube Platform will not check its presence in the data sent to the platform."
496520
]
497521
},
498522
{
499523
"cell_type": "code",
500-
"execution_count": null,
524+
"execution_count": 16,
501525
"metadata": {},
502526
"outputs": [],
503527
"source": [
@@ -514,7 +538,7 @@
514538
},
515539
{
516540
"cell_type": "code",
517-
"execution_count": null,
541+
"execution_count": 17,
518542
"metadata": {},
519543
"outputs": [],
520544
"source": [
@@ -525,14 +549,12 @@
525549
"cell_type": "markdown",
526550
"metadata": {},
527551
"source": [
528-
"After we added the data schema, we are able to create our model.\n",
529-
"\n",
530552
"A model is uniquely identified by `name` and `model version`."
531553
]
532554
},
533555
{
534556
"cell_type": "code",
535-
"execution_count": null,
557+
"execution_count": 18,
536558
"metadata": {},
537559
"outputs": [],
538560
"source": [
@@ -553,7 +575,7 @@
553575
"Training data are uploaded as historical data i.e., any data that do not come from production.\n",
554576
"Then we indicate them as reference data of our model in order to set up the detection algorithms.\n",
555577
"\n",
556-
"Note that it is possible to add other historical data in any time."
578+
"Note that it is possible to add other historical data at any time."
557579
]
558580
},
559581
{
@@ -593,8 +615,8 @@
593615
"metadata": {},
594616
"source": [
595617
"Now, we are ready to upload production data.\n",
596-
"Production data can be uploaded asynchronously, that means that we can upload each data category whenever, it is available without waiting for the others.\n",
597-
"This is specially true for *target* data that usually are available with an amount of delay."
618+
"Notice that production data can be uploaded asynchronously, which means that we can upload each data category whenever it is available, without waiting for the others.\n",
619+
"This is specially true for *target* data, that usually are available with an amount of delay."
598620
]
599621
},
600622
{

0 commit comments

Comments
 (0)