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