Skip to content

Commit

Permalink
[MISC] Introduction on Neural Networks (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
deusebio committed Oct 3, 2024
1 parent 9668715 commit f52839b
Show file tree
Hide file tree
Showing 10 changed files with 4,928 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
folder: Chapter02
- name: chap3
folder: Chapter03
- name: chap-nn
folder: ChapterNN
- name: chap5
folder: Chapter05
- name: chap6
Expand Down
237 changes: 237 additions & 0 deletions ChapterNN/01_ImageClassification_TensorFlow.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "8c4e9e09-b0c6-4671-86a0-ca6bc73056b6",
"metadata": {},
"source": [
"# Image Classification with TensorFlow"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "012237ce-0396-4c88-8b13-994c7a830421",
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow.keras.datasets import fashion_mnist\n",
"from matplotlib import pyplot as plt"
]
},
{
"cell_type": "markdown",
"id": "6080305f-eaac-49d2-a4cb-658a907186ac",
"metadata": {},
"source": [
"### Load and re-scale input data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1135c9e8-1765-48cc-beb8-25fd6ff363d4",
"metadata": {},
"outputs": [],
"source": [
"(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee2989f4-9b32-48f6-b669-8255aa9e9c79",
"metadata": {},
"outputs": [],
"source": [
"x_train = x_train.astype('float32') / 255.\n",
"x_test = x_test.astype('float32') / 255.\n",
"\n",
"print (x_train.shape)\n",
"print (x_test.shape)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0525097f-4b57-4e9c-b850-966540589a30",
"metadata": {},
"outputs": [],
"source": [
"classes = {\n",
" 0: \"T-shirt\",\n",
" 1: \"Trouser\",\n",
" 2: \"Pullover\",\n",
" 3: \"Dress\",\n",
" 4: \"Coat\",\n",
" 5: \"Sandal\",\n",
" 6: \"Shirt\",\n",
" 7: \"Sneaker\",\n",
" 8: \"Bag\",\n",
" 9: \"Ankle boot\", \n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a0e2bc95-ee33-4024-99d2-4ba7cb4fd0c6",
"metadata": {},
"outputs": [],
"source": [
"n = 6\n",
"plt.figure(figsize=(20, 4))\n",
"for i in range(n):\n",
" # display original\n",
" ax = plt.subplot(1, n, i + 1)\n",
" plt.imshow(x_test[i])\n",
" plt.title(classes[y_test[i]])\n",
" plt.gray()\n",
" ax.get_xaxis().set_visible(False)\n",
" ax.get_yaxis().set_visible(False)\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "c7ace701-0cd8-4173-982c-8682a860dd26",
"metadata": {},
"source": [
"### Build model"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a2d549c8-a410-4caa-95a4-b0bb20a05236",
"metadata": {},
"outputs": [],
"source": [
"model = tf.keras.models.Sequential([\n",
" tf.keras.layers.Flatten(input_shape=(28, 28)),\n",
" tf.keras.layers.Dense(128, activation='relu'),\n",
" tf.keras.layers.Dropout(0.2),\n",
" tf.keras.layers.Dense(10)\n",
"])\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f20ef704-3435-4f12-b41b-db42fcfb3b43",
"metadata": {},
"outputs": [],
"source": [
"model.summary()"
]
},
{
"cell_type": "markdown",
"id": "9af979aa-9ccb-4b92-b0fd-ef39fcf6f317",
"metadata": {},
"source": [
"### Train the network"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "53f02c48-3d8f-4e41-a9d4-703016a0dc19",
"metadata": {},
"outputs": [],
"source": [
"loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)\n",
"optimizer = tf.keras.optimizers.Adam()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6427e500-41d2-43f1-b235-622d1d59572e",
"metadata": {},
"outputs": [],
"source": [
"model.compile(optimizer=optimizer,\n",
" loss=loss_fn,\n",
" metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef1fea96-97cc-4c84-bb0c-78417a571575",
"metadata": {},
"outputs": [],
"source": [
"model.fit(\n",
" x_train, \n",
" y_train, \n",
" validation_data=(x_test, y_test), \n",
" epochs=20, \n",
" batch_size=128,\n",
" shuffle=True\n",
")"
]
},
{
"cell_type": "markdown",
"id": "a8c64c99-cbab-4503-8a1d-84f80c6f2af7",
"metadata": {},
"source": [
"### More advanced model"
]
},
{
"cell_type": "markdown",
"id": "c4649c36-1157-438b-bff0-01e980aa7da3",
"metadata": {},
"source": [
"For a slightly more complex and deeper network, try to train the model below"
]
},
{
"cell_type": "raw",
"id": "e9af3bfc-7b19-4441-9dc4-46df1b3739cc",
"metadata": {},
"source": [
"input_img = tf.keras.layers.Input(shape=(28, 28, 1))\n",
"\n",
"x = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)\n",
"x = tf.keras.layers.MaxPooling2D((2, 2), padding='same')(x)\n",
"x = tf.keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)\n",
"x = tf.keras.layers.MaxPooling2D((2, 2), padding='same')(x)\n",
"x = tf.keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)\n",
"x = tf.keras.layers.MaxPooling2D((2, 2), padding='same')(x)\n",
"x = tf.keras.layers.Flatten(input_shape=(26, 26))(x)\n",
"x = tf.keras.layers.Dense(128, activation='relu')(x)\n",
"x = tf.keras.layers.Dropout(0.2)(x)\n",
"x = tf.keras.layers.Dense(10)(x)\n",
"\n",
"model = tf.keras.Model(input_img, x)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "chap-nn",
"language": "python",
"name": "chap-nn"
},
"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.8.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit f52839b

Please sign in to comment.