First, I will briefly explain the MNIST that we will use this time. MNIST is the name of an image dataset of handwritten numbers (0-9). The contents of the MNIST dataset used this time are ・ Train: 55,000 sheets ・ Validationm: 5000 sheets ・ Test: 10000 sheets It is composed of a total of 70,000 sheets, and is a set of handwritten number images and labels for correct answers.
train is training, validation is hyperparameter training, test is used to check the accuracy of the created model, and this code does not use validation data.
One image is a 28 * 28 gray image (= 1 channel)
The model created this time is as follows
The layer depth is a simple model with only two layers, an input layer and an output layer. The number of neurons in the input layer is 784 in length x width x number of channels (28 * 28 * 1) when the MNIST image is converted into a one-dimensional array, and the output is 0 to 9, for a total of 10 neurons. The method of converting to this one-dimensional array ignores the relationship and shape of adjacent pixel information, but this time we will use this method. Next time, I would like to make an estimation using the method using CNN.
mnist_for_ml_beginners
#Import TensorFlow
import tensorflow as tf
#Import of Numpu of library for numerical calculation
import numpy as np
#tensorflow.contrib.learn.python.learn.datasets.mnist.py function read_data_Import sets
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
#Read MNIST data
mnist = read_data_sets("MNIST_data/", one_hot=True)
#Input variables(The number of data:Nne input data:784 pieces)
x = tf.placeholder("float", [None, 784])
#Variable for weight(Initialize with 0)
W = tf.Variable(tf.zeros([784, 10]))
#Bias variable(Initialize with 0)
b = tf.Variable(tf.zeros([10]))
#output(Perform softmax regression after calculating the inner product of x and W and adding bias)
#By using the softmax function, the total value of the output becomes 1.(=Can be expressed as a probability)
y = tf.nn.softmax(tf.matmul(x, W) + b)
#Variables for correct labels(1 out of 10 values is 1 and the rest are 0)
y_ = tf.placeholder("float", [None, 10])
#Calculate cross entropy error(The larger the value of y corresponding to the correct label, the closer to 0)
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
#Minimize cross entropy error using gradient descent
#Learning rate is 0.001(Hyperparameters)
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
#Start the model created in the session
sess = tf.Session()
#Initialization
#tf.initialize_all_variables() -> tf.global_variables_initializer()
sess.run(tf.global_variables_initializer())
#Start learning 1000 times
for i in range(1000):
#Batch processing(Randomly get 100 from train)
batch_xs, batch_ys = mnist.train.next_batch(100)
#feet_In dict x and y_Replace value with placeholder in
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
#Evaluate the model
#argmax(y,1) :Calculate the index of the largest value in y
#tf.equal(,) :True if the values match, False otherwise
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
#tf.cast() :Cast bool type to float type(True =1, False =0)
#tf.reduce_mean() :Calculate the average value
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
#Precision calculation(=Use Mnist Test)
print (sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
The accuracy of the execution result is about 92%
What I learned this time ・ Input x and correct label y_ are placeholders ・ Weight W and bias b are Variable To use.
And if you implement this code, it will also calculate using GPU, so I found that it is a library that you can easily try machine learning, so I would like to continue trying various things using TensorFlow in the future.
Recommended Posts