Compare commits
2 Commits
9b5bd17fde
...
aeeb2e24d0
Author | SHA1 | Date | |
---|---|---|---|
aeeb2e24d0 | |||
2cb145e74c |
154
notebooks/03_ml_basics_ex_1_display_Cifar10_Greyscale.ipynb
Normal file
154
notebooks/03_ml_basics_ex_1_display_Cifar10_Greyscale.ipynb
Normal file
@ -0,0 +1,154 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "50680cbc",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Read/load the cifar10 dataset using tf.keras.datasets\n",
|
||||
"- Display the first 25 images\n",
|
||||
"- Convert them to greyscale images by reducing the 3 colors (r,g,b) to \n",
|
||||
" one greyscale\n",
|
||||
" using the formula gray = 0.2989 * r + 0.5870 * g + 0.1140 * b\n",
|
||||
"- Display the first 25 images in greyscale"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "2dc5ea2f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e846355a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Load the CIFAR-10 dataset\n",
|
||||
"(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "85a493d2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# In case of special pictures\n",
|
||||
"selectPicture = -1 # -1 for all or number for a class starting from 0\n",
|
||||
"\n",
|
||||
"# Define a list of class names for CIFAR-10\n",
|
||||
"class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4eacb734",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Get the indices of images with a special label in the training set\n",
|
||||
"special_indices = np.where(y_train == selectPicture)[0]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "be2d0cd2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Display the first 25 images in the training set\n",
|
||||
"plt.figure(figsize=(10,10))\n",
|
||||
"for i in range(25):\n",
|
||||
" plt.subplot(5, 5, i+1)\n",
|
||||
" if selectPicture == -1 :\n",
|
||||
" plt.imshow(x_train[i]) # all images\n",
|
||||
" plt.title(class_names[y_train[i][0]])\n",
|
||||
" else : \n",
|
||||
" plt.imshow(x_train[special_indices[i]]) # special Picture only\n",
|
||||
" plt.title(class_names[selectPicture]) \n",
|
||||
" plt.xticks([])\n",
|
||||
" plt.yticks([])\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "938e632d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Convert images to grayscale\n",
|
||||
"train_images_gray = np.dot(x_train[..., :3], [0.2989, 0.5870, 0.1140])\n",
|
||||
"test_images_gray = np.dot(x_test[..., :3], [0.2989, 0.5870, 0.1140])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "96333686",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Normalize pixel values to [0, 1]\n",
|
||||
"train_images_gray = train_images_gray / 255.0\n",
|
||||
"test_images_gray = test_images_gray / 255.0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "64d3d2ae",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Display the first 25 images in the training set\n",
|
||||
"plt.figure(figsize=(10,10))\n",
|
||||
"for i in range(25):\n",
|
||||
" plt.subplot(5, 5, i+1)\n",
|
||||
" if selectPicture == -1 :\n",
|
||||
" plt.imshow(train_images_gray[i],cmap='gray') # all images\n",
|
||||
" plt.title(class_names[y_train[i][0]])\n",
|
||||
" else : \n",
|
||||
" plt.imshow(train_images_gray[special_indices[i]],cmap='gray') # special Picture only\n",
|
||||
" plt.title(class_names[selectPicture]) \n",
|
||||
" plt.xticks([])\n",
|
||||
" plt.yticks([])\n",
|
||||
"plt.show()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.8.16"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
236
notebooks/03_ml_basics_ex_3_mlp_clothing.ipynb
Normal file
236
notebooks/03_ml_basics_ex_3_mlp_clothing.ipynb
Normal file
@ -0,0 +1,236 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d63563a8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Exercise 3\n",
|
||||
"# fashion mnist data\n",
|
||||
"# MLP model with two hidden layers, each with a ReLU activation function.\n",
|
||||
"# Input data is flattened to a 1D array and passed to the model."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "062f7519",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"from tensorflow import keras\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4ccb3a5e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Load the MNIST Fashion dataset\n",
|
||||
"(x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e5ede535",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Normalize pixel values to between 0 and 1\n",
|
||||
"x_train = x_train.astype(\"float32\") / 255.0\n",
|
||||
"x_test = x_test.astype(\"float32\") / 255.0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "39c100c1",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# MNIST dataset images have a shape of (28, 28). The images are flattened\n",
|
||||
"# into a 1D array of length 784 \n",
|
||||
"x_train = x_train.reshape(-1, 784)\n",
|
||||
"x_test = x_test.reshape(-1, 784)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e267a290",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# The model is defined here with three dense (fully connected) layers\n",
|
||||
"# The first layer is a Dense layer with 128 units and a ReLU activation\n",
|
||||
"# function with an input shape of (784,). This layer serves as the input\n",
|
||||
"# layer of the model.\n",
|
||||
"# The second layer is also a Dense layer with 64 units and a ReLU activation\n",
|
||||
"# function. This layer takes the output of the previous layer as input, and\n",
|
||||
"# applies a non-linear transformation to it to produce a new set of features\n",
|
||||
"# that the next layer can use.\n",
|
||||
"# The third is another Dense layer, one for each class in the output. The\n",
|
||||
"# output is raw scores or logits for each class since there is no activation\n",
|
||||
"# function . This layer is responsible for producing the final output of the\n",
|
||||
"# model, which can then be used to make predictions.\n",
|
||||
"# With Dropout(0.2) 20 % of the input is randomly droped, this should reduce overfitting\n",
|
||||
"model = keras.Sequential([\n",
|
||||
" keras.layers.Dense(128, activation='relu', input_shape=(784,)),\n",
|
||||
" # keras.layers.Dropout(0.2),\n",
|
||||
" keras.layers.Dense(64, activation='relu'),\n",
|
||||
" keras.layers.Dense(10)\n",
|
||||
"])\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "7dae353a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Compile the model\n",
|
||||
"# adam = specifies the optimizer to use during training\n",
|
||||
"# loss function to use during training, SparseCategoricalCrossentropy loss\n",
|
||||
"# is commonly used for multi-class classification problems.\n",
|
||||
"# from_logits=True indicates that the model's output is a raw score\n",
|
||||
"# for each class and not a probability distribution.\n",
|
||||
"model.compile(optimizer='adam',\n",
|
||||
" loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n",
|
||||
" metrics=['accuracy'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "bd237a4b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Train the model\n",
|
||||
"history = model.fit(x_train, y_train, epochs=10, validation_split=0.2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "169fc8c4",
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Evaluate the model on the test set\n",
|
||||
"test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)\n",
|
||||
"print(\"Test accuracy:\", test_acc)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "7f2e657a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Plot the training and validation accuracy and loss over time\n",
|
||||
"plt.figure(figsize=(10, 4))\n",
|
||||
"plt.subplot(1, 2, 1)\n",
|
||||
"plt.plot(history.history[\"accuracy\"])\n",
|
||||
"plt.plot(history.history[\"val_accuracy\"])\n",
|
||||
"plt.title(\"Model accuracy\")\n",
|
||||
"plt.ylabel(\"Accuracy\")\n",
|
||||
"plt.xlabel(\"Epoch\")\n",
|
||||
"plt.legend([\"Train\", \"Validation\"], loc=\"lower right\")\n",
|
||||
"\n",
|
||||
"plt.subplot(1, 2, 2)\n",
|
||||
"plt.plot(history.history[\"loss\"])\n",
|
||||
"plt.plot(history.history[\"val_loss\"])\n",
|
||||
"plt.title(\"Model loss\")\n",
|
||||
"plt.ylabel(\"Loss\")\n",
|
||||
"plt.xlabel(\"Epoch\")\n",
|
||||
"plt.legend([\"Train\", \"Validation\"], loc=\"upper right\")\n",
|
||||
"\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "298ac3bc",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Plot a confusion matrix of the test set predictions\n",
|
||||
"test_preds = np.argmax(model.predict(x_test), axis=1)\n",
|
||||
"conf_mat = tf.math.confusion_matrix(y_test, test_preds)\n",
|
||||
"plt.imshow(conf_mat, cmap=\"Blues\")\n",
|
||||
"plt.xlabel(\"Predicted labels\")\n",
|
||||
"plt.ylabel(\"True labels\")\n",
|
||||
"plt.xticks(np.arange(10))\n",
|
||||
"plt.yticks(np.arange(10))\n",
|
||||
"plt.colorbar()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9a0355ec",
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Make predictions on the test set\n",
|
||||
"y_pred = model.predict(x_test)\n",
|
||||
"y_pred = np.argmax(y_pred, axis=1)\n",
|
||||
"\n",
|
||||
"# Plot some examples from the test set and their predictions\n",
|
||||
"fig, axes = plt.subplots(4, 4, figsize=(14, 14))\n",
|
||||
"for i, ax in enumerate(axes.ravel()):\n",
|
||||
" ax.matshow(x_test[i].reshape(28, 28), cmap='gray')\n",
|
||||
" ax.set_title(\"True: %d\\nPredict: %d\" % (np.argmax(y_test[i]), y_pred[i]))\n",
|
||||
" ax.axis(\"off\")\n",
|
||||
"\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "facda3d1",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.8.16"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
247
notebooks/03_ml_basics_tf_binary_classification_example.ipynb
Normal file
247
notebooks/03_ml_basics_tf_binary_classification_example.ipynb
Normal file
@ -0,0 +1,247 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "344b183c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#\n",
|
||||
"# train a simple TensorFlow model to perform binary classification on a generated\n",
|
||||
"# 2-dimensional dataset \n",
|
||||
"# 02/2023\n",
|
||||
"# "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "92c9d0a1",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import tensorflow as tf"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3814ea1d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Generate toy data\n",
|
||||
"np.random.seed(4321)\n",
|
||||
"n_samples = 1000"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "84d8bc5e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"machine learning algorithms need data close to 1 , this generated here, it is not needed to normalize data"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a1d437df",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class1_data = np.random.multivariate_normal([-1., -1.], [[1., 0.], [0., 1.]], n_samples)\n",
|
||||
"class2_data = np.random.multivariate_normal([1.0, 1.0], [[1., 0.], [0., 1.]], n_samples)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6bbccf56",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# the data is merged together and the toy labels are asigned as [1, 0] and [0,1]\n",
|
||||
"train_data = np.concatenate([class1_data, class2_data])\n",
|
||||
"toy_labels = np.zeros(train_data.shape)\n",
|
||||
"toy_labels[:n_samples, 0] = 1\n",
|
||||
"toy_labels[n_samples:, 1] = 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f8dd0511",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Plot the input data with points colored according to their labels\n",
|
||||
"plt.scatter(class1_data[:, 0], class1_data[:, 1], color='red')\n",
|
||||
"plt.scatter(class2_data[:, 0], class2_data[:, 1], color='blue')\n",
|
||||
"plt.title(\"Input data with points colored according to their labels\")\n",
|
||||
"plt.xlabel(\"Feature 1\")\n",
|
||||
"plt.ylabel(\"Feature 2\")\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "75fb1af9",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Build a model, the sequential model is a linear stack of pre-made layers. In a Dense layer all neueral network layer is connected with all other layers. Here we have 32 nodes with input_shape 2 with means 2 dimensional data. The activation is 'relu' (rectified linear unit)\n",
|
||||
"Softmax maps the output of a model to probability distributions of the 2 classes. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ad6dcef9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model = tf.keras.models.Sequential([\n",
|
||||
" tf.keras.layers.Dense(32, activation='relu', input_shape=(2,)),\n",
|
||||
" tf.keras.layers.Dense(32, activation='relu'),\n",
|
||||
" tf.keras.layers.Dense(2, activation='softmax')\n",
|
||||
"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a9c38493",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Adam optimizer is a gradient-based optimization algorithm for updating the weights in \n",
|
||||
"a neural network. The leanrning rate depends on the first and second moments of the \n",
|
||||
"gradients of the loss function with respect to the weights\n",
|
||||
"The loss function is BinaryCrossentropy since we have 2 classes of data\n",
|
||||
"The accuracy metric measures the percentage of instances where the model \n",
|
||||
"correctly predicted the class label and it can be computed as the number of correct\n",
|
||||
"predictions divided by the total number of instances in the test set."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "7f6dc4ef",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model.compile(optimizer='adam',\n",
|
||||
"# loss=tf.keras.losses.CategoricalCrossentropy(),\n",
|
||||
" loss=tf.keras.losses.BinaryCrossentropy(),\n",
|
||||
" metrics=['accuracy'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a6397c0f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"The object history contains loss and accuracy from the training process\n",
|
||||
"The model is trained by dividing the entire training data into smaller batches\n",
|
||||
"of a specified size, updating the model's parameters after each batch.\n",
|
||||
"The batch_size parameter determines the number of samples to be used in each batch. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b98e46ba",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"history = model.fit(train_data, toy_labels, epochs=20, batch_size=32, verbose=1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "806497d2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Plot loss and accuracy\n",
|
||||
"plt.figure(figsize=(12, 4))\n",
|
||||
"\n",
|
||||
"plt.subplot(1, 2, 1)\n",
|
||||
"plt.plot(history.history['loss'])\n",
|
||||
"plt.title('Loss')\n",
|
||||
"plt.xlabel('Epoch')\n",
|
||||
"\n",
|
||||
"plt.subplot(1, 2, 2)\n",
|
||||
"plt.plot(history.history['accuracy'])\n",
|
||||
"plt.title('Accuracy')\n",
|
||||
"plt.xlabel('Epoch')\n",
|
||||
"\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ca6da322",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Plot data points and decision boundary\n",
|
||||
"x_min, x_max = train_data[:, 0].min() - .5, train_data[:, 0].max() + .5\n",
|
||||
"y_min, y_max = train_data[:, 1].min() - .5, train_data[:, 1].max() + .5\n",
|
||||
"xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02), np.arange(y_min, y_max, 0.02))\n",
|
||||
"# creating a 2D grid from the arrays xx and yy which is the area of our inputs \n",
|
||||
"grid = np.c_[xx.ravel(), yy.ravel()]\n",
|
||||
"# get the predicted class probabilities for each data point in the grid.\n",
|
||||
"# The result Z is an array with shape (n_samples, n_classes) where n_samples\n",
|
||||
"# is the number of data points in the grid and n_classes is the number of\n",
|
||||
"# classes in the toy_labels. Z contains the predicted class probabilities\n",
|
||||
"# for each data point in the grid.\n",
|
||||
"Z = model.predict(grid)\n",
|
||||
"# The line Z = np.argmax(Z, axis=1) is used to convert the predicted probabilities\n",
|
||||
"# into class labels.\n",
|
||||
"Z = np.argmax(Z, axis=1)\n",
|
||||
"# reshaped Z variable is used to create the contour plot of the model's predictions \n",
|
||||
"# on the grid.\n",
|
||||
"Z = Z.reshape(xx.shape)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "54c02602",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.figure(figsize=(8, 8))\n",
|
||||
"plt.contourf(xx, yy, Z, cmap=plt.cm.RdBu, alpha=.8)\n",
|
||||
"plt.scatter(train_data[:, 0], train_data[:, 1], c=np.argmax(toy_labels, axis=1), cmap=plt.cm.RdBu)\n",
|
||||
"\n",
|
||||
"plt.show()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.8.16"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
240
notebooks/03_ml_basics_tf_mlp_mnist_digits.ipynb
Normal file
240
notebooks/03_ml_basics_tf_mlp_mnist_digits.ipynb
Normal file
@ -0,0 +1,240 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "13fa2f64",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# In this example, we used the TensorFlow library to load the MNIST data,\n",
|
||||
"# define an MLP model with three dense layers, compile the model, train it\n",
|
||||
"# for 10 epochs, evaluate it on the test set, and make predictions on\n",
|
||||
"# the test set. Finally, we plot some examples of the predictions made\n",
|
||||
"# by the model."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1c4405f8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"from tensorflow import keras\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1e7e58fb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Normalize the pixel values to be between 0 and 1\n",
|
||||
"x_train = x_train / 255\n",
|
||||
"x_test = x_test / 255"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3d8a7370",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Flatten the 2D images into 1D arrays\n",
|
||||
"x_train = x_train.reshape(x_train.shape[0], -1)\n",
|
||||
"x_test = x_test.reshape(x_test.shape[0], -1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c2df0f54",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Convert the labels into one-hot encoded arrays\n",
|
||||
"y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)\n",
|
||||
"y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e2b249ea",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Define the model\n",
|
||||
"# The number of parameters depends on the shapes and sizes of the layers.\n",
|
||||
"# In the given model, the first layer Dense(512, activation='relu',\n",
|
||||
"# input_shape=(784,)) has 784 input nodes and 512 output nodes. Therefore,\n",
|
||||
"# the number of parameters in this layer would be (784 * 512) + 512 = 401920,\n",
|
||||
"# where the +512 term is for the bias terms.\n",
|
||||
"# The second layer also has 512 input nodes and 512 output nodes, which makes\n",
|
||||
"# 512512 = 262,144 parameters. The third and last layer has 512 input nodes\n",
|
||||
"# and 10 output nodes, which makes 512*10 = 5,120 parameters.\n",
|
||||
"model = tf.keras.models.Sequential()\n",
|
||||
"model.add(tf.keras.layers.Dense(512, activation='relu', input_shape=(784,)))\n",
|
||||
"model.add(tf.keras.layers.Dense(512, activation='relu'))\n",
|
||||
"model.add(tf.keras.layers.Dense(10, activation='softmax'))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "bab8730a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# over come overfitting by regularization\n",
|
||||
"#model = tf.keras.models.Sequential([\n",
|
||||
"# tf.keras.layers.Dense(64, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001),input_shape=(784,)),\n",
|
||||
"# tf.keras.layers.Dense(64, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.001)),\n",
|
||||
"# tf.keras.layers.Dense(10, activation='softmax')\n",
|
||||
"#])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e3223c61",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Compile the model\n",
|
||||
"model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "51e7758f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Train the model and record the history\n",
|
||||
"history = model.fit(x_train, y_train, epochs=10, batch_size=64, validation_data=(x_test, y_test))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e3240069",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Get the weights of the Dense layer\n",
|
||||
"# plot the weights as a heatmap or image, where the weights are represented\n",
|
||||
"# as pixel values.\n",
|
||||
"# model.layers[2].get_weights()[0] returns only the weights of the third\n",
|
||||
"# layer. If you wanted to get the biases, you would use\n",
|
||||
"# model.layers[2].get_weights()[1].\n",
|
||||
"dense_weights = model.layers[2].get_weights()[0]\n",
|
||||
"\n",
|
||||
"# Plot the weights as a heatmap\n",
|
||||
"plt.imshow(dense_weights, cmap='coolwarm')\n",
|
||||
"plt.colorbar()\n",
|
||||
"plt.title('weights in the output layer')\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "13118686",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Evaluate the model on the test set\n",
|
||||
"test_loss, test_acc = model.evaluate(x_test, y_test)\n",
|
||||
"print('Test accuracy:', test_acc)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "40f35fe5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Plot loss and accuracy\n",
|
||||
"plt.figure(figsize=(12, 4))\n",
|
||||
"\n",
|
||||
"# Plot the loss and accuracy for training and validation data\n",
|
||||
"plt.subplot(1, 2, 1)\n",
|
||||
"plt.plot(history.history['loss'], label='training loss')\n",
|
||||
"plt.plot(history.history['val_loss'], label='validation loss')\n",
|
||||
"plt.xlabel('Epoch')\n",
|
||||
"plt.ylabel('Loss')\n",
|
||||
"plt.legend()\n",
|
||||
"\n",
|
||||
"plt.subplot(1, 2, 2)\n",
|
||||
"plt.plot(history.history['accuracy'])\n",
|
||||
"plt.plot(history.history['val_accuracy'])\n",
|
||||
"plt.xlabel('Epoch')\n",
|
||||
"plt.ylabel('Accuracy')\n",
|
||||
"plt.legend()\n",
|
||||
"\n",
|
||||
"plt.show()\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6176ce1e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Make predictions on the test set\n",
|
||||
"y_pred = model.predict(x_test)\n",
|
||||
"y_pred = np.argmax(y_pred, axis=1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3635ded5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Plot some examples from the test set and their predictions\n",
|
||||
"fig, axes = plt.subplots(4, 4, figsize=(14, 14))\n",
|
||||
"for i, ax in enumerate(axes.ravel()):\n",
|
||||
" ax.matshow(x_test[i].reshape(28, 28), cmap='gray')\n",
|
||||
" ax.set_title(\"True: %d\\nPredict: %d\" % (np.argmax(y_test[i]), y_pred[i]))\n",
|
||||
" ax.axis(\"off\")\n",
|
||||
"\n",
|
||||
"plt.show()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.8.16"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user