Skip to content

Commit 53225a5

Browse files
committed
Update to new version of Keras
1 parent 633a1ff commit 53225a5

File tree

1 file changed

+76
-36
lines changed

1 file changed

+76
-36
lines changed

source-code/keras/mnist_ensemble.ipynb

Lines changed: 76 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,17 @@
1616
},
1717
{
1818
"cell_type": "code",
19-
"execution_count": 1,
19+
"execution_count": 2,
2020
"metadata": {},
21-
"outputs": [
22-
{
23-
"name": "stderr",
24-
"output_type": "stream",
25-
"text": [
26-
"Using TensorFlow backend.\n"
27-
]
28-
}
29-
],
21+
"outputs": [],
3022
"source": [
3123
"from collections import Counter\n",
32-
"import keras\n",
24+
"from tensorflow import keras\n",
3325
"from keras.datasets import mnist\n",
3426
"from keras.layers import Dense, Dropout\n",
3527
"from keras.models import load_model, Sequential\n",
3628
"from keras.optimizers import SGD\n",
37-
"from keras.utils import np_utils\n",
29+
"from keras.utils import to_categorical\n",
3830
"import matplotlib.pyplot as plt\n",
3931
"%matplotlib inline\n",
4032
"import numpy as np\n",
@@ -57,7 +49,7 @@
5749
},
5850
{
5951
"cell_type": "code",
60-
"execution_count": 2,
52+
"execution_count": 3,
6153
"metadata": {},
6254
"outputs": [],
6355
"source": [
@@ -67,14 +59,14 @@
6759
" x_test = x_test.reshape(10000, 784)\n",
6860
" x_train = x_train.astype(np.float32)/255.0\n",
6961
" x_test = x_test.astype(np.float32)/255.0\n",
70-
" y_train = np_utils.to_categorical(y_train)\n",
71-
" y_test = np_utils.to_categorical(y_test)\n",
62+
" y_train = to_categorical(y_train)\n",
63+
" y_test = to_categorical(y_test)\n",
7264
" return x_train, y_train, x_test, y_test"
7365
]
7466
},
7567
{
7668
"cell_type": "code",
77-
"execution_count": 3,
69+
"execution_count": 4,
7870
"metadata": {},
7971
"outputs": [],
8072
"source": [
@@ -97,7 +89,7 @@
9789
},
9890
{
9991
"cell_type": "code",
100-
"execution_count": 4,
92+
"execution_count": 5,
10193
"metadata": {},
10294
"outputs": [],
10395
"source": [
@@ -129,7 +121,7 @@
129121
},
130122
{
131123
"cell_type": "code",
132-
"execution_count": 5,
124+
"execution_count": 6,
133125
"metadata": {},
134126
"outputs": [],
135127
"source": [
@@ -145,7 +137,7 @@
145137
},
146138
{
147139
"cell_type": "code",
148-
"execution_count": 6,
140+
"execution_count": 7,
149141
"metadata": {
150142
"scrolled": true
151143
},
@@ -154,7 +146,44 @@
154146
"name": "stdout",
155147
"output_type": "stream",
156148
"text": [
157-
"model 1\n",
149+
"model 1\n"
150+
]
151+
},
152+
{
153+
"name": "stderr",
154+
"output_type": "stream",
155+
"text": [
156+
"2024-03-20 09:54:27.891996: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node\n",
157+
"Your kernel may have been built without NUMA support.\n",
158+
"2024-03-20 09:54:28.321918: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node\n",
159+
"Your kernel may have been built without NUMA support.\n",
160+
"2024-03-20 09:54:28.321969: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node\n",
161+
"Your kernel may have been built without NUMA support.\n",
162+
"2024-03-20 09:54:28.326420: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node\n",
163+
"Your kernel may have been built without NUMA support.\n",
164+
"2024-03-20 09:54:28.326464: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node\n",
165+
"Your kernel may have been built without NUMA support.\n",
166+
"2024-03-20 09:54:28.326487: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node\n",
167+
"Your kernel may have been built without NUMA support.\n",
168+
"2024-03-20 09:54:35.782651: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node\n",
169+
"Your kernel may have been built without NUMA support.\n",
170+
"2024-03-20 09:54:35.783569: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node\n",
171+
"Your kernel may have been built without NUMA support.\n",
172+
"2024-03-20 09:54:35.783589: I tensorflow/core/common_runtime/gpu/gpu_device.cc:2022] Could not identify NUMA node of platform GPU id 0, defaulting to 0. Your kernel may not have been built with NUMA support.\n",
173+
"2024-03-20 09:54:35.783689: I external/local_xla/xla/stream_executor/cuda/cuda_executor.cc:887] could not open file to read NUMA node: /sys/bus/pci/devices/0000:01:00.0/numa_node\n",
174+
"Your kernel may have been built without NUMA support.\n",
175+
"2024-03-20 09:54:35.784579: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1929] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 3421 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3060 Laptop GPU, pci bus id: 0000:01:00.0, compute capability: 8.6\n",
176+
"2024-03-20 09:55:35.800249: I external/local_xla/xla/service/service.cc:168] XLA service 0x7f56bc0020b0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:\n",
177+
"2024-03-20 09:55:35.800419: I external/local_xla/xla/service/service.cc:176] StreamExecutor device (0): NVIDIA GeForce RTX 3060 Laptop GPU, Compute Capability 8.6\n",
178+
"2024-03-20 09:55:37.662907: I external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:454] Loaded cuDNN version 8907\n",
179+
"WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n",
180+
"I0000 00:00:1710924937.828903 11615 device_compiler.h:186] Compiled cluster using XLA! This line is logged at most once for the lifetime of the process.\n"
181+
]
182+
},
183+
{
184+
"name": "stdout",
185+
"output_type": "stream",
186+
"text": [
158187
"model 2\n",
159188
"model 3\n"
160189
]
@@ -188,16 +217,16 @@
188217
},
189218
{
190219
"cell_type": "code",
191-
"execution_count": 7,
220+
"execution_count": 8,
192221
"metadata": {},
193222
"outputs": [
194223
{
195224
"data": {
196225
"text/plain": [
197-
"[0.9514, 0.9506, 0.9488]"
226+
"[0.9503999948501587, 0.9484999775886536, 0.9495999813079834]"
198227
]
199228
},
200-
"execution_count": 7,
229+
"execution_count": 8,
201230
"metadata": {},
202231
"output_type": "execute_result"
203232
}
@@ -219,13 +248,24 @@
219248
},
220249
{
221250
"cell_type": "code",
222-
"execution_count": 8,
251+
"execution_count": 10,
223252
"metadata": {},
224-
"outputs": [],
253+
"outputs": [
254+
{
255+
"name": "stdout",
256+
"output_type": "stream",
257+
"text": [
258+
"313/313 [==============================] - 1s 2ms/step\n",
259+
"313/313 [==============================] - 1s 2ms/step\n",
260+
"313/313 [==============================] - 0s 1ms/step\n"
261+
]
262+
}
263+
],
225264
"source": [
226265
"y_infer = np.empty((len(models), len(y_test)), dtype=np.int64)\n",
227266
"for i, model in enumerate(models):\n",
228-
" y_infer[i, :] = model.predict_classes(x_test)"
267+
" prediction = model.predict(x_test)\n",
268+
" y_infer[i, :] = np.argmax(prediction, axis=1)"
229269
]
230270
},
231271
{
@@ -237,7 +277,7 @@
237277
},
238278
{
239279
"cell_type": "code",
240-
"execution_count": 9,
280+
"execution_count": 11,
241281
"metadata": {
242282
"scrolled": true
243283
},
@@ -246,7 +286,7 @@
246286
"name": "stdout",
247287
"output_type": "stream",
248288
"text": [
249-
"0.9519\n"
289+
"0.951\n"
250290
]
251291
}
252292
],
@@ -285,7 +325,7 @@
285325
},
286326
{
287327
"cell_type": "code",
288-
"execution_count": 15,
328+
"execution_count": 12,
289329
"metadata": {},
290330
"outputs": [],
291331
"source": [
@@ -294,7 +334,7 @@
294334
},
295335
{
296336
"cell_type": "code",
297-
"execution_count": 16,
337+
"execution_count": 13,
298338
"metadata": {},
299339
"outputs": [],
300340
"source": [
@@ -306,16 +346,16 @@
306346
},
307347
{
308348
"cell_type": "code",
309-
"execution_count": 17,
349+
"execution_count": 14,
310350
"metadata": {},
311351
"outputs": [
312352
{
313353
"data": {
314354
"text/plain": [
315-
"[0.09286915022358298, 0.9707]"
355+
"[0.09376676380634308, 0.9704999923706055]"
316356
]
317357
},
318-
"execution_count": 17,
358+
"execution_count": 14,
319359
"metadata": {},
320360
"output_type": "execute_result"
321361
}
@@ -334,7 +374,7 @@
334374
],
335375
"metadata": {
336376
"kernelspec": {
337-
"display_name": "Python 3",
377+
"display_name": "Python 3 (ipykernel)",
338378
"language": "python",
339379
"name": "python3"
340380
},
@@ -348,9 +388,9 @@
348388
"name": "python",
349389
"nbconvert_exporter": "python",
350390
"pygments_lexer": "ipython3",
351-
"version": "3.6.6"
391+
"version": "3.11.8"
352392
}
353393
},
354394
"nbformat": 4,
355-
"nbformat_minor": 2
395+
"nbformat_minor": 4
356396
}

0 commit comments

Comments
 (0)