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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 2,
|
6 |
+
"id": "780b8bd2-ddee-4f38-8507-48643ff1fb3d",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [
|
9 |
+
{
|
10 |
+
"name": "stdout",
|
11 |
+
"output_type": "stream",
|
12 |
+
"text": [
|
13 |
+
"2.13.0\n"
|
14 |
+
]
|
15 |
+
}
|
16 |
+
],
|
17 |
+
"source": [
|
18 |
+
"import tensorflow\n",
|
19 |
+
"print(tensorflow.__version__)"
|
20 |
+
]
|
21 |
+
},
|
22 |
+
{
|
23 |
+
"cell_type": "code",
|
24 |
+
"execution_count": 3,
|
25 |
+
"id": "edea97c0-8733-45a0-838e-07bdde3e09c6",
|
26 |
+
"metadata": {},
|
27 |
+
"outputs": [],
|
28 |
+
"source": [
|
29 |
+
"mnist = tensorflow.keras.datasets.mnist\n",
|
30 |
+
"\n",
|
31 |
+
"(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
|
32 |
+
"x_train, x_test = x_train / 255.0, x_test / 255.0"
|
33 |
+
]
|
34 |
+
},
|
35 |
+
{
|
36 |
+
"cell_type": "code",
|
37 |
+
"execution_count": 4,
|
38 |
+
"id": "e4f05156-c521-4706-84c5-c759de4243e3",
|
39 |
+
"metadata": {},
|
40 |
+
"outputs": [
|
41 |
+
{
|
42 |
+
"name": "stderr",
|
43 |
+
"output_type": "stream",
|
44 |
+
"text": [
|
45 |
+
"2024-04-28 13:30:52.410401: I tensorflow/core/common_runtime/process_util.cc:146] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.\n"
|
46 |
+
]
|
47 |
+
}
|
48 |
+
],
|
49 |
+
"source": [
|
50 |
+
"model = tensorflow.keras.models.Sequential([\n",
|
51 |
+
" tensorflow.keras.layers.Flatten(input_shape=(28, 28)),\n",
|
52 |
+
" tensorflow.keras.layers.Dense(128, activation='relu'),\n",
|
53 |
+
" tensorflow.keras.layers.Dropout(0.2),\n",
|
54 |
+
" tensorflow.keras.layers.Dense(10)\n",
|
55 |
+
"])"
|
56 |
+
]
|
57 |
+
},
|
58 |
+
{
|
59 |
+
"cell_type": "code",
|
60 |
+
"execution_count": 5,
|
61 |
+
"id": "0af8cfcd-1025-45cc-83bd-1af251495622",
|
62 |
+
"metadata": {},
|
63 |
+
"outputs": [
|
64 |
+
{
|
65 |
+
"data": {
|
66 |
+
"text/plain": [
|
67 |
+
"array([[ 0.51568913, -0.22694974, 0.7567456 , -0.8453866 , 0.40375337,\n",
|
68 |
+
" 0.06754087, 1.095877 , -0.82267034, 0.59129924, 0.15186527]],\n",
|
69 |
+
" dtype=float32)"
|
70 |
+
]
|
71 |
+
},
|
72 |
+
"execution_count": 5,
|
73 |
+
"metadata": {},
|
74 |
+
"output_type": "execute_result"
|
75 |
+
}
|
76 |
+
],
|
77 |
+
"source": [
|
78 |
+
"predictions = model(x_train[:1]).numpy()\n",
|
79 |
+
"predictions"
|
80 |
+
]
|
81 |
+
},
|
82 |
+
{
|
83 |
+
"cell_type": "code",
|
84 |
+
"execution_count": 6,
|
85 |
+
"id": "afab32b7-ef89-4f5b-8641-4d15f31137d3",
|
86 |
+
"metadata": {},
|
87 |
+
"outputs": [
|
88 |
+
{
|
89 |
+
"data": {
|
90 |
+
"text/plain": [
|
91 |
+
"2.571601"
|
92 |
+
]
|
93 |
+
},
|
94 |
+
"execution_count": 6,
|
95 |
+
"metadata": {},
|
96 |
+
"output_type": "execute_result"
|
97 |
+
}
|
98 |
+
],
|
99 |
+
"source": [
|
100 |
+
"loss_fn = tensorflow.keras.losses.SparseCategoricalCrossentropy(from_logits=True)\n",
|
101 |
+
"loss_fn(y_train[:1], predictions).numpy()"
|
102 |
+
]
|
103 |
+
},
|
104 |
+
{
|
105 |
+
"cell_type": "code",
|
106 |
+
"execution_count": 7,
|
107 |
+
"id": "e9c2c641-62ed-483b-a60a-73b7bef90052",
|
108 |
+
"metadata": {},
|
109 |
+
"outputs": [],
|
110 |
+
"source": [
|
111 |
+
"model.compile(\n",
|
112 |
+
" optimizer='adam',\n",
|
113 |
+
" loss=loss_fn,\n",
|
114 |
+
" metrics=['accuracy']\n",
|
115 |
+
")"
|
116 |
+
]
|
117 |
+
},
|
118 |
+
{
|
119 |
+
"cell_type": "code",
|
120 |
+
"execution_count": 8,
|
121 |
+
"id": "6cc7246e-e875-4d84-9447-2f4349e0d1a3",
|
122 |
+
"metadata": {},
|
123 |
+
"outputs": [
|
124 |
+
{
|
125 |
+
"name": "stdout",
|
126 |
+
"output_type": "stream",
|
127 |
+
"text": [
|
128 |
+
"Start time 2024-04-28 13:30:52.532594\n",
|
129 |
+
"Epoch 1/5\n",
|
130 |
+
"1875/1875 [==============================] - 10s 5ms/step - loss: 0.2980 - accuracy: 0.9135\n",
|
131 |
+
"Epoch 2/5\n",
|
132 |
+
"1875/1875 [==============================] - 33s 17ms/step - loss: 0.1455 - accuracy: 0.9574\n",
|
133 |
+
"Epoch 3/5\n",
|
134 |
+
"1875/1875 [==============================] - 19s 10ms/step - loss: 0.1104 - accuracy: 0.9668\n",
|
135 |
+
"Epoch 4/5\n",
|
136 |
+
"1875/1875 [==============================] - 17s 9ms/step - loss: 0.0874 - accuracy: 0.9724\n",
|
137 |
+
"Epoch 5/5\n",
|
138 |
+
"1875/1875 [==============================] - 17s 9ms/step - loss: 0.0774 - accuracy: 0.9759\n",
|
139 |
+
"End time 2024-04-28 13:33:15.639799\n",
|
140 |
+
"0:02:23.107205\n"
|
141 |
+
]
|
142 |
+
}
|
143 |
+
],
|
144 |
+
"source": [
|
145 |
+
"import datetime\n",
|
146 |
+
"\n",
|
147 |
+
"start = datetime.datetime.now()\n",
|
148 |
+
"print(f\"Start time {start}\")\n",
|
149 |
+
"\n",
|
150 |
+
"model.fit(x_train, y_train, epochs=5)\n",
|
151 |
+
"\n",
|
152 |
+
"end = datetime.datetime.now()\n",
|
153 |
+
"print(f\"End time {end}\")\n",
|
154 |
+
"print(end - start)"
|
155 |
+
]
|
156 |
+
},
|
157 |
+
{
|
158 |
+
"cell_type": "code",
|
159 |
+
"execution_count": 1,
|
160 |
+
"id": "1db98023-3052-4f26-8af0-4725eae0fad6",
|
161 |
+
"metadata": {},
|
162 |
+
"outputs": [
|
163 |
+
{
|
164 |
+
"name": "stderr",
|
165 |
+
"output_type": "stream",
|
166 |
+
"text": [
|
167 |
+
"2024-04-28 13:40:14.388937: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n",
|
168 |
+
"To enable the following instructions: AVX2 AVX512F FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
|
169 |
+
"2024-04-28 13:41:07.064024: I tensorflow/core/common_runtime/process_util.cc:146] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.\n"
|
170 |
+
]
|
171 |
+
},
|
172 |
+
{
|
173 |
+
"name": "stdout",
|
174 |
+
"output_type": "stream",
|
175 |
+
"text": [
|
176 |
+
"Start time 2024-04-28 13:41:08.409854\n",
|
177 |
+
"End time 2024-04-28 13:42:22.532379\n",
|
178 |
+
"0:01:14.122525\n"
|
179 |
+
]
|
180 |
+
}
|
181 |
+
],
|
182 |
+
"source": [
|
183 |
+
"import tensorflow\n",
|
184 |
+
"import datetime\n",
|
185 |
+
"\n",
|
186 |
+
"mnist = tensorflow.keras.datasets.mnist\n",
|
187 |
+
"(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
|
188 |
+
"x_train, x_test = x_train / 255.0, x_test / 255.0\n",
|
189 |
+
"\n",
|
190 |
+
"model = tensorflow.keras.models.Sequential([\n",
|
191 |
+
" tensorflow.keras.layers.Flatten(input_shape=(28, 28)),\n",
|
192 |
+
" tensorflow.keras.layers.Dense(128, activation='relu'),\n",
|
193 |
+
" tensorflow.keras.layers.Dropout(0.2),\n",
|
194 |
+
" tensorflow.keras.layers.Dense(10)\n",
|
195 |
+
"])\n",
|
196 |
+
"\n",
|
197 |
+
"loss_fn = tensorflow.keras.losses.SparseCategoricalCrossentropy(from_logits=True)\n",
|
198 |
+
"\n",
|
199 |
+
"optimizer = tensorflow.keras.optimizers.Adam()\n",
|
200 |
+
"\n",
|
201 |
+
"@tensorflow.function\n",
|
202 |
+
"def train_step(x, y):\n",
|
203 |
+
" with tensorflow.GradientTape() as tape:\n",
|
204 |
+
" predictions = model(x, training=True)\n",
|
205 |
+
" loss = loss_fn(y, predictions)\n",
|
206 |
+
" gradients = tape.gradient(loss, model.trainable_variables)\n",
|
207 |
+
" optimizer.apply_gradients(zip(gradients, model.trainable_variables))\n",
|
208 |
+
"\n",
|
209 |
+
"batch_size = 32\n",
|
210 |
+
"dataset = tensorflow.data.Dataset.from_tensor_slices((x_train, y_train)).batch(batch_size)\n",
|
211 |
+
"\n",
|
212 |
+
"start = datetime.datetime.now()\n",
|
213 |
+
"print(f\"Start time {start}\")\n",
|
214 |
+
"\n",
|
215 |
+
"epochs = 5\n",
|
216 |
+
"for epoch in range(epochs):\n",
|
217 |
+
" for x_batch, y_batch in dataset:\n",
|
218 |
+
" train_step(x_batch, y_batch)\n",
|
219 |
+
"\n",
|
220 |
+
"end = datetime.datetime.now()\n",
|
221 |
+
"print(f\"End time {end}\")\n",
|
222 |
+
"print(end - start)"
|
223 |
+
]
|
224 |
+
},
|
225 |
+
{
|
226 |
+
"cell_type": "code",
|
227 |
+
"execution_count": null,
|
228 |
+
"id": "62988706-fd36-4cf5-b80a-b4adf3ee3653",
|
229 |
+
"metadata": {},
|
230 |
+
"outputs": [],
|
231 |
+
"source": []
|
232 |
+
},
|
233 |
+
{
|
234 |
+
"cell_type": "code",
|
235 |
+
"execution_count": null,
|
236 |
+
"id": "fca4de9a-798c-46e9-bf70-b51cb7c79ab8",
|
237 |
+
"metadata": {},
|
238 |
+
"outputs": [],
|
239 |
+
"source": []
|
240 |
+
}
|
241 |
+
],
|
242 |
+
"metadata": {
|
243 |
+
"kernelspec": {
|
244 |
+
"display_name": "Tensorflow (Intel® oneAPI 2023.2)",
|
245 |
+
"language": "python",
|
246 |
+
"name": "c009-intel_distribution_of_python_3_oneapi-beta05-tf"
|
247 |
+
},
|
248 |
+
"language_info": {
|
249 |
+
"codemirror_mode": {
|
250 |
+
"name": "ipython",
|
251 |
+
"version": 3
|
252 |
+
},
|
253 |
+
"file_extension": ".py",
|
254 |
+
"mimetype": "text/x-python",
|
255 |
+
"name": "python",
|
256 |
+
"nbconvert_exporter": "python",
|
257 |
+
"pygments_lexer": "ipython3",
|
258 |
+
"version": "3.9.16"
|
259 |
+
}
|
260 |
+
},
|
261 |
+
"nbformat": 4,
|
262 |
+
"nbformat_minor": 5
|
263 |
+
}
|