tensorflow手写体识别

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
# -*- coding:utf-8 -*-
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

# number from 0 to 9:
mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)


def add_layer(inputs, in_size, out_size, activation_function=None):
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    bises = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs, Weights) + bises

    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)

    return outputs


# 计算准确度
def compute_accuracy(x, y):
    global prediction
    y_pre = sess.run(prediction, feed_dict={xs: x})
    correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    result = sess.run(accuracy, feed_dict={xs: x, ys: y})
    return result


# def placeholder for inputs
xs = tf.placeholder(tf.float32, [None, 784])  # 28*28
ys = tf.placeholder(tf.float32, [None, 10])  # 10个输出

# add output layer
prediction = add_layer(xs, 784, 10, tf.nn.softmax)  # softmax常用于分类

cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction), reduction_indices=[1]))
train = tf.train.GradientDescentOptimizer(0.3).minimize(cross_entropy)

sess = tf.Session()
sess.run(tf.initialize_all_variables())

for i in range(2000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train, feed_dict={xs: batch_xs, ys: batch_ys})
    if i % 100 == 0:
        print(compute_accuracy(mnist.test.images, mnist.test.labels))
TensorFlow是一个广泛应用于机器学习和深度学习的开源框架,也可以用于手写字体识别任务。手写字体识别是指将手写的字符转换为可识别的文本形式,常用于识别手写字体的数字、字母和汉字等。 在TensorFlow中,可以使用卷积神经网络(CNN)来进行手写字体识别。首先,需要准备一个手写字体数据集,包含大量的手写字符样本。然后,使用TensorFlow的图像处理功能将手写字符样本进行预处理,将其转换为标准大小的图像。 接下来,可以利用TensorFlow的深度学习模型构建和训练一个卷积神经网络。卷积神经网络是一种专门用于处理图像识别任务的神经网络模型,通过多层的卷积、池化和全连接层,可以高效地提取并学习图像的特征。 在训练过程中,可以使用TensorFlow提供的优化算法和损失函数来使得模型逐渐收敛,并能够正确地识别手写字体。通过反复迭代和不断调整模型参数,可以提高模型在手写字体识别任务上的准确率。 最后,当模型训练完成后,就可以将其应用于实际的手写字体识别场景中。只需将待识别的手写字符输入到经过训练的模型中,即可输出对应的文本标识,实现手写字体识别的功能。 总而言之,利用TensorFlow进行手写字体识别可以通过构建和训练卷积神经网络模型实现。这种方法可以提高手写字体识别的准确率和效率,并可以应用于各种实际场景中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值