-
大小: 4KB文件类型: .py金币: 1下载: 0 次发布日期: 2021-06-02
- 语言: Python
- 标签: tensorflow2. python mnist
资源简介
基于python3.7版本的tensorflow2.0实现mnist手写数字识别代码
代码片段和文件信息
import warnings
warnings.filterwarnings(‘ignore‘)
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers datasets Sequential optimizers metrics
(x_train y_train) (x_test y_test) = tf.keras.datasets.mnist.load_data()
# 数据预处理
def train_preprocess(x_train y_train):
x_train = tf.cast(x = x_train dtype = tf.float32) / 255.
y_train = tf.cast(x = y_train dtype = tf.int32)
y_train = tf.one_hot(indices = y_train depth = 10)
return x_train y_train
def test_preprocess(x_test y_test):
x_test = tf.cast(x = x_test dtype = tf.float32) / 255.
y_test = tf.cast(x = y_test dtype = tf.int32)
return x_test y_test
train_db = tf.data.Dataset.from_tensor_slices(tensors=(x_train y_train))
train_db = train_db.map(map_func=train_preprocess).shuffle(buffer_size=1000).batch(batch_size=128)
test_db = tf.data.Dataset.from_tensor_slices(tensors=(x_test y_test))
test_db = test_db.map(map_func=test_preprocess).batch(batch_size=128)
# 建立网络模型
model = tf.keras.Sequential([
tf.keras.layers.Dense(units=512 activation=tf.nn.relu)
tf.keras.layers.Dense(units=256 activation=tf.nn.relu)
tf.keras.layers.Dense(units=128 activation=tf.nn.relu)
tf.keras.layers.Dense(units=32 activation=tf.nn.relu)
tf.keras.layers.Dense(units=10)
])
model.build(input_shape=[None 28 * 28])
model.summary()
optimizer = tf.keras.optimizers.Adam(learning_rate = 1e-4)
def main():
for epoch in range(100):
for step (x_train y_train) in enumerate(train_db):
x_train = tf.reshape(tensor = x_train shape = [-1 28 * 28])
with tf.GradientTape() as tape:
logits = model(x_train)
loss = tf.losses.categorical_crossentropy(y_true = y_train y_pr
- 上一篇:中间代码生成代码中缀表达式转换为四元式
- 下一篇:python QQ第三方登陆
相关资源
- python QQ第三方登陆
- Python源码剖析_代码(pythonympx.rar)
- 豆瓣爬虫python
- 计算机视觉视频教程百度云盘资源
- Shapely-1.6.4.post1-cp36-cp36m-win_amd64.whl
- python 战棋游戏六边形地图代码实现
- naive bayes代码实现(python版)
- springcloudpython
- MODIS_Mosaic.py
- 经典动量与反转交易策略python版
- Python习题集含答案
- Python实现一个简单的3层BP神经网络
- python-urx-master.zip
- Python3.x+Pyqt5实现绘图界面matplotlib绘图
- python-克里金插值 代码
- python就业班.txt
- 爬虫源码:分页爬取,mysql数据库连接
- python 邻接矩阵三种方法实现有向图、
- Python3入门与进阶
- mmdetection在windows中可运行的train.py
- 编译原理由正则表达式到NFA到DFA到最
- Pyboard利用两个Zigbee模块发送并接收
- python实现贪吃蛇小游戏
- Python-TensorFlow语义分割组件
- numpy-1.17.2+mkl-cp37-cp37m-win_amd64.rar
- python大作业--爬虫完美应付大作业.z
- pyltp python3.7可用版本,已编译好的.
- python小游戏大全——30个
- python3网络爬虫开发实战 无密码
- 山东大学抢课脚本
评论
共有 条评论