使用tf.data数据转换来训练MNIST数据集

以MNIST数据集为例来训练模型

# -*- coding: UTF-8 -*-
"""
Author: LGD
FileName: fashion_mnist_tfdataset
DateTime: 2020/11/26 09:04 
SoftWare: PyCharm
"""
import tensorflow as tf

print('Tensorflow version: {}'.format(tf.__version__))

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()

# 数据归一化
train_images = train_images / 255
test_images = test_images / 255

# 建立train_images的Dataset
ds_train_img = tf.data.Dataset.from_tensor_slices(train_images)
print(ds_train_img)
ds_train_label = tf.data.Dataset.from_tensor_slices(train_labels)
print(ds_train_label)

# 使用zip将数据合并到一起
ds_train = tf.data.Dataset.zip((ds_train_img, ds_train_label))
print(ds_train)

# 对数据做变换,取出10000组数据乱序,循环,分批次,每批次数据量为64
ds_train = ds_train.shuffle(10000).repeat().batch(64)

# 建立模型
model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

# 编译模型
model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)

# # 训练
# steps_per_epochs = train_images.shape[0] // 64  # 每次迭代64张图片,每个epoch迭代的步数
# model.fit(
#     ds_train,
#     epochs=5,
#     steps_per_epoch=steps_per_epochs
# )

# 建立test_images的Dataset
ds_test = tf.data.Dataset.from_tensor_slices((test_images, test_labels))
ds_test = ds_test.batch(64)

# 训练
steps_per_epochs = train_images.shape[0] // 64  # 每次迭代64张图片,每个epoch迭代的步数
model.fit(
    ds_train,
    epochs=5,
    steps_per_epoch=steps_per_epochs,
    validation_data=ds_test,
    validation_steps=10000//64  # 由于有循环,必须要有step它才知道什么时候打印一下验证准确率。
)

获取MNIST数据集,可以直接是在代码加载里下载,也可以关注下列公众号加读者微信,分享百度网盘链接。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hong_Youth

您的鼓励将是我创作的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值