{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Réseaux de neurones avec keras" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using TensorFlow backend.\n" ] } ], "source": [ "import tensorflow as tf\n", "import keras\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(60000, 28, 28)\n", "(10000, 28, 28)\n" ] } ], "source": [ "from keras.datasets import mnist\n", "#load (first download if necessary) the MNIST dataset\n", "# (the dataset is stored in your home direcoty in ~/.keras/datasets/mnist.npz\n", "# and will take ~11MB)\n", "# data is already split in train and test datasets\n", "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", "\n", "print(x_train.shape)\n", "print(x_test.shape)\n", "\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from __future__ import print_function\n", "\n", "\n", "\n", "#print('tensorflow:', tf.__version__)\n", "#print('keras:', keras.__version__)\n", "\n", "\n", "\n", "# x_train : 60000 images of size 28x28, i.e., x_train.shape = (60000, 28, 28)\n", "# y_train : 60000 labels (from 0 to 9)\n", "# x_test : 10000 images of size 28x28, i.e., x_test.shape = (10000, 28, 28)\n", "# x_test : 10000 labels\n", "# all datasets are of type uint8\n", "\n", "#To input our values in our network Dense layer, we need to flatten the datasets, i.e.,\n", "# pass from (60000, 28, 28) to (60000, 784)\n", "#flatten images\n", "num_pixels = x_train.shape[1] * x_train.shape[2]\n", "x_train = x_train.reshape(x_train.shape[0], num_pixels)\n", "x_test = x_test.reshape(x_test.shape[0], num_pixels)\n", "\n", "#Convert to float\n", "x_train = x_train.astype('float32')\n", "x_test = x_test.astype('float32')\n", "\n", "#Normalize inputs from [0; 255] to [0; 1]\n", "x_train = x_train / 255\n", "x_test = x_test / 255\n", "\n", "\n", "#Convert class vectors to binary class matrices (\"one hot encoding\")\n", "## Doc : https://keras.io/utils/#to_categorical\n", "y_train = keras.utils.to_categorical(y_train)\n", "y_test = keras.utils.to_categorical(y_test)\n", "\n", "\n", "num_classes = y_train.shape[1]\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }