TensorFlow Study Notes (1) MNIST for Beginners

Source: Internet
Author: User

The first part of the tutorial is mainly about the code inside the mnist_softmax.py:

The first is to download and read the mnist data set, two lines of code to achieve:

From tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets ("mnist_data/", One_ Hot=true)
a one-hot vector is a vector which are 0 in the most dimensions, and 1 in asingle dimension.

The first line is the code that imports the Input_data module, the module:

Import TensorFlow as TF from
tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets

Found that there is no direct input_data.read_data_sets () function, but also imported another module, the module called Read_data_sets, find this file, the code is:

def read_data_sets (Train_dir, Fake_data=false, One_hot=false, dty Pe=dtypes.float32, Reshape=true, validation_size=5000): if Fake_data:def Fak E (): Return DataSet ([], [], Fake_data=true, One_hot=one_hot, dtype=dtype) train = fake () validation = fake ( ) test = fake () return base. Datasets (Train=train, Validation=validation, test=test) train_images = ' train-images-idx3-ubyte.gz ' TRAIN_LABELS = ' t rain-labels-idx1-ubyte.gz ' test_images = ' t10k-images-idx3-ubyte.gz ' test_labels = ' t10k-labels-idx1-ubyte.gz ' Loca  L_file = Base.maybe_download (Train_images, Train_dir, Source_url + train_images) with Open (Local_file, ' RB ') as F:train_images = Extract_images (f) local_file = Base.maybe_download (Train_labels, train _dir, Source_url + train_labels) with open (Local_file, ' RB ') as F:trAin_labels = Extract_labels (f, one_hot=one_hot) Local_file = Base.maybe_download (Test_images, Train_dir,

  Source_url + test_images) with open (Local_file, ' RB ') as F:test_images = Extract_images (f) Local_file = Base.maybe_download (Test_labels, Train_dir, Source_url + test_labels) wi Th open (local_file, ' RB ') as F:test_labels = Extract_labels (f, one_hot=one_hot) if not 0 <= validation_size &lt ; = Len (train_images): Raise ValueError (' Validation size should be between 0 and {}.
        Received: {}. ' . Format (len (train_images), validation_size)) Validation_images = train_images[:validation_size] Validation_labels = t Rain_labels[:validation_size] Train_images = train_images[validation_size:] Train_labels = Train_labels[validation_ Size:] train = DataSet (Train_images, Train_labels, Dtype=dtype, reshape=reshape) validation = DataSet (Validation_imag ES, VALidation_labels, Dtype=dtype, reshape=reshape) test = DataSet (Test_images, Test_labels, Dtype=dtype, Reshape=reshape) return base.
 Datasets (Train=train, validation=validation, Test=test)
This code is primarily to prepare the training set, test set, and validation set. The previous part of the training set that was downloaded was turned into a prepared training set, and the latter part became the validation set.

and turn this 28*28 image into a vector of 1*784.

The final result is to turn 55000 images into a tensor tensor [55000,784]. Each image corresponds to a one-hotx vector 1*10, so label becomes tensor[55000,10]

Next is Softmax regressions Introduction:

In fact, the solution is 10 classification of the problem, mainly divided into two steps: The first is to derive the input belongs to a class of evidence, and then, the evidence into probability.

Here the evidence is actually similar to the weighted sum:


Then you get the probability value y:

y = Softmax (evidence), the following is the workflow:


The probability value is then obtained by Softmax ():

In the form of a matrix:

The final concise representation is:

y = Softmax (wx+b)

Complete regression with TensorFlow (implementing the regression)

First Import TensorFlow

Import TensorFlow as TF

Create X to hold picture data

x = Tf.placeholder (Tf.float32, [None, 784])

Then create two variables to hold the weight parameter and the offset parameter

W = tf. Variable (Tf.zeros ([784, ten]))
B = tf. Variable (Tf.zeros ([10]))

Then use a single line of code to complete the classification task.

y = Tf.nn.softmax (Tf.matmul (x, W) + b)

Training (Training):

The process of training is essentially the process of making the loss smaller, if the loss is close to 0, it shows that the training effect is good.

Cross-entropy is one of the methods to define the loss model.

Before using cross-entropy, you need to import the truth (Gold Standard) (the label) in:

Y_ = Tf.placeholder (Tf.float32, [None, 10])
So:

Cross_entropy = Tf.reduce_mean (-tf.reduce_sum (Y_ * tf.log (y), reduction_indices=[1]))

After calculating the loss, it is necessary to use the loss reverse propagation, the method is gradient descent method. (There are many other optimization algorithms)

Train_step = Tf.train.GradientDescentOptimizer (0.5). Minimize (Cross_entropy)

Before training, you need to initialize all the variables,

init = Tf.initialize_all_variables ()

can now be initialized in the session layer

Sess = tf. Session ()
sess.run (init)

The Python code trained 1000 times is as follows:

For I in range (£):
  batch_xs, Batch_ys = Mnist.train.next_batch (+)
  sess.run (Train_step, feed_dict={x: Batch_xs, Y_: Batch_ys})

Finally, the test, the output accuracy:

Correct_prediction = Tf.equal (Tf.argmax (y,1), Tf.argmax (y_,1))
accuracy = Tf.reduce_mean (Tf.cast (correct_ Prediction, Tf.float32))
print (Sess.run (accuracy, feed_dict={x:mnist.test.images, Y_: Mnist.test.labels}))

Complete the task. I was running in the Ipython notebook example. Talk about some of the problems encountered in this process.

1, the beginning I use is the download tensorflow inside of the mnist_softmax.py inside of code, is really a pit, because there is no definition session session, so will error, according to the official tutorial to add the session inside.

2, again run, no error, but also no output results, very puzzled, and then I entered in the terminal: Python path (mnist_softmax.py path) unexpectedly has the result.

3, later found that because the code inside the definition of a main () function, so it will only compile, not run.

4, then I call this function in another cell, error. About the Input_data problem, found that because the location of the data can not find, so give it a location, run successfully, the following is attached to the complete code and results, (summary is running the program, do not use the downloaded code, the official tutorial step-by-step code to write)

From __future__ import Absolute_import to __future__ Import division from __future__ import print_function import ARGP
    Arse import TensorFlow as TF from tensorflow.examples.tutorials.mnist import Input_data def main (_): Print ("Test") Mnist = Input_data.read_data_sets (".. /.. /mnist_data/", one_hot = True) x = Tf.placeholder (tf.float32,[none,784]) W = tf. Variable (Tf.zeros ([784,10])) b = tf. Variable (tf.zeros) y = Tf.matmul (x,w) + b y_ = Tf.placeholder (tf.float32,[none,10]) cross_entropy = t F.reduce_mean (Tf.nn.softmax_cross_entropy_with_logits (y,y_)) Train_step = Tf.train.GradientDescentOptimizer (0.5). Minimize (cross_entropy) init = tf.initialize_all_variables () Sess = tf. Session () Sess.run (init) for I in range (£): Batch_xs, Batch_ys = Mnist.train.next_batch (+) Sess . Run (Train_step, Feed_dict={x:batch_xs, Y_: Batch_ys}) correct_prediction = Tf.equal (Tf.argmax (y,1), Tf.argmax (y_ , 1)) accuracy = tF.reduce_mean (Tf.cast (Correct_prediction,tf.float32)) print (Sess.run (accuracy, feed_dict={x:mnist.test.images, y_
 : Mnist.test.labels})) Main (_)

Test results:

Test
extracting. /.. /mnist_data/train-images-idx3-ubyte.gz
extracting. /.. /mnist_data/train-labels-idx1-ubyte.gz
extracting. /.. /mnist_data/t10k-images-idx3-ubyte.gz
extracting. /.. /mnist_data/t10k-labels-idx1-ubyte.gz
0.9142

The correct rate is 91%.

Note: Operating Environment: Ubuntu14.04+python2.7+ipython Notebook Environment

Reference: https://www.tensorflow.org/versions/r0.11/tutorials/mnist/beginners/index.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.