TF is a single neural network - identified using perceptron solution mnist

Loading data set and describes Mnist

  1. mnist handwritten data sets, a total of 55,000 training samples, 5000 samples, and validation test samples 10000
  2. import tensorflow as tf
    import tensorflow.examples.tutorials.mnist.input_data as input_data
    import matplotlib.pyplot as plt
    import numpy as np
    # 数据载入
    mnist = input_data.read_data_sets("MNIST/", one_hot=True)  # 以独热编码的形式载入数据
    # 训练集训练数据,验证集评估调参,测试集进行最后的测试
    print("训练集数量: ", mnist.train.num_examples)
    print("验证集数量: ", mnist.validation.num_examples)
    print("测试集数量: ", mnist.test.num_examples)
    
    # 可以解开注释,看里面的数据形式
    # print("图像形状:", mnist.train.images.shape)  # 784即28X28
    # print("标签形状:", mnist.train.labels.shape)
    # print("首图尺寸:", mnist.train.images[0].shape)
    # print("首图内容:", mnist.train.images[0].reshape(28, 28))
    Loading code as described above, i.e., using the tf mnist module, wherein the method using read_data_sets, and note read in the form of one-hot encoded to read data into; above code print some information related to display data dimensions dimensions, see specific comments.
  3. For explanation of one-hot encoding: a so-called hot encoded is encoded in the form of an array, and the array has one and only one, the remaining values ​​are 0; can use this code print (mnist.labels [1]) to achieve printing , mnits data set, when loaded in terms hot encoded data, a unique tag value will be a one-dimensional array of heat; mnist due identification, the accuracy is determined based on the idea similar to the Euclidean distance, the use of heat alone coding the difference more easily calculated.

Data Partitioning

To construct the final model and training that can be predicted as accurately as possible the unknown data; to ensure effective prediction, the data set needs to be divided into a data set, the test set validation set.

The whole training:

And, in order to ensure the effect on the test set can be a good fit for the position data to verify the effect, it requires: the number of test set is large enough and until the last models do not touch the test set and test set and the training set for the data to be distribution on the overall description of the same.

In mnist dataset, officials have a good division of the training set, test set and validation set.

Batch read data

  1. Python may be utilized in slicing statements to retrieve data in batches; as print (mnist.train.labels [0:10]), to display the batch printing sample index tag value corresponding to 0 to 9.
  2. mnist packet in tf, there is provided a method of data read batches,
    # mnist中提供了下述方法,可以进行批量读取数据
    batch_images_xs, batch_labels_ys = mnist.train.next_batch(batch_size=10)
    print(batch_images_xs.shape, batch_images_ys.shape)
    I.e. next_batch; its parameters package volume into which the data (the batch_size) was read, return values ​​include a collection of images with a corresponding set of tags; through print statements, dimensions of the image set obtained for the [10,784], Dimensions tab set is [10, 10].

Construction of model

  1. x = tf.placeholder(tf.float32, [None, 784], name="X")
    y = tf.placeholder(tf.float32, [None, 10], name="Y")
    w = tf.Variable(tf.random_normal([784, 10]), name="W")
    b = tf.Variable(tf.zeros([10]), name="b")
    forward = tf.matmul(x, w)+b  # 前向计算
    preb = tf.nn.softmax(forward)  # 利用softmax进行结果向概率域的映射
    Defined input weighting and forward calculation. For input dimension, size is not limited only a limited number, a placeholder with NONE, facilitate mating and batch read data; while randomly initialized weights w, but note that corresponds to the dimensions, the matrix multiplication can be made to the x, b general initialization directly is 0; forward calculation is the definition of a matrix multiplication plus offset calculation, the results are computed using the softmax calculated probability value is mapped to a domain, this mapping is often used in the cnn, used The last layer FC.
  2. For softmax added: This method corresponds to the size of various types of score value calculated in the tag, gives a corresponding probability value, the probability that the total value is 1, a high probability scores corresponding to the type of a large value, the specific formula It is P_{i} = \frac{e^{^{y_{i}}}}{\sum_{k=1}^{C}e^{y_{k}}}, where C represents the number of classes.
  3. # 超参设置
    train_epochs = 50  # 训练轮数
    batch_size = 100  # 单批次训练样本数
    total_batch = int(mnist.train.num_examples/batch_size)  # 一轮训练要训练的批次数
    learing_rate = 0.01  # 学习率
    display_step = 1  # 显示粒度
    Definition of hyper-parameters, as shown in FIG. Setting a read 100 samples to do a single training, a training do total_batch single training times, to train a total of 50.
  4. # 定义损失函数
    # 交叉熵损失函数
    loss_fuction = tf.reduce_mean(-tf.reduce_sum(y*tf.log(preb), reduction_indices=1))
    Setting loss function, for multi-classification problems, often using cross entropy loss function; cross entropy in the field of information originally comparing the degree of approximation of the probability, in the use of hot encoded, calculating a difference between the results of the mapping softmax tag values for is the difference in probability; cross entropy is calculated H(p,q) = -\sum p(x)log(q(x))), where p is a value tag, q is a value calculated after softmax map, the closer the two, the smaller the cross entropy.
  5. # 优化器
    optimizer = tf.train.GradientDescentOptimizer(learing_rate).minimize(loss_fuction)
    Custom optimization; still using a gradient descent algorithm, multi-classification algorithm using L2 do loss_function when gradient descent algorithm might fall into local minimum, and using a cross-loss will avoid this problem.
  6. # 定义准确率
    correct_prediction = tf.equal(tf.argmax(preb, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    Operation defined accuracy, i.e. a correct ratio of training; correct_prediction said first set of predicted values ​​tag value set index distribution where the maximum value progressive, as a line corresponding to a sample index value after comparing various items obtained, otherwise, the same as True False; accuracy is the set of True and False into float32 type, all the elements in the averaging, the average result is accuracy.

Training and Evaluation Model

  1. # 声明会话以及初始化变量
    sess = tf.Session()
    init = tf.global_variables_initializer()
    sess.run(init)
    
    # 开始训练
    print("Train Start !!!!!")
    for epoch in range(train_epochs):  # 逐轮次
        for batch_size in range(total_batch):  # 共进行total_batch次的单一批次训练
            xs, ys = mnist.train.next_batch(batch_size)  # zip数据送入feed,此时类型已经符合
            sess.run(optimizer, feed_dict={x: xs, y: ys})  # 运行迭代优化器
        # 运行一个l轮后,利用验证即求解损失与准确度
        loss, acc = sess.run([loss_fuction, accuracy],
                             feed_dict={x: mnist.validation.images,y:mnist.validation.labels})
        # 打印本轮训练结果
        if(epoch+1) % display_step == 0:
            print("Train Epoch: ",'%02d'%(epoch+1), "Loss: ", "{:.9f}".format(loss),
                  "Accuracy: ", "{:.4f}".format(acc))
    
    
    print("Train Finish !!!!!")
    
    Training model, as the codes.
  2. # 测试结果可视化函数
    def plot_images_labels_prediction(images,  # 图像列表
                                      labels,  # 标签列表
                                      prediction,  # 预测值列表
                                      index,  # 所显示的第一张图的索引值
                                      num=10  # 显示的图像数目
    ):
        fig = plt.gcf()  # 获取当前图表
        fig.set_size_inches(10, 12)  # 设置总图尺寸为10英寸X12英寸
        if num > 25:  # 限制总的显示数目不超过25张
            num = 25
        for i in range(0, num):  # 逐个处理子图
            ax = plt.subplot(5, 5, i+1)
            ax.imshow(np.reshape(images[index], (28, 28)), cmap='binary')
            title = "label="+str(np.argmax(labels[index]))
            if len(prediction) > 0:
                title += ",Prediction=" + str(prediction[index])
            ax.set_title(title, fontsize=10)
            ax.set_xticks([])
            ax.set_yticks([])
            index += 1
        plt.show()
    Helper function for displaying the extracted images, the tag value and a predicted value.
  3. # 一次检测所有test样本
    prediction_result = sess.run(tf.argmax(preb, 1), feed_dict={x: mnist.test.images})
    # 打印前十个检测结果
    print(prediction_result[:10])
    # 可视化前10个检测结果
    plot_images_labels_prediction(mnist.test.images, mnist.test.labels, prediction_result, 10, 10)
    Using a model detection and visualization.
  4. A detection result as described above.

 

Released five original articles · won praise 0 · Views 63

Guess you like

Origin blog.csdn.net/weixin_41707744/article/details/104739434