This commit is contained in:
Joerg Marks 2023-03-31 17:34:03 +02:00
parent 9b5bd17fde
commit 2cb145e74c
4 changed files with 641 additions and 0 deletions

View 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
}

View 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
}

View 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.