Skip to content

Instantly share code, notes, and snippets.

@fedxa
Created May 11, 2020 23:25
Show Gist options
  • Save fedxa/45eb1a412964ddf19820fff347c5b2de to your computer and use it in GitHub Desktop.
Save fedxa/45eb1a412964ddf19820fff347c5b2de to your computer and use it in GitHub Desktop.
keras-tensorflow-problem.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "keras-tensorflow-problem.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyMScnaczMGn1SWIP3CXA3VI",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/fedxa/45eb1a412964ddf19820fff347c5b2de/untitled2.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "eyeh5WbCUMtw",
"colab_type": "code",
"colab": {}
},
"source": [
"# from keras.layers import Input, Dense\n",
"# from keras.models import Model\n",
"# from keras import regularizers\n",
"from tensorflow.keras.layers import Input, Dense\n",
"from tensorflow.keras.models import Model\n",
"from tensorflow.keras import regularizers"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "2e-VxnQgUOT4",
"colab_type": "code",
"colab": {}
},
"source": [
"encoding_dim = 32 # 32 floats -> compression of factor 24.5, assuming the input is 784 floats\n",
"\n",
"input_img = Input(shape=(784,))\n",
"# \"encoded\" is the encoded representation of the input\n",
"encoded = Dense(encoding_dim, activation='relu')(input_img)\n",
"# \"decoded\" is the lossy reconstruction of the input\n",
"decoded = Dense(784, activation='sigmoid')(encoded)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "VX2PK92LUUVU",
"colab_type": "code",
"colab": {}
},
"source": [
"# this model maps an input to its reconstruction\n",
"autoencoder = Model(input_img, decoded)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "-jTy_pn4UW21",
"colab_type": "code",
"colab": {}
},
"source": [
"# this model maps an input to its encoded representation\n",
"encoder = Model(input_img, encoded)\n",
"\n",
"# create a placeholder for an encoded (32-dimensional) input\n",
"encoded_input = Input(shape=(encoding_dim,))\n",
"# retrieve the last layer of the autoencoder model\n",
"decoder_layer = autoencoder.layers[-1]\n",
"# create the decoder model\n",
"decoder = Model(encoded_input, decoder_layer(encoded_input))"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "JvgM4lrqUaHz",
"colab_type": "code",
"colab": {}
},
"source": [
"autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "hGB1kjdsUc36",
"colab_type": "code",
"outputId": "8bf4b87a-efcc-494f-f7da-0c6d1b823a5b",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 104
}
},
"source": [
"from keras.datasets import mnist\n",
"import numpy as np\n",
"(x_train, _), (x_test, _) = mnist.load_data()\n",
"\n",
"x_train = x_train.astype('float32') / 255.\n",
"x_test = x_test.astype('float32') / 255.\n",
"x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))\n",
"x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))\n",
"print(x_train.shape)\n",
"print(x_test.shape)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz\n",
"\r 8192/11490434 [..............................] - ETA: 0s"
],
"name": "stdout"
},
{
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
],
"name": "stderr"
},
{
"output_type": "stream",
"text": [
"11493376/11490434 [==============================] - 0s 0us/step\n",
"(60000, 784)\n",
"(10000, 784)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "KPJYHADGUe1m",
"colab_type": "code",
"outputId": "20939fb0-a4b5-4266-a351-15c9251606e0",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 434
}
},
"source": [
"autoencoder.fit(x_train, x_train,\n",
" epochs=20,\n",
" batch_size=256,\n",
" shuffle=True,\n",
" validation_data=(x_test, x_test))"
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"Epoch 1/20\n",
"235/235 [==============================] - 2s 10ms/step - loss: 0.6950 - val_loss: 0.6950\n",
"Epoch 2/20\n",
"235/235 [==============================] - 2s 10ms/step - loss: 0.6949 - val_loss: 0.6948\n",
"Epoch 3/20\n",
"235/235 [==============================] - 2s 10ms/step - loss: 0.6947 - val_loss: 0.6946\n",
"Epoch 4/20\n",
"235/235 [==============================] - 2s 10ms/step - loss: 0.6945 - val_loss: 0.6944\n",
"Epoch 5/20\n",
"235/235 [==============================] - 2s 10ms/step - loss: 0.6943 - val_loss: 0.6943\n",
"Epoch 6/20\n",
"235/235 [==============================] - 2s 10ms/step - loss: 0.6942 - val_loss: 0.6941\n",
"Epoch 7/20\n",
"235/235 [==============================] - 2s 10ms/step - loss: 0.6940 - val_loss: 0.6939\n",
"Epoch 8/20\n",
"235/235 [==============================] - 2s 10ms/step - loss: 0.6938 - val_loss: 0.6938\n",
"Epoch 9/20\n",
"235/235 [==============================] - 2s 10ms/step - loss: 0.6937 - val_loss: 0.6936\n",
"Epoch 10/20\n",
"235/235 [==============================] - 2s 10ms/step - loss: 0.6935 - val_loss: 0.6934\n",
"Epoch 11/20\n",
"235/235 [==============================] - 2s 10ms/step - loss: 0.6934 - val_loss: 0.6933\n",
"Epoch 12/20\n",
" 13/235 [>.............................] - ETA: 1s - loss: 0.6933"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "JaVPCf9NUhMb",
"colab_type": "code",
"colab": {}
},
"source": [
"# encode and decode some digits\n",
"# note that we take them from the *test* set\n",
"encoded_imgs = encoder.predict(x_test)\n",
"decoded_imgs = decoder.predict(encoded_imgs)\n",
"\n",
"# use Matplotlib (don't ask)\n",
"import matplotlib.pyplot as plt\n",
"\n",
"n = 10 # how many digits we will display\n",
"plt.figure(figsize=(20, 4))\n",
"for i in range(n):\n",
" # display original\n",
" ax = plt.subplot(2, n, i + 1)\n",
" plt.imshow(x_test[i].reshape(28, 28))\n",
" plt.gray()\n",
" ax.get_xaxis().set_visible(False)\n",
" ax.get_yaxis().set_visible(False)\n",
"\n",
" # display reconstruction\n",
" ax = plt.subplot(2, n, i + 1 + n)\n",
" plt.imshow(decoded_imgs[i].reshape(28, 28))\n",
" plt.gray()\n",
" ax.get_xaxis().set_visible(False)\n",
" ax.get_yaxis().set_visible(False)\n",
"plt.show()"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "DP8VznpQUklv",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment