Handwritten numeral recognition & Tensorboard

Tensorboard&Tensorflow

As the most popular functional parts, Tensorflow and Tensorboard have a strong power to build and visualize your neural network which make your project much more understandable. Today, I am trying to explain the usage of Tensorboard and put the construction of neural network into practice.

Tensorboard

`Our ideal network:
在这里插入图片描述
The network visualized by tensorboard:
在这里插入图片描述
In this flow chart, all the data are measured by ‘tensor’, a special data type in tensorflow. Therefore we can clearly see the flow of those tensors.

Simple network

import Database

We will use the database handwritten numeral recognition in ‘mnist’ as our ‘Train’ and ‘Test’ data.

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets(r"C:\Users\ghx\PycharmProjects\untitled\.idea\mnist", one_hot=True)

visualize the unprocessed data:
在这里插入图片描述

Define the neural layer

Our basic thought is to create a function and deliver the shape parameter and specific inputs to return us the outputs. Here comes the code(the structural frames in tensorboard are also added ):


def add_layer(inputs,in_size,out_size,activation_function=None):
    with tf.name_scope('input_layer'):
        # add input layer and return output layer
        with tf.name_scope('weights'):
            Weights = tf.Variable(tf.random_normal([in_size,out_size]))
            tf.summary.histogram('/weight',Weights)
        with tf.name_scope('biases'):
            biases = tf.Variable(tf.zeros([1,out_size]))+ 0.1
            tf.summary.histogram('/biase', biases)
        with tf.name_scope('Wx_plus_b'):
            Wx_plus_b = tf.matmul(inputs,Weights) + biases
        if activation_function is None:
            outputs = Wx_plus_b
        else:
            outputs =activation_function(Wx_plus_b)
        tf.summary.histogram('/Outputs', outputs)
        return outputs

在这里插入图片描述

Cross_entropy&Optimizer

In this project we focus on how to identify the number behind the handwritten pictures, meaning that above all it’s a classification qusetion. Hence, we use cross_entropy as the optimization target.


with tf.name_scope('cross_entropy'):
    # error between prediction and training label
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),reduction_indices=[1]))#loss
    tf.summary.scalar('cross_entropy',cross_entropy)

with tf.name_scope('train_step'):
    # train method -> GradiantDescent
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

在这里插入图片描述

Train the network and caculate the accuracy
def compute_accuracy(v_xs, v_ys):
    global prediction
    with tf.name_scope('accuracy'):
        y_pre = sess.run(prediction,feed_dict={xs: v_xs})
        with tf.name_scope('correct_prediction'):
            # tf.arg_max(y_pre,1) the predicted label
            # judge whether v_pre and v_ys share the label
            correct_prediction = tf.equal(tf.arg_max(y_pre,1),tf.arg_max(v_ys, 1))
        with tf.name_scope('accuracy'):
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        result = sess.run(accuracy,feed_dict={xs:v_xs, ys:v_ys})
        tf.summary.histogram('/result',result)
    return result

with tf.name_scope('inputs'):
sess = tf.Session()
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter('number',sess.graph)
sess.run(tf.global_variables_initializer())

for i in range(1000):
    batch_xs,batch_ys = mnist.train.next_batch(100)# SGD
    sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys})
    if i % 50 == 0:
        print(compute_accuracy(mnist.test.images, mnist.test.labels))
        rs = sess.run(merged,feed_dict={xs: batch_xs, ys: batch_ys})
        writer.add_summary(rs,i)

Print the accuracy every 50 steps:
0.1826
0.6357
0.7335
0.7789
0.8061
0.8181
0.8313
0.8384
0.8453
0.8528
0.8539
0.8592
0.8614
0.8612
0.8645
0.8665
0.866
0.8721
0.8731
0.8737

CNN neural network

I am not going to explain the rationale of CNN as I am also a naive guy when dealing with these questions. I will just try to build the core parts of CNN which are the convolutional layer and pooling layer.

conv1 = tf.layers.conv2d(inputs=image,filter=16,kernel_size=5,stride=1,padding='same'
                        activation=tf.nn.relu)  #(28,28,16)
pooling1=tf.layers.max_poolong2d(conv1,pool_size=2,strides=2) #(14,14,16)
conv2=tf.layers.conv2d(pooling1,filter=32,5,1,'same',tf.nn.relu) #(14,14,32)
pooling2=tf.layers.max_poolong2d(conv2,2,2) #(7,7,32)
flat=tf.reshape(pooling2,[-1,7*7*32]) # 1568 cols
output=tf.layers.dense(flat,10)  
 
#loss
loss = tf.losses.softmax_cross_entropy(onehot_labels=tf_y, logits=output)           # compute cost
train_op = tf.train.AdamOptimizer(lr).minimize(loss)
 

every 100 steps print the train accuracy:
在这里插入图片描述
Finally, test the CNN network by passing the mnist.test into it:
在这里插入图片描述

The CNN network is inspired by those two blogs:
https://blog.csdn.net/greedystar/article/details/80444519
https://blog.csdn.net/xiexu911/article/details/88634970

The statement: All the materials are used to study python, if this article involves all infringement, please contact me to delete.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
手写数字分类是一种利用神经网络(NN)方法的图像识别任务。在这种任务中,我们的目标是对手写数字图像进行分类,将其识别为0到9之间的具体数字。 通过使用神经网络,我们可以训练一个模型来学习特定类型的图像,并将其与其他类型的图像进行区分。神经网络是一种由多个神经元组成的网络,每个神经元都有一个权重和一个激活函数。通过不断调整神经元的权重,模型可以学习到不同特征之间的关联性,从而对新的图像进行分类。 在手写数字分类任务中,我们需要用大量的手写数字图像作为训练数据来训练神经网络模型。这些图像通常都是灰度图像,每个图像都包含一个手写数字。我们需要将这些图像转换成数字矩阵,然后将其输入到神经网络中。 训练神经网络模型的过程是一个迭代的过程。在每一轮迭代中,我们通过将训练数据输入到网络中来计算预测结果,并计算预测结果与真实标签之间的误差。然后,根据误差来更新神经元的权重,使得下一轮的预测结果更加准确。 通过不断迭代训练,我们可以逐渐提高模型对手写数字的分类准确率。一旦训练完成,我们就可以使用这个模型来对新的手写数字图像进行分类,从而实现自动识别手写数字的功能。 总而言之,通过使用神经网络方法,我们可以实现手写数字图像的分类任务。这种方法可以通过训练神经网络模型来学习手写数字的特征,并将其与其他类型的图像进行区分,从而实现自动识别手写数字的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值