python手写数字数据集的识别

源代码

import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
import matplotlib.pyplot as plt
import numpy as np

# 0 读取mnist数据集
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
tf.compat.v1.disable_eager_execution()  #因为tensorflow版本问题的不同,所以为保证sess.run正常运行而进行的更改

# 1 读取训练集的一张图片与标签
batch_xs, batch_ys = mnist.train.next_batch(1)
img = tf.reshape(batch_xs, [28, 28])
label = tf.argmax(batch_ys, 1)

# 2 读取测试集的一张图片与标签
img_tt = mnist.test.images[0]
img_t = tf.reshape(img_tt, [28, 28])
label_tt = mnist.test.labels[0]
label_tt = np.reshape(label_tt, [1, 10])
label_t = tf.argmax(label_tt, 1)

with tf.compat.v1.Session() as sess:
    # 训练集 打印标签 像素值 
    print("训练集:%d" % sess.run(label))
    im = sess.run(img)
    for i in range(28):
        for j in range(28):
            print("%d " % round(im[i][j]), end='')
        print()

        # 测试集 打印标签 像素值
    print("测试集:%d" % sess.run(label_t))
    im_t = sess.run(img_t)
    for i in range(28):
        for j in range(28):
            print("%d " % round(im_t[i][j]), end='')
        print()

    # 显示图片
    plt.figure()
    plt.subplot(121)
    plt.imshow(im, cmap='gray')
    plt.subplot(122)
    plt.imshow(im_t, cmap='gray')
    plt.show()

运行结果

2021-05-10 00:23:48.679784: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2021-05-10 00:23:48.680059: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
WARNING:tensorflow:From D:/pythonProject/手写数字.py:7: read_data_sets (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as: tensorflow_datasets.load('mnist')
WARNING:tensorflow:From D:\Anaconda3\envs\tensorflow2.2_gpu\lib\site-packages\tensorflow\examples\tutorials\mnist\input_data.py:297: _maybe_download (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
WARNING:tensorflow:From D:\Anaconda3\envs\tensorflow2.2_gpu\lib\site-packages\tensorflow\examples\tutorials\mnist\input_data.py:299: _extract_images (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting MNIST_data/train-images-idx3-ubyte.gz
WARNING:tensorflow:From D:\Anaconda3\envs\tensorflow2.2_gpu\lib\site-packages\tensorflow\examples\tutorials\mnist\input_data.py:304: _extract_labels (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
WARNING:tensorflow:From D:\Anaconda3\envs\tensorflow2.2_gpu\lib\site-packages\tensorflow\examples\tutorials\mnist\input_data.py:112: _dense_to_one_hot (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.one_hot on tensors.
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
WARNING:tensorflow:From D:\Anaconda3\envs\tensorflow2
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值