Recently in the study of Huang Wenjian TensorFlow Books, hope to do a summary of learning.
Softmax Regression Algorithm principle: When we predict a picture, we will calculate the probability of each number, such as 3 probability is the probability of 3%,5 is 6%,1 probability is 80%, then return 1.
TensorFlow version: 0.8.0
# import handwriting recognition data, TensorFlow provides the handwriting recognition library
fromTensorflow.examples.tutorials.mnistImportInput_data
# Read handwriting recognition data mnist= Input_data.read_data_sets ("mnist_data/", one_hot=True)
# The dimension of the training set data is (55000,784), the dimension of the Training set label is (55000,10)
# The dimension of the test set data is (10000,784), the dimension of the test set label is (10000,10)
# The dimension of the validation set data is (5000,784), the dimension of the validation set label is (5000,10)
# Why is the dimension of training data 784? Because the picture pixels of the dataset provided by TensorFlow are 28*28=784
# Why the dimension of the label is 10, the label is processed, and each expected result becomes a 10-dimensional data containing only 0 and 1.
# For example, label 5 is represented as [0,0,0,0,0,1,0,0,0,0], this method is called One-hot encoding
Print(Mnist.train.images.shape,mnist.train.labels.shape)Print(Mnist.test.images.shape,mnist.test.labels.shape)Print(Mnist.validation.images.shape,mnist.validation.labels.shape)
# import TensorFlow LibraryImportTensorFlow as TF
# Register the session as the default session, and the operation will run in the session. Placeholder is the place where the data is entered
# Placeholder's first parameter represents the data type, the 2nd parameter represents the dimension of the data, none represents any length of data sess=TF. InteractiveSession () x= Tf.placeholder (tf.float32,[none,784])
# variable is used to store parameters, it is persistent, can exist for a long time, each iteration will update the # data dimension is 784, the dimension of the category has been one-hot encoded into 10 dimensions, so the parameter of W is [784,10]
# b is the [10] dimension, W and B are all initialized to 0, the initial value of the simple model is not important
W= TF. Variable (Tf.zeros ([784,10])) b=TF. Variable (Tf.zeros ([10]) # Softmax function to define Softmax regression algorithm
# Matmul for vector multiplication
Y=tf.nn.softmax (Tf.matmul (x,w) +b)
# to find the loss function cross-entropy, first define a placeholder, enter the real label
# Cross_entropy defines the calculation method of the loss function, calculates the entropy by Reduce_sum and reduce_mean the average of the entropy of each batch Y_=tf.placeholder (tf.float32,[none,10]) cross_entropy= Tf.reduce_mean (-tf.reduce_sum (Y_ * tf.log (y), reduction_indices=[1]))
# define an optimizer, Gradientdescentoptimizer for optimizer, learning rate of 0.5, optimization target set to Cross_entropytrain_step= Tf.train.GradientDescentOptimizer (0.5). Minimize (Cross_entropy)
# Global Parameters Initialize and execute Runtf.initialize_all_variables (). Run ()
# FETCH 100 samples at a time, feed to placeholder, execute 1000 times, train_step data training forIinchRange (1000): Batch_xs,batch_ys= Mnist.train.next_batch (100) Train_step.run ({X:batch_xs,y_:batch_ys})
# to find the most probability of the number, to determine whether it is in line with the actual label, y is the predictive data, Y_ is the actual data correct_prediction= Tf.equal (Tf.argmax (y,1), Tf.argmax (y_,1))
# Calculate Accuracy Accuracy=Tf.reduce_mean (Tf.cast (correct_prediction,tf.float32))Print(Accuracy.eval ({x:mnist.test.images,y_:mnist.test.labels})
Overall, tensorflow feeling is relatively simple, perhaps this is just the simplest model it.
The concepts involved are only session,variable,placeholder,gradientdescentoptimizer.
Gradient descent and other complex methods are encapsulated, with Python less than 30 lines of code to achieve the handwriting recognition, although the recognition accuracy rate of only about 92%.
TensorFlow using the Softmax regression algorithm for handwriting recognition