tensorflow实战一---基于线性回归简单实现mnist手写体识别

       Mnist手写体识别是tensorflow的入门经典教程,此处的mnist的手写体识别率达到了91%,优化算法为梯度下降算法,激活函数为softmax回归,没有中间层,基本步骤可以分为七步。

1、设置变量

2、设置数据与结果的计算关系(设置图)

3、设置优化算法(梯度下降,train_step

4、设置激活函数(sotfmax

5、初始化数据

6、开始训练

7、模型评估

代码如下:

 

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

INPUT_NODE =
784
OUTPUT_NODE = 10

#梯度下降率
GDDOWN = 0.01

#训练轮数
TRAINING_TIMES = 1000

#每次训练数量
TRAINING_STEPS = 100

if __name__ == '__main__':
   
mnist = input_data.read_data_sets("/path/to/MNIST_data/", one_hot=True)
   
##首先打印出mnist图片数据的大小
   
print("input_data\'s train size: " , mnist.train.num_examples)
   
print("input_data\'s validation size: " , mnist.validation.num_examples)
   
print("input_data\'s test size: " , mnist.test.num_examples)

   
# 各个变量
   
x = tf.placeholder("float", shape=[None, INPUT_NODE])
   
y_ = tf.placeholder("float", shape=[None,OUTPUT_NODE])

   
W = tf.Variable(tf.zeros([INPUT_NODE, OUTPUT_NODE]))
    b = tf.Variable(tf.zeros([OUTPUT_NODE]))

    # 变量初始化
   
init = tf.initialize_all_variables()
   
sess = tf.InteractiveSession()
    sess.run(init)

    # 初始化图
   
y = tf.nn.softmax(tf.matmul(x, W) + b)

   
# 优化算法
   
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
   
train_step =tf.train.GradientDescentOptimizer(GDDOWN).minimize(cross_entropy)

    for i in range(TRAINING_TIMES):
       
# 训练
       
batch_xs, batch_ys =mnist.train.next_batch(TRAINING_STEPS)
       
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
       
# 模型评估
       
correct_prediction = tf.equal(tf.argmax(y, 1),tf.argmax(y_, 1))
       
accuracy =tf.reduce_mean(tf.cast(correct_prediction, "float"))
       
j = i+1
       
print("第%d轮训练,训练个数%d个" % (j,j*TRAINING_STEPS))
       
#print("正确率预测: " + correct_prediction + "\n")
       
print("当前正确率: ")
       
print(sess.run(accuracy,feed_dict={x: mnist.test.images, y_: mnist.test.labels}))



 

 

 

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是基于 TensorFlow 使用 LeNet-5 实现MNIST 手写数字识别分类的步骤: 1. 安装 TensorFlow:可以通过 pip 安装,命令为 `pip install tensorflow`。 2. 导入必要的库: ``` import tensorflow as tf from tensorflow.keras import datasets, layers, models ``` 3. 加载 MNIST 数据集: ``` (train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data() ``` 4. 对数据进行预处理: ``` train_images = train_images.reshape((60000, 28, 28, 1)) test_images = test_images.reshape((10000, 28, 28, 1)) train_images, test_images = train_images / 255.0, test_images / 255.0 ``` 5. 构建 LeNet-5 模型: ``` model = models.Sequential() model.add(layers.Conv2D(6, (5, 5), activation='relu', input_shape=(28, 28, 1))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(16, (5, 5), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Flatten()) model.add(layers.Dense(120, activation='relu')) model.add(layers.Dense(84, activation='relu')) model.add(layers.Dense(10, activation='softmax')) ``` 6. 编译模型: ``` model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) ``` 7. 训练模型: ``` model.fit(train_images, train_labels, epochs=5, validation_data=(test_images, test_labels)) ``` 8. 测试模型: ``` test_loss, test_acc = model.evaluate(test_images, test_labels) print('Test accuracy:', test_acc) ``` 以上就是基于 TensorFlow 使用 LeNet-5 实现MNIST 手写数字识别分类的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值