0% found this document useful (0 votes)
102 views3 pages

Mnist

This document contains code to build and train a simple neural network model for classifying MNIST handwritten digits. The model takes in images, runs them through a single hidden layer, and outputs predictions for each of the 10 digit classes. It trains the model for 1000 iterations on batches of images and labels, then evaluates accuracy on test data, achieving around 91% accuracy.

Uploaded by

zeroneptunes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views3 pages

Mnist

This document contains code to build and train a simple neural network model for classifying MNIST handwritten digits. The model takes in images, runs them through a single hidden layer, and outputs predictions for each of the 10 digit classes. It trains the model for 1000 iterations on batches of images and labels, then evaluates accuracy on test data, achieving around 91% accuracy.

Uploaded by

zeroneptunes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

{

"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.\n",
"Extracting /tmp/tensorflow/mnist/input_data\\train-images-idx3-ubyte.gz\n",
"Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.\n",
"Extracting /tmp/tensorflow/mnist/input_data\\train-labels-idx1-ubyte.gz\n",
"Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.\n",
"Extracting /tmp/tensorflow/mnist/input_data\\t10k-images-idx3-ubyte.gz\n",
"Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.\n",
"Extracting /tmp/tensorflow/mnist/input_data\\t10k-labels-idx1-ubyte.gz\n",
"WARNING:tensorflow:From <ipython-input-1-d9c5a814a848>:58:
softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated
and will be removed in a future version.\n",
"Instructions for updating:\n",
"\n",
"Future major versions of TensorFlow will allow gradients to flow\n",
"into the labels input on backprop by default.\n",
"\n",
"See tf.nn.softmax_cross_entropy_with_logits_v2.\n",
"\n",
"0.9143\n"
]
},
{
"ename": "SystemExit",
"evalue": "",
"output_type": "error",
"traceback": [
"An exception has occurred, use %tb to see the full traceback.\n",
"\u001b[1;31mSystemExit\u001b[0m\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\Athena\\Anaconda3\\envs\\tensorflow15\\lib\\site-
packages\\IPython\\core\\interactiveshell.py:2971: UserWarning: To exit: use
'exit', 'quit', or Ctrl-D.\n",
" warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n"
]
}
],
"source": [
"# Copyright 2015 The TensorFlow Authors. All Rights Reserved.\n",
"#\n",
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# http://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License.\n",
"#
==============================================================================\n",
"\n",
"\"\"\"A very simple MNIST classifier.\n",
"\n",
"See extensive documentation at\n",
"http://tensorflow.org/tutorials/mnist/beginners/index.md\n",
"\"\"\"\n",
"from __future__ import absolute_import\n",
"from __future__ import division\n",
"from __future__ import print_function\n",
"\n",
"import argparse\n",
"import sys\n",
"\n",
"from tensorflow.examples.tutorials.mnist import input_data\n",
"\n",
"import tensorflow as tf\n",
"\n",
"FLAGS = None\n",
"\n",
"\n",
"def main(_):\n",
" # Import data\n",
" mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)\n",
"\n",
" # Create the model\n",
" x = tf.placeholder(tf.float32, [None, 784])\n",
" W = tf.Variable(tf.zeros([784, 10]))\n",
" b = tf.Variable(tf.zeros([10]))\n",
" y = tf.matmul(x, W) + b\n",
"\n",
" # Define loss and optimizer\n",
" y_ = tf.placeholder(tf.float32, [None, 10])\n",
"\n",
" # The raw formulation of cross-entropy,\n",
" #\n",
" # tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)),\n",
" # reduction_indices=[1]))\n",
" #\n",
" # can be numerically unstable.\n",
" #\n",
" # So here we use tf.nn.softmax_cross_entropy_with_logits on the raw\n",
" # outputs of 'y', and then average across the batch.\n",
" cross_entropy = tf.reduce_mean(\n",
" tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))\n",
" train_step =
tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)\n",
"\n",
" sess = tf.InteractiveSession()\n",
" tf.global_variables_initializer().run()\n",
" # Train\n",
" 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})\n",
"\n",
" # Test trained model\n",
" correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n",
" accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
" print(sess.run(accuracy, feed_dict={x: mnist.test.images,\n",
" y_: mnist.test.labels}))\n",
"\n",
"if __name__ == '__main__':\n",
" parser = argparse.ArgumentParser()\n",
" parser.add_argument('--data_dir', type=str,
default='/tmp/tensorflow/mnist/input_data',\n",
" help='Directory for storing input data')\n",
" FLAGS, unparsed = parser.parse_known_args()\n",
" tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:tensorflow15]",
"language": "python",
"name": "conda-env-tensorflow15-py"
},
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

You might also like