Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# TensorFlow and tf.keras\n",
- "import tensorflow as tf\n",
- "\n",
- "# Helper libraries\n",
- "import numpy as np\n",
- "import matplotlib.pyplot as plt\n",
- "\n",
- "print(tf.__version__)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "29515/29515 [==============================] - 0s 2us/step\n",
- "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz\n",
- "26421880/26421880 [==============================] - 45s 2us/step\n",
- "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz\n",
- "5148/5148 [==============================] - 0s 0s/step\n",
- "Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz\n",
- "4422102/4422102 [==============================] - 6s 1us/step\n"
- ]
- }
- ],
- "source": [
- "fashion_mnist = tf.keras.datasets.fashion_mnist\n",
- "\n",
- "(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',\n",
- " 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "train_images.shape"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "len(train_labels)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "test_labels"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "test_images.shape"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "len(test_labels)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "plt.figure()\n",
- "plt.imshow(train_images[0])\n",
- "plt.colorbar()\n",
- "plt.grid(False)\n",
- "plt.show()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "train_images = train_images / 255.0\n",
- "\n",
- "test_images = test_images / 255.0"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "\n",
- "plt.figure(figsize=(10,10))\n",
- "for i in range(5):\n",
- " plt.subplot(5,5,i+1)\n",
- " plt.xticks([])\n",
- " plt.yticks([])\n",
- " plt.grid(False)\n",
- " plt.imshow(train_images[i], cmap=plt.cm.binary)\n",
- " plt.xlabel(class_names[train_labels[i]])\n",
- " plt.show()\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "import tensorflow as tf\n",
- "from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "model = tf.keras.Sequential([\n",
- " Conv2D(64, (3,3), activation='relu', input_shape=(28, 28, 1)),\n",
- " MaxPooling2D((2, 2)),\n",
- " Conv2D(128, (3,3), activation='relu', padding=\"same\"),\n",
- " MaxPooling2D((2, 2)),\n",
- " Flatten(),\n",
- " Dense(256, activation='relu'),\n",
- " Dropout(0.2),\n",
- " Dense(10, activation='softmax')\n",
- "])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "model.compile(optimizer='adam',\n",
- " loss='sparse_categorical_crossentropy',\n",
- " metrics=['accuracy'])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "model.fit(train_images, train_labels, epochs=10)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "model.evaluate(test_images, test_labels)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "probability_model = tf.keras.Sequential([model,\n",
- "tf.keras.layers.Softmax()])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "predictions = probability_model.predict(test_images)\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "predictions[0]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "def plot_image(i, predictions_array, true_label, img):\n",
- " true_label, img = true_label[i], img[i]\n",
- " plt.grid(False)\n",
- " plt.xticks([])\n",
- " plt.yticks([])\n",
- " plt.imshow(img, cmap=plt.cm.binary)\n",
- " predicted_label = np.argmax(predictions_array)\n",
- " if predicted_label == true_label:\n",
- " color = 'blue'\n",
- " else:\n",
- " color = 'red'\n",
- " plt.xlabel(\"{} {:2.0f}% ({})\".format(class_names[predicted_label],\n",
- " 100*np.max(predictions_array),\n",
- " class_names[true_label]),\n",
- " color=color)\n",
- "\n",
- "def plot_value_array(i, predictions_array, true_label):\n",
- " true_label = true_label[i]\n",
- " plt.grid(False)\n",
- " plt.xticks(range(10))\n",
- " plt.yticks([])\n",
- " thisplot = plt.bar(range(10), predictions_array, color=\"#777777\")\n",
- " plt.ylim([0, 1])\n",
- " predicted_label = np.argmax(predictions_array)\n",
- " thisplot[predicted_label].set_color('red')\n",
- " thisplot[true_label].set_color('blue')\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "i = 3\n",
- "plt.figure(figsize=(6,3))\n",
- "plt.subplot(1,2,1)\n",
- "plot_image(i, predictions[i], test_labels, test_images)\n",
- "plt.subplot(1,2,2)\n",
- "plot_value_array(i, predictions[i], test_labels)\n",
- "plt.show()\n"
- ]
- }
- ],
- "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.11.8"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 4
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement