mnist手写数据集(tensorflow)

本文介绍了如何运用全连接网络,通过TensorFlow框架实现对MNIST手写数字数据集的识别。详细讲解了网络结构及训练过程。
摘要由CSDN通过智能技术生成

全连接网络实现mnist

import tensorflow as tf
from tensorflow import keras
#  导入经典数据集加载模块
from tensorflow.keras import datasets
import matplotlib
from matplotlib import pyplot as plt


def preprocess(x, y):
    # 调用此函数时会自动传入 x,y 对象,shape 为[b, 28, 28], [b]
    # 标准化到 0~1
    x = tf.cast(x, dtype=tf.float32) / 255.
    x = tf.reshape(x, [-1, 28 * 28])  # 打平
    y = tf.cast(y, dtype=tf.int32)  # 转成整型张量
    y = tf.one_hot(y, depth=10)  # one-hot 编码
    # 返回的 x,y 将替换传入的 x,y 参数,从而实现数据的预处理功能
    return x, y;


#  数据加载
# 加载 MNIST 数据集
(x, y), (x_test, y_test) = datasets.mnist.load_data()
print('x:', x.shape, 'y:', y.shape, 'x test:', x_test.shape, 'y test:', y_test)

#  数据图片 x ,标签 y
#  构建 Dataset 对象
batchsz = 128
train_db = tf.data.Dataset.from_tensor_slices((x, y))
#  随机打散样本,不会打乱样本与标签映射关系
train_db 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值