1
0

complete lab3

This commit is contained in:
Rokas Puzonas 2024-05-28 20:04:43 +03:00
parent 65d6d65b0f
commit c201f46825
10 changed files with 525881 additions and 0 deletions

File diff suppressed because it is too large Load Diff

32574
Lab3/4.1/assets/test.xls Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,78 @@
center_id,city_code,region_code,center_type,op_area
11,679,56,TYPE_A,3.7
13,590,56,TYPE_B,6.7
124,590,56,TYPE_C,4
66,648,34,TYPE_A,4.1
94,632,34,TYPE_C,3.6
64,553,77,TYPE_A,4.4
129,593,77,TYPE_A,3.9
139,693,34,TYPE_C,2.8
88,526,34,TYPE_A,4.1
143,562,77,TYPE_B,3.8
101,699,85,TYPE_C,2.8
86,699,85,TYPE_C,4
32,526,34,TYPE_A,3.8
149,478,77,TYPE_A,2.4
152,576,34,TYPE_B,4
92,526,34,TYPE_C,2.9
27,713,85,TYPE_A,4.5
14,654,56,TYPE_C,2.7
26,515,77,TYPE_C,3
104,647,56,TYPE_A,4.5
77,676,34,TYPE_A,3.8
23,698,23,TYPE_A,3.4
97,628,77,TYPE_A,4.6
146,526,34,TYPE_B,5
113,680,77,TYPE_C,4
145,620,77,TYPE_A,3.9
80,604,56,TYPE_C,5.1
55,647,56,TYPE_C,2
186,649,34,TYPE_A,3.4
99,596,71,TYPE_A,4.5
91,590,56,TYPE_C,0.9
20,522,56,TYPE_A,4
106,675,34,TYPE_A,4
81,526,34,TYPE_A,4
73,576,34,TYPE_A,4
29,526,34,TYPE_C,4
43,590,56,TYPE_A,5.1
102,593,77,TYPE_A,2.8
61,473,77,TYPE_A,4.5
50,556,77,TYPE_A,4.8
83,659,77,TYPE_A,5.3
57,541,77,TYPE_C,2.8
126,577,56,TYPE_A,2.7
177,683,56,TYPE_A,3.4
67,638,56,TYPE_B,7
174,700,56,TYPE_A,7
59,456,56,TYPE_A,4.2
58,695,77,TYPE_C,3.8
65,602,34,TYPE_A,4.8
39,526,34,TYPE_C,3.8
132,522,56,TYPE_A,3.9
89,703,56,TYPE_A,4.8
162,526,34,TYPE_C,2
75,651,77,TYPE_B,4.7
72,638,56,TYPE_C,3.9
41,590,56,TYPE_C,1.9
10,590,56,TYPE_B,6.3
110,485,77,TYPE_A,3.8
52,685,56,TYPE_B,5.6
93,461,34,TYPE_A,3.9
74,702,35,TYPE_A,2.8
34,615,34,TYPE_B,4.2
137,590,56,TYPE_A,4.4
153,590,56,TYPE_A,3.9
24,614,85,TYPE_B,3.6
109,599,56,TYPE_A,3.6
108,579,56,TYPE_B,4.4
36,517,56,TYPE_B,4.4
157,609,93,TYPE_A,4.1
17,517,56,TYPE_A,3.2
161,658,34,TYPE_B,3.9
42,561,77,TYPE_B,3.9
53,590,56,TYPE_A,3.8
30,604,56,TYPE_A,3.5
76,614,85,TYPE_A,3
68,676,34,TYPE_B,4.1
51,638,56,TYPE_A,7
1 center_id city_code region_code center_type op_area
2 11 679 56 TYPE_A 3.7
3 13 590 56 TYPE_B 6.7
4 124 590 56 TYPE_C 4
5 66 648 34 TYPE_A 4.1
6 94 632 34 TYPE_C 3.6
7 64 553 77 TYPE_A 4.4
8 129 593 77 TYPE_A 3.9
9 139 693 34 TYPE_C 2.8
10 88 526 34 TYPE_A 4.1
11 143 562 77 TYPE_B 3.8
12 101 699 85 TYPE_C 2.8
13 86 699 85 TYPE_C 4
14 32 526 34 TYPE_A 3.8
15 149 478 77 TYPE_A 2.4
16 152 576 34 TYPE_B 4
17 92 526 34 TYPE_C 2.9
18 27 713 85 TYPE_A 4.5
19 14 654 56 TYPE_C 2.7
20 26 515 77 TYPE_C 3
21 104 647 56 TYPE_A 4.5
22 77 676 34 TYPE_A 3.8
23 23 698 23 TYPE_A 3.4
24 97 628 77 TYPE_A 4.6
25 146 526 34 TYPE_B 5
26 113 680 77 TYPE_C 4
27 145 620 77 TYPE_A 3.9
28 80 604 56 TYPE_C 5.1
29 55 647 56 TYPE_C 2
30 186 649 34 TYPE_A 3.4
31 99 596 71 TYPE_A 4.5
32 91 590 56 TYPE_C 0.9
33 20 522 56 TYPE_A 4
34 106 675 34 TYPE_A 4
35 81 526 34 TYPE_A 4
36 73 576 34 TYPE_A 4
37 29 526 34 TYPE_C 4
38 43 590 56 TYPE_A 5.1
39 102 593 77 TYPE_A 2.8
40 61 473 77 TYPE_A 4.5
41 50 556 77 TYPE_A 4.8
42 83 659 77 TYPE_A 5.3
43 57 541 77 TYPE_C 2.8
44 126 577 56 TYPE_A 2.7
45 177 683 56 TYPE_A 3.4
46 67 638 56 TYPE_B 7
47 174 700 56 TYPE_A 7
48 59 456 56 TYPE_A 4.2
49 58 695 77 TYPE_C 3.8
50 65 602 34 TYPE_A 4.8
51 39 526 34 TYPE_C 3.8
52 132 522 56 TYPE_A 3.9
53 89 703 56 TYPE_A 4.8
54 162 526 34 TYPE_C 2
55 75 651 77 TYPE_B 4.7
56 72 638 56 TYPE_C 3.9
57 41 590 56 TYPE_C 1.9
58 10 590 56 TYPE_B 6.3
59 110 485 77 TYPE_A 3.8
60 52 685 56 TYPE_B 5.6
61 93 461 34 TYPE_A 3.9
62 74 702 35 TYPE_A 2.8
63 34 615 34 TYPE_B 4.2
64 137 590 56 TYPE_A 4.4
65 153 590 56 TYPE_A 3.9
66 24 614 85 TYPE_B 3.6
67 109 599 56 TYPE_A 3.6
68 108 579 56 TYPE_B 4.4
69 36 517 56 TYPE_B 4.4
70 157 609 93 TYPE_A 4.1
71 17 517 56 TYPE_A 3.2
72 161 658 34 TYPE_B 3.9
73 42 561 77 TYPE_B 3.9
74 53 590 56 TYPE_A 3.8
75 30 604 56 TYPE_A 3.5
76 76 614 85 TYPE_A 3
77 68 676 34 TYPE_B 4.1
78 51 638 56 TYPE_A 7

View File

@ -0,0 +1,52 @@
meal_id,category,cuisine
1885,Beverages,Thai
1993,Beverages,Thai
2539,Beverages,Thai
1248,Beverages,Indian
2631,Beverages,Indian
1311,Extras,Thai
1062,Beverages,Italian
1778,Beverages,Italian
1803,Extras,Thai
1198,Extras,Thai
2707,Beverages,Italian
1847,Soup,Thai
1438,Soup,Thai
2494,Soup,Thai
2760,Other Snacks,Thai
2490,Salad,Italian
1109,Rice Bowl,Indian
2290,Rice Bowl,Indian
1525,Other Snacks,Thai
2704,Other Snacks,Thai
1878,Starters,Thai
2640,Starters,Thai
2577,Starters,Thai
1754,Sandwich,Italian
1971,Sandwich,Italian
2306,Pasta,Italian
2139,Beverages,Indian
2826,Sandwich,Italian
2664,Salad,Italian
2569,Salad,Italian
1230,Beverages,Continental
1207,Beverages,Continental
2322,Beverages,Continental
2492,Desert,Indian
1216,Pasta,Italian
1727,Rice Bowl,Indian
1902,Biryani,Indian
1247,Biryani,Indian
2304,Desert,Indian
1543,Desert,Indian
1770,Biryani,Indian
2126,Pasta,Italian
1558,Pizza,Continental
2581,Pizza,Continental
1962,Pizza,Continental
1571,Fish,Continental
2956,Fish,Continental
2104,Fish,Continental
2444,Seafood,Continental
2867,Seafood,Continental
1445,Seafood,Continental
1 meal_id category cuisine
2 1885 Beverages Thai
3 1993 Beverages Thai
4 2539 Beverages Thai
5 1248 Beverages Indian
6 2631 Beverages Indian
7 1311 Extras Thai
8 1062 Beverages Italian
9 1778 Beverages Italian
10 1803 Extras Thai
11 1198 Extras Thai
12 2707 Beverages Italian
13 1847 Soup Thai
14 1438 Soup Thai
15 2494 Soup Thai
16 2760 Other Snacks Thai
17 2490 Salad Italian
18 1109 Rice Bowl Indian
19 2290 Rice Bowl Indian
20 1525 Other Snacks Thai
21 2704 Other Snacks Thai
22 1878 Starters Thai
23 2640 Starters Thai
24 2577 Starters Thai
25 1754 Sandwich Italian
26 1971 Sandwich Italian
27 2306 Pasta Italian
28 2139 Beverages Indian
29 2826 Sandwich Italian
30 2664 Salad Italian
31 2569 Salad Italian
32 1230 Beverages Continental
33 1207 Beverages Continental
34 2322 Beverages Continental
35 2492 Desert Indian
36 1216 Pasta Italian
37 1727 Rice Bowl Indian
38 1902 Biryani Indian
39 1247 Biryani Indian
40 2304 Desert Indian
41 1543 Desert Indian
42 1770 Biryani Indian
43 2126 Pasta Italian
44 1558 Pizza Continental
45 2581 Pizza Continental
46 1962 Pizza Continental
47 1571 Fish Continental
48 2956 Fish Continental
49 2104 Fish Continental
50 2444 Seafood Continental
51 2867 Seafood Continental
52 1445 Seafood Continental

456549
Lab3/4.1/assets/train/train.csv Normal file

File diff suppressed because it is too large Load Diff

1708
Lab3/4.1/main.ipynb Normal file

File diff suppressed because one or more lines are too long

480
Lab3/4.2/main.ipynb Normal file
View File

@ -0,0 +1,480 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lab42"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From f:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\keras\\src\\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"f:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from time import time\n",
"from sklearn.model_selection import train_test_split as split_test_train\n",
"\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense, SimpleRNN, LSTM, GRU\n",
"from keras.optimizers import RMSprop\n",
"from keras.callbacks import Callback\n",
"import tensorflow_datasets as tfds\n",
"import tensorflow as tf\n",
"\n",
"tfds.disable_progress_bar()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"text: b\"This was an absolutely terrible movie. Don't be lured in by Christopher Walken or Michael Ironside. Both are great actors, but this must simply be their worst role in history. Even their great acting could not redeem this movie's ridiculous storyline. This movie is an early nineties US propaganda piece. The most pathetic scenes were those when the Columbian rebels were making their cases for revolutions. Maria Conchita Alonso appeared phony, and her pseudo-love affair with Walken was nothing but a pathetic emotional plug in a movie that was devoid of any real meaning. I am disappointed that there are movies like this, ruining actor's like Christopher Walken's good name. I could barely sit through it.\"\n",
"label: 0\n",
"texts: [b'Closer to reality and containing more depth than \"Breakdance\", Stan Lathan\\'s \"Beat Street\" is still a pretty dull show. Again this pic is really only cashing in on the \\'breakin\\' craze but at least we get a little bit of entertainment from the plot, which concerns the lives of three young friends and a younger brother, all growing up in Queens, New York. Each has their own unique talent. One is a hustler, one a d.j., another an artist who creates \\'burners\\', while little bro\\' Lee is a hot \\'hip hop\\' dancer.<br /><br />Lathan is unable to generate any real audience interest in the story though, and his young cast are likewise struggling with their characters. Therefore it is left almost entirely to the funky music and the \\'fresh\\' dancing to save the day.<br /><br />Choreography is again sharp for both club and street scenes, but this alone is not enough to lift \"\"Beat Street\" to greater heights. Unfortunately the film really falls flat late on, after showing a glimmer of hope that it just might get interesting.<br /><br />Sunday, August 25, 1996 - Video'\n",
" b'Of the three remakes of this plot, I like them all, I have all three on VHS and in addition have a copy of this one on DVD. There is just enough variation in the scripts to make all three entertaining and re-watchable. In addition has any other film been remade three times with such all star casts in each? Of course the main stars in this one are great, but the supporting actors are also superb. I particularly like William Tracy as Pepi. He was such a scene stealer that I have searched to find other movies he is in. He appeared in many, but most are not available. As the other comments, I also say - buy this one.'\n",
" b\"This Cecil B. DeMille epic of the old West contains what may be Jean Arthur's finest performance, as a hysterical, eccentric, incurably amoral, but devotedly doting Calamity Jane. She really pulled it off! Gary Cooper is at his most taciturn, but manages some occasional pithy sayings: 'The plains are big, but trails cross ... sometimes.' The story is a pastiche to end all pastiches. All the cowboy heroes of Western lore seem to be in there somehow except for Jesse James. Even Abraham Lincoln opens the story in person (or at least, DeMille would have us believe so). There is no room for anything so evanescent as subtlety, this is a 'stomp 'em in the face' tale for the masses. A remarkable thing about this film however is that it is a very early full frontal attack on what Eisenhower was eventually to name 'the military industrial complex'. It isn't just a story about gun-runners, but about arming anyone for money, and doing so from the heart of Washington. But let's not get into politics, let's leave that to DeMille, who can be guaranteed to be superficial. The chief interest of this film all these years later is that it uses the first film score composed by George Antheil, who has a lot to say about the job in his autobiography, 'Bad Boy of Music'. Antheil seems to have originated 'the big sound' adopted by all subsequent Westerns, whereby the plains sing out with the voices and sounds of countless cowboys in the sky, celebrating the open spaces and interweaving common melodies. That is why it does not sound at all unusual, because we have heard it a thousand times. But he seems to have been the first to summon up the combined rustlings of all the sage brush into this symphony of the open skies which has entered into American mythic lore, and given it a soundtrack which has never varied since then, corny as it may be, but doubtless appropriate. It is amusing to see Anthony Quinn in an early appearance as a Cheyenne Indian. Gabby Hayes is in there somewhere, but you miss him in the crowd. Gary Cooper overtops them all, looming large, - but when did he ever loom small?\"]\n",
"\n",
"labels: [0 1 1]\n"
]
}
],
"source": [
"def plot_graphs(history, metric):\n",
" plt.plot(history.history[metric])\n",
" plt.plot(history.history['val_'+metric], '')\n",
" plt.xlabel(\"Epochs\")\n",
" plt.ylabel(metric)\n",
" plt.legend([metric, 'val_'+metric])\n",
"\n",
"dataset, info = tfds.load('imdb_reviews', with_info=True, as_supervised=True)\n",
"train_dataset, test_dataset = dataset['train'], dataset['test']\n",
"\n",
"for example, label in train_dataset.take(1):\n",
" print('text: ', example.numpy())\n",
" print('label: ', label.numpy())\n",
"\n",
"BUFFER_SIZE = 10000\n",
"BATCH_SIZE = 64\n",
"\n",
"train_dataset = train_dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)\n",
"test_dataset = test_dataset.batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)\n",
"\n",
"for example, label in train_dataset.take(1):\n",
" print('texts: ', example.numpy()[:3])\n",
" print()\n",
" print('labels: ', label.numpy()[:3])\n",
"\n",
"VOCAB_SIZE = 1000\n",
"encoder = tf.keras.layers.TextVectorization(max_tokens=VOCAB_SIZE)\n",
"encoder.adapt(train_dataset.map(lambda text, label: text))\n",
"\n",
"model = tf.keras.Sequential([\n",
" encoder,\n",
" tf.keras.layers.Embedding(\n",
" input_dim=len(encoder.get_vocabulary()),\n",
" output_dim=64,\n",
" # Use masking to handle the variable sequence lengths\n",
" mask_zero=True\n",
" ),\n",
" tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),\n",
" tf.keras.layers.Dense(64, activation='relu'),\n",
" tf.keras.layers.Dense(1)\n",
"])\n",
"\n",
"model.compile(\n",
" loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n",
" optimizer=tf.keras.optimizers.Adam(1e-4),\n",
" metrics=['accuracy']\n",
")\n",
"\n",
"checkpoint = tf.train.Checkpoint(\n",
" model=model\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/10\n",
"WARNING:tensorflow:From f:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\keras\\src\\engine\\base_layer_utils.py:384: The name tf.executing_eagerly_outside_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outside_functions instead.\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From f:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\keras\\src\\engine\\base_layer_utils.py:384: The name tf.executing_eagerly_outside_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outside_functions instead.\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"391/391 [==============================] - 439s 1s/step - loss: 0.6559 - accuracy: 0.5520 - val_loss: 0.5300 - val_accuracy: 0.7615\n",
"Epoch 2/10\n",
"391/391 [==============================] - 428s 1s/step - loss: 0.4360 - accuracy: 0.7975 - val_loss: 0.3924 - val_accuracy: 0.8203\n",
"Epoch 3/10\n",
"391/391 [==============================] - 852s 2s/step - loss: 0.3608 - accuracy: 0.8432 - val_loss: 0.3666 - val_accuracy: 0.8250\n",
"Epoch 4/10\n",
"391/391 [==============================] - 1106s 3s/step - loss: 0.3334 - accuracy: 0.8585 - val_loss: 0.3467 - val_accuracy: 0.8349\n",
"Epoch 5/10\n",
"391/391 [==============================] - 716s 2s/step - loss: 0.3153 - accuracy: 0.8655 - val_loss: 0.3263 - val_accuracy: 0.8568\n",
"Epoch 6/10\n",
"391/391 [==============================] - 1155s 3s/step - loss: 0.3116 - accuracy: 0.8684 - val_loss: 0.3270 - val_accuracy: 0.8542\n",
"Epoch 7/10\n",
"391/391 [==============================] - 1022s 3s/step - loss: 0.3037 - accuracy: 0.8723 - val_loss: 0.3213 - val_accuracy: 0.8578\n",
"Epoch 8/10\n",
"391/391 [==============================] - 433s 1s/step - loss: 0.3022 - accuracy: 0.8722 - val_loss: 0.3266 - val_accuracy: 0.8609\n",
"Epoch 9/10\n",
"391/391 [==============================] - 430s 1s/step - loss: 0.2968 - accuracy: 0.8750 - val_loss: 0.3257 - val_accuracy: 0.8620\n",
"Epoch 10/10\n",
"391/391 [==============================] - 438s 1s/step - loss: 0.2962 - accuracy: 0.8764 - val_loss: 0.3417 - val_accuracy: 0.8557\n"
]
}
],
"source": [
"history = model.fit(\n",
" train_dataset,\n",
" epochs=10,\n",
" validation_data=test_dataset,\n",
" validation_steps=30\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<tensorflow.python.checkpoint.checkpoint.CheckpointLoadStatus at 0x1a70719f550>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"checkpoint.restore(tf.train.latest_checkpoint(\".\"))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From f:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\keras\\src\\engine\\base_layer_utils.py:384: The name tf.executing_eagerly_outside_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outside_functions instead.\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From f:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\keras\\src\\engine\\base_layer_utils.py:384: The name tf.executing_eagerly_outside_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outside_functions instead.\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"391/391 [==============================] - 48s 115ms/step - loss: 0.3387 - accuracy: 0.8605\n",
"Test Loss: 0.3387344777584076\n",
"Test Accuracy: 0.8605200052261353\n"
]
}
],
"source": [
"test_loss, test_acc = model.evaluate(test_dataset)\n",
"\n",
"print('Test Loss:', test_loss)\n",
"print('Test Accuracy:', test_acc)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"ename": "FailedPreconditionError",
"evalue": "./reference is not a directory",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mFailedPreconditionError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[7], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mcheckpoint\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msave\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile_prefix\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m./reference/RNN_chpt\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32mf:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\tensorflow\\python\\checkpoint\\checkpoint.py:2508\u001b[0m, in \u001b[0;36mCheckpoint.save\u001b[1;34m(self, file_prefix, options)\u001b[0m\n\u001b[0;32m 2503\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m 2504\u001b[0m options\u001b[38;5;241m.\u001b[39mexperimental_write_callbacks\u001b[38;5;241m.\u001b[39mappend(\n\u001b[0;32m 2505\u001b[0m _update_checkpoint_state_internal\n\u001b[0;32m 2506\u001b[0m )\n\u001b[1;32m-> 2508\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_write\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 2509\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;132;43;01m%s\u001b[39;49;00m\u001b[38;5;124;43m-\u001b[39;49m\u001b[38;5;132;43;01m%d\u001b[39;49;00m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m%\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile_prefix\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcheckpoint_number\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 2510\u001b[0m \u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32mf:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\tensorflow\\python\\checkpoint\\checkpoint.py:2344\u001b[0m, in \u001b[0;36mCheckpoint._write\u001b[1;34m(self, file_prefix, options)\u001b[0m\n\u001b[0;32m 2342\u001b[0m start_time \u001b[38;5;241m=\u001b[39m time\u001b[38;5;241m.\u001b[39mtime()\n\u001b[0;32m 2343\u001b[0m options \u001b[38;5;241m=\u001b[39m options \u001b[38;5;129;01mor\u001b[39;00m checkpoint_options\u001b[38;5;241m.\u001b[39mCheckpointOptions()\n\u001b[1;32m-> 2344\u001b[0m output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_saver\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msave\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile_prefix\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfile_prefix\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 2345\u001b[0m output \u001b[38;5;241m=\u001b[39m _convert_file_name_tensor_to_string(output)\n\u001b[0;32m 2347\u001b[0m \u001b[38;5;66;03m# Execute callbacks (the only place they are executed; i.e. all entry points\u001b[39;00m\n\u001b[0;32m 2348\u001b[0m \u001b[38;5;66;03m# for callbacks will ultimately be directed to here for execution)\u001b[39;00m\n",
"File \u001b[1;32mf:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\tensorflow\\python\\checkpoint\\checkpoint.py:1330\u001b[0m, in \u001b[0;36mTrackableSaver.save\u001b[1;34m(self, file_prefix, checkpoint_number, session, options)\u001b[0m\n\u001b[0;32m 1327\u001b[0m object_graph_tensor \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 1329\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m tensor_util\u001b[38;5;241m.\u001b[39mis_tensor(file_prefix):\n\u001b[1;32m-> 1330\u001b[0m \u001b[43mfile_io\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrecursive_create_dir\u001b[49m\u001b[43m(\u001b[49m\u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpath\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdirname\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile_prefix\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 1332\u001b[0m save_path, new_feed_additions \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_save_cached_when_graph_building(\n\u001b[0;32m 1333\u001b[0m file_prefix_tensor, object_graph_tensor, options)\n\u001b[0;32m 1335\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m new_feed_additions:\n",
"File \u001b[1;32mf:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\tensorflow\\python\\lib\\io\\file_io.py:498\u001b[0m, in \u001b[0;36mrecursive_create_dir\u001b[1;34m(dirname)\u001b[0m\n\u001b[0;32m 486\u001b[0m \u001b[38;5;129m@tf_export\u001b[39m(v1\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgfile.MakeDirs\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n\u001b[0;32m 487\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrecursive_create_dir\u001b[39m(dirname):\n\u001b[0;32m 488\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Creates a directory and all parent/intermediate directories.\u001b[39;00m\n\u001b[0;32m 489\u001b[0m \n\u001b[0;32m 490\u001b[0m \u001b[38;5;124;03m It succeeds if dirname already exists and is writable.\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 496\u001b[0m \u001b[38;5;124;03m errors.OpError: If the operation fails.\u001b[39;00m\n\u001b[0;32m 497\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[1;32m--> 498\u001b[0m \u001b[43mrecursive_create_dir_v2\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdirname\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32mf:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\tensorflow\\python\\lib\\io\\file_io.py:513\u001b[0m, in \u001b[0;36mrecursive_create_dir_v2\u001b[1;34m(path)\u001b[0m\n\u001b[0;32m 501\u001b[0m \u001b[38;5;129m@tf_export\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mio.gfile.makedirs\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 502\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrecursive_create_dir_v2\u001b[39m(path):\n\u001b[0;32m 503\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Creates a directory and all parent/intermediate directories.\u001b[39;00m\n\u001b[0;32m 504\u001b[0m \n\u001b[0;32m 505\u001b[0m \u001b[38;5;124;03m It succeeds if path already exists and is writable.\u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 511\u001b[0m \u001b[38;5;124;03m errors.OpError: If the operation fails.\u001b[39;00m\n\u001b[0;32m 512\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[1;32m--> 513\u001b[0m \u001b[43m_pywrap_file_io\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mRecursivelyCreateDir\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcompat\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpath_to_bytes\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpath\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[1;31mFailedPreconditionError\u001b[0m: ./reference is not a directory"
]
}
],
"source": [
"checkpoint.save(file_prefix = \"./reference/RNN_chpt\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Pakeiskite tokius parametrus kaip buferio dydis ir žodyno dydis."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From f:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\keras\\src\\backend.py:873: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From f:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\keras\\src\\backend.py:873: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From f:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\keras\\src\\utils\\tf_utils.py:492: The name tf.ragged.RaggedTensorValue is deprecated. Please use tf.compat.v1.ragged.RaggedTensorValue instead.\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From f:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\keras\\src\\utils\\tf_utils.py:492: The name tf.ragged.RaggedTensorValue is deprecated. Please use tf.compat.v1.ragged.RaggedTensorValue instead.\n",
"\n"
]
}
],
"source": [
"BUFFER_SIZE = 1000\n",
"BATCH_SIZE = 64\n",
"VOCAB_SIZE = 100\n",
"\n",
"dataset, info = tfds.load('imdb_reviews', with_info=True, as_supervised=True)\n",
"train_dataset, test_dataset = dataset['train'], dataset['test']\n",
"\n",
"train_dataset = train_dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)\n",
"test_dataset = test_dataset.batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)\n",
"\n",
"encoder = tf.keras.layers.TextVectorization(max_tokens=VOCAB_SIZE)\n",
"encoder.adapt(train_dataset.map(lambda text, label: text))\n",
"\n",
"model = tf.keras.Sequential([\n",
" encoder,\n",
" tf.keras.layers.Embedding(\n",
" input_dim=len(encoder.get_vocabulary()),\n",
" output_dim=64,\n",
" # Use masking to handle the variable sequence lengths\n",
" mask_zero=True\n",
" ),\n",
" tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),\n",
" tf.keras.layers.Dense(64, activation='relu'),\n",
" tf.keras.layers.Dense(1)\n",
"])\n",
"\n",
"model.compile(\n",
" loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n",
" optimizer=tf.keras.optimizers.Adam(1e-4),\n",
" metrics=['accuracy']\n",
")\n",
"\n",
"checkpoint = tf.train.Checkpoint(\n",
" model=model\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/10\n",
"WARNING:tensorflow:From f:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\keras\\src\\engine\\base_layer_utils.py:384: The name tf.executing_eagerly_outside_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outside_functions instead.\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From f:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\keras\\src\\engine\\base_layer_utils.py:384: The name tf.executing_eagerly_outside_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outside_functions instead.\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"391/391 [==============================] - 419s 1s/step - loss: 0.6884 - accuracy: 0.5036 - val_loss: 0.6677 - val_accuracy: 0.4917\n",
"Epoch 2/10\n",
"391/391 [==============================] - 483s 1s/step - loss: 0.6242 - accuracy: 0.5987 - val_loss: 0.6088 - val_accuracy: 0.6245\n",
"Epoch 3/10\n",
"391/391 [==============================] - 451s 1s/step - loss: 0.5704 - accuracy: 0.6875 - val_loss: 0.5618 - val_accuracy: 0.7161\n",
"Epoch 4/10\n",
"391/391 [==============================] - 461s 1s/step - loss: 0.5462 - accuracy: 0.7132 - val_loss: 0.5517 - val_accuracy: 0.7167\n",
"Epoch 5/10\n",
"391/391 [==============================] - 473s 1s/step - loss: 0.5398 - accuracy: 0.7162 - val_loss: 0.5479 - val_accuracy: 0.7078\n",
"Epoch 6/10\n",
"391/391 [==============================] - 471s 1s/step - loss: 0.5335 - accuracy: 0.7213 - val_loss: 0.5417 - val_accuracy: 0.7115\n",
"Epoch 7/10\n",
"391/391 [==============================] - 495s 1s/step - loss: 0.5311 - accuracy: 0.7222 - val_loss: 0.5438 - val_accuracy: 0.7120\n",
"Epoch 8/10\n",
"391/391 [==============================] - 629s 2s/step - loss: 0.5296 - accuracy: 0.7226 - val_loss: 0.5371 - val_accuracy: 0.7188\n",
"Epoch 9/10\n",
"391/391 [==============================] - 1174s 3s/step - loss: 0.5290 - accuracy: 0.7241 - val_loss: 0.5492 - val_accuracy: 0.7245\n",
"Epoch 10/10\n",
"391/391 [==============================] - 959s 2s/step - loss: 0.5254 - accuracy: 0.7247 - val_loss: 0.5409 - val_accuracy: 0.7021\n"
]
}
],
"source": [
"history = model.fit(\n",
" train_dataset,\n",
" epochs=10,\n",
" validation_data=test_dataset,\n",
" validation_steps=30\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"ename": "RuntimeError",
"evalue": "You must compile your model before training/testing. Use `model.compile(optimizer, loss)`.",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mRuntimeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[8], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m test_loss, test_acc \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mevaluate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtest_dataset\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mTest Loss:\u001b[39m\u001b[38;5;124m'\u001b[39m, test_loss)\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mTest Accuracy:\u001b[39m\u001b[38;5;124m'\u001b[39m, test_acc)\n",
"File \u001b[1;32mf:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\keras\\src\\utils\\traceback_utils.py:70\u001b[0m, in \u001b[0;36mfilter_traceback.<locals>.error_handler\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 67\u001b[0m filtered_tb \u001b[38;5;241m=\u001b[39m _process_traceback_frames(e\u001b[38;5;241m.\u001b[39m__traceback__)\n\u001b[0;32m 68\u001b[0m \u001b[38;5;66;03m# To get the full stack trace, call:\u001b[39;00m\n\u001b[0;32m 69\u001b[0m \u001b[38;5;66;03m# `tf.debugging.disable_traceback_filtering()`\u001b[39;00m\n\u001b[1;32m---> 70\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\u001b[38;5;241m.\u001b[39mwith_traceback(filtered_tb) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m 71\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[0;32m 72\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m filtered_tb\n",
"File \u001b[1;32mf:\\KTU\\Neuroninių tinklų metodai\\venv\\Lib\\site-packages\\keras\\src\\engine\\training.py:3983\u001b[0m, in \u001b[0;36mModel._assert_compile_was_called\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 3977\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_assert_compile_was_called\u001b[39m(\u001b[38;5;28mself\u001b[39m):\n\u001b[0;32m 3978\u001b[0m \u001b[38;5;66;03m# Checks whether `compile` has been called. If it has been called,\u001b[39;00m\n\u001b[0;32m 3979\u001b[0m \u001b[38;5;66;03m# then the optimizer is set. This is different from whether the\u001b[39;00m\n\u001b[0;32m 3980\u001b[0m \u001b[38;5;66;03m# model is compiled\u001b[39;00m\n\u001b[0;32m 3981\u001b[0m \u001b[38;5;66;03m# (i.e. whether the model is built and its inputs/outputs are set).\u001b[39;00m\n\u001b[0;32m 3982\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_is_compiled:\n\u001b[1;32m-> 3983\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\n\u001b[0;32m 3984\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mYou must compile your model before \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 3985\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtraining/testing. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 3986\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUse `model.compile(optimizer, loss)`.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 3987\u001b[0m )\n",
"\u001b[1;31mRuntimeError\u001b[0m: You must compile your model before training/testing. Use `model.compile(optimizer, loss)`."
]
}
],
"source": [
"test_loss, test_acc = model.evaluate(test_dataset)\n",
"\n",
"print('Test Loss:', test_loss)\n",
"print('Test Accuracy:', test_acc)\n",
"\n",
"plot_graphs(history, 'accuracy')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. Pakeiskite Bidirectional sluoksnį į paprastą RNN ir palyginkite modelių tikslumą."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"model = tf.keras.Sequential([\n",
" encoder,\n",
" tf.keras.layers.Embedding(\n",
" input_dim=len(encoder.get_vocabulary()),\n",
" output_dim=64,\n",
" # Use masking to handle the variable sequence lengths\n",
" mask_zero=True\n",
" ),\n",
" tf.keras.layers.SimpleRNN(64),\n",
" tf.keras.layers.Dense(64, activation='relu'),\n",
" tf.keras.layers.Dense(1)\n",
"])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

496
Lab3/5/main.ipynb Normal file

File diff suppressed because one or more lines are too long

15
Lab3/README.md Normal file
View File

@ -0,0 +1,15 @@
# Lab3
## Informacija apie atsiskaitymą
* Laboratorinių darbų atsiskaitymui 3 atlikite Lab41, Lab42, Lab5 nurodytus uždavinius.
* Neuroninių tinklų modelius, skaičiavimus, rezultatus, komentarus ir išvadas pateikite interaktyviose JupyterLab užrašų knygutėse.
* Laboratorinio darbo pristatymo metu mokėkite paaiškinti atliekamus veiksmus ir gaunamus rezultatus.
* Modelius, kurių apmokymas užtrunka ilgai, išsaugokite.
* Atsiskaitymo metu mokėkite pakeisti programos kodą.
* Gerai įsigilinkite į teorinius metodų, kuriuos taikote laboratorinių darbų uždavinių sprendimui, aspektus. Atsiskaitymo metu mokėkite juos paaiškinti.
## Vertinimo kriterijai
* Pateiktų uždavinių sprendimas ir rezultatų pateikimas JupyterLab aplinkoje - 30%.
* Gautų rezultatų korektiškumas - 30%.
* Praktinės užduoties atlikimas - 20%.
* Teorinių aspektų supratimas - 20%.

1355
Lab3/examples/Lab41.ipynb Normal file

File diff suppressed because one or more lines are too long