SHARE
TWEET

Untitled

a guest Jan 21st, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.     "nbformat_minor": 1,
  3.     "cells": [
  4.         {
  5.             "execution_count": 3,
  6.             "cell_type": "code",
  7.             "metadata": {},
  8.             "outputs": [
  9.                 {
  10.                     "output_type": "stream",
  11.                     "name": "stdout",
  12.                     "text": "Extracting MNIST_data/train-images-idx3-ubyte.gz\nExtracting MNIST_data/train-labels-idx1-ubyte.gz\nExtracting MNIST_data/t10k-images-idx3-ubyte.gz\nExtracting MNIST_data/t10k-labels-idx1-ubyte.gz\n"
  13.                 }
  14.             ],
  15.             "source": "from tensorflow.examples.tutorials.mnist import input_data\nmnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)"
  16.         },
  17.         {
  18.             "execution_count": 12,
  19.             "cell_type": "code",
  20.             "metadata": {},
  21.             "outputs": [],
  22.             "source": "import tensorflow as tf"
  23.         },
  24.         {
  25.             "execution_count": 5,
  26.             "cell_type": "code",
  27.             "metadata": {},
  28.             "outputs": [],
  29.             "source": "%matplotlib inline\nimport matplotlib.pyplot as plt"
  30.         },
  31.         {
  32.             "execution_count": 26,
  33.             "cell_type": "code",
  34.             "metadata": {},
  35.             "outputs": [
  36.                 {
  37.                     "output_type": "stream",
  38.                     "name": "stdout",
  39.                     "text": "[[ 0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]]\n"
  40.                 },
  41.                 {
  42.                     "execution_count": 26,
  43.                     "metadata": {},
  44.                     "data": {
  45.                         "text/plain": "<matplotlib.image.AxesImage at 0x7f3252144fd0>"
  46.                     },
  47.                     "output_type": "execute_result"
  48.                 },
  49.                 {
  50.                     "output_type": "display_data",
  51.                     "data": {
  52.                         "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJ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Cf6GkP0+5f1DdNeW3S9phZi+bWX/TzUxj4eTMSNnPCxru51S5MzfX6ZSZpbvmtetkxuuyNRH+6WYT6aYhh0+5+39KukXS17K3t2jPDyUt1cQ0boclfa/JZrKZpYckfd3d/9pkL1NN01cjr1sT4T8o6aIp9z8q6VADfUzL3Q9lP49K2qKJjynd5MjkJKnZz6MN9/NP7n7E3U+6+7ikH6nB1y6bWXpI0k/dfXP2cOOv3XR9NfW6NRH+lyRdbGYfM7MPSfqSpK0N9PEBZnZW9kWMzOwsSZ9V980+vFXSmuz2GknPNtjLv+mWmZtbzSythl+7bpvxupGDfLKhjHWSzpC0wd2/U3sT0zCzj2tiby9NnPH4syZ7M7OnJd2gibO+jkj6lqRfSfqlpEWSDkj6orvX/sVbi95u0Axnbq6ot1YzS7+oBl+7Mme8LqUfjvADYuIIPyAowg8ERfiBoAg/EBThB4Ii/EBQhB8IivADQf0DnDeb/EWld88AAAAASUVORK5CYII=\n",
  53.                         "text/plain": "<matplotlib.figure.Figure at 0x7f325219c128>"
  54.                     },
  55.                     "metadata": {}
  56.                 }
  57.             ],
  58.             "source": "batch_xs, batch_ys = mnist.train.next_batch(1)\nX = batch_xs\nX = X.reshape([28,28])\nplt.gray()\nprint(batch_ys)\nplt.imshow(X)"
  59.         },
  60.         {
  61.             "execution_count": 31,
  62.             "cell_type": "code",
  63.             "metadata": {},
  64.             "outputs": [],
  65.             "source": "x = tf.placeholder(tf.float32, [None, 784]) # add data during training\nW = tf.Variable(tf.zeros([784,10])) # Tensorflow variable to hold training data, one for each softmax regression model\nb = tf.Variable(tf.zeros([10])) # variable to hold bias, one for each regression model\ny = tf.nn.softmax(tf.matmul(x,W) + b) # create a model (no computation is happening at this stage), launching nodes together to form a computational graph"
  66.         },
  67.         {
  68.             "execution_count": 32,
  69.             "cell_type": "code",
  70.             "metadata": {},
  71.             "outputs": [],
  72.             "source": "y_ = tf.placeholder(tf.float32, [None,10]) #variable for hot-encoded vector"
  73.         },
  74.         {
  75.             "execution_count": 38,
  76.             "cell_type": "code",
  77.             "metadata": {},
  78.             "outputs": [],
  79.             "source": "cross_entropy = tf.reduce_mean( -tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))   # cost function in a form of cross-entropy\n\ntrain_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)"
  80.         },
  81.         {
  82.             "execution_count": 40,
  83.             "cell_type": "code",
  84.             "metadata": {},
  85.             "outputs": [],
  86.             "source": "sess = tf.InteractiveSession()"
  87.         },
  88.         {
  89.             "execution_count": 42,
  90.             "cell_type": "code",
  91.             "metadata": {},
  92.             "outputs": [],
  93.             "source": "tf.global_variables_initializer().run()"
  94.         },
  95.         {
  96.             "execution_count": 46,
  97.             "cell_type": "code",
  98.             "metadata": {},
  99.             "outputs": [],
  100.             "source": "for _ in range(1000):\n    batch_xs, batch_ys = mnist.train.next_batch(100)\n    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})"
  101.         },
  102.         {
  103.             "execution_count": 48,
  104.             "cell_type": "code",
  105.             "metadata": {},
  106.             "outputs": [],
  107.             "source": "correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))"
  108.         },
  109.         {
  110.             "execution_count": 50,
  111.             "cell_type": "code",
  112.             "metadata": {},
  113.             "outputs": [],
  114.             "source": "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))"
  115.         },
  116.         {
  117.             "execution_count": 53,
  118.             "cell_type": "code",
  119.             "metadata": {},
  120.             "outputs": [
  121.                 {
  122.                     "output_type": "stream",
  123.                     "name": "stdout",
  124.                     "text": "0.919\n"
  125.                 }
  126.             ],
  127.             "source": "print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))"
  128.         }
  129.     ],
  130.     "metadata": {
  131.         "kernelspec": {
  132.             "display_name": "Python 3.5 with Spark 2.1",
  133.             "name": "python3-spark21",
  134.             "language": "python"
  135.         },
  136.         "language_info": {
  137.             "mimetype": "text/x-python",
  138.             "nbconvert_exporter": "python",
  139.             "version": "3.5.4",
  140.             "name": "python",
  141.             "file_extension": ".py",
  142.             "pygments_lexer": "ipython3",
  143.             "codemirror_mode": {
  144.                 "version": 3,
  145.                 "name": "ipython"
  146.             }
  147.         }
  148.     },
  149.     "nbformat": 4
  150. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top