详解TensorFlow训练网络两种方式
作者:学姐带你玩AI 时间:2021-06-24 00:18:23
TensorFlow训练网络有两种方式,一种是基于tensor(array),另外一种是迭代器
两种方式区别是:
第一种是要加载全部数据形成一个tensor,然后调用model.fit()然后指定参数batch_size进行将所有数据进行分批训练
第二种是自己先将数据分批形成一个迭代器,然后遍历这个迭代器,分别训练每个批次的数据
方式一:通过迭代器
IMAGE_SIZE = 1000
# step1:加载数据集
(train_images, train_labels), (val_images, val_labels) = tf.keras.datasets.mnist.load_data()
# step2:将图像归一化
train_images, val_images = train_images / 255.0, val_images / 255.0
# step3:设置训练集大小
train_images = train_images[:IMAGE_SIZE]
val_images = val_images[:IMAGE_SIZE]
train_labels = train_labels[:IMAGE_SIZE]
val_labels = val_labels[:IMAGE_SIZE]
# step4:将图像的维度变为(IMAGE_SIZE,28,28,1)
train_images = tf.expand_dims(train_images, axis=3)
val_images = tf.expand_dims(val_images, axis=3)
# step5:将图像的尺寸变为(32,32)
train_images = tf.image.resize(train_images, [32, 32])
val_images = tf.image.resize(val_images, [32, 32])
# step6:将数据变为迭代器
train_loader = tf.data.Dataset.from_tensor_slices((train_images, train_labels)).batch(32)
val_loader = tf.data.Dataset.from_tensor_slices((val_images, val_labels)).batch(IMAGE_SIZE)
# step5:导入模型
model = LeNet5()
# 让模型知道输入数据的形式
model.build(input_shape=(1, 32, 32, 1))
# 结局Output Shape为 multiple
model.call(Input(shape=(32, 32, 1)))
# step6:编译模型
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# 权重保存路径
checkpoint_path = "./weight/cp.ckpt"
# 回调函数,用户保存权重
save_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
save_best_only=True,
save_weights_only=True,
monitor='val_loss',
verbose=0)
EPOCHS = 11
for epoch in range(1, EPOCHS):
# 每个批次训练集误差
train_epoch_loss_avg = tf.keras.metrics.Mean()
# 每个批次训练集精度
train_epoch_accuracy = tf.keras.metrics.SparseCategoricalAccuracy()
# 每个批次验证集误差
val_epoch_loss_avg = tf.keras.metrics.Mean()
# 每个批次验证集精度
val_epoch_accuracy = tf.keras.metrics.SparseCategoricalAccuracy()
for x, y in train_loader:
history = model.fit(x,
y,
validation_data=val_loader,
callbacks=[save_callback],
verbose=0)
# 更新误差,保留上次
train_epoch_loss_avg.update_state(history.history['loss'][0])
# 更新精度,保留上次
train_epoch_accuracy.update_state(y, model(x, training=True))
val_epoch_loss_avg.update_state(history.history['val_loss'][0])
val_epoch_accuracy.update_state(next(iter(val_loader))[1], model(next(iter(val_loader))[0], training=True))
# 使用.result()计算每个批次的误差和精度结果
print("Epoch {:d}: trainLoss: {:.3f}, trainAccuracy: {:.3%} valLoss: {:.3f}, valAccuracy: {:.3%}".format(epoch,
train_epoch_loss_avg.result(),
train_epoch_accuracy.result(),
val_epoch_loss_avg.result(),
val_epoch_accuracy.result()))
方式二:适用model.fit()进行分批训练
import model_sequential
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
# step2:将图像归一化
train_images, test_images = train_images / 255.0, test_images / 255.0
# step3:将图像的维度变为(60000,28,28,1)
train_images = tf.expand_dims(train_images, axis=3)
test_images = tf.expand_dims(test_images, axis=3)
# step4:将图像尺寸改为(60000,32,32,1)
train_images = tf.image.resize(train_images, [32, 32])
test_images = tf.image.resize(test_images, [32, 32])
# step5:导入模型
# history = LeNet5()
history = model_sequential.LeNet()
# 让模型知道输入数据的形式
history.build(input_shape=(1, 32, 32, 1))
# history(tf.zeros([1, 32, 32, 1]))
# 结局Output Shape为 multiple
history.call(Input(shape=(32, 32, 1)))
history.summary()
# step6:编译模型
history.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# 权重保存路径
checkpoint_path = "./weight/cp.ckpt"
# 回调函数,用户保存权重
save_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
save_best_only=True,
save_weights_only=True,
monitor='val_loss',
verbose=1)
# step7:训练模型
history = history.fit(train_images,
train_labels,
epochs=10,
batch_size=32,
validation_data=(test_images, test_labels),
callbacks=[save_callback])
来源:https://blog.csdn.net/m0_47256162/article/details/122179151
标签:TensorFlow,训练网络
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python协程asyncio异步编程笔记分享
2022-03-05 22:23:31
微信小程序页面缩放式侧滑效果的实现代码
2023-09-02 05:21:45
![](https://img.aspxhome.com/file/2023/4/56264_0s.gif)
Python数据类型转换详解
2021-03-04 04:11:13
如何确定Oracle数据库表重复的记录
2010-07-26 13:23:00
iframe高度自适应,兼容IE,FF
2008-06-18 12:15:00
PHP程序员玩转Linux系列 nginx初学者引导
2023-11-21 19:51:16
带农历的JavaScript日期时间js代码
2010-08-01 10:29:00
MySQL语句优化的原则
2009-10-27 12:09:00
页面中横排布局的思考
2008-01-18 12:56:00
![](https://img.aspxhome.com/file/UploadPic/20081/18/200811813731407s.jpg)
YUI学习笔记(1)
2009-01-12 18:06:00
教你轻松了解MySQL数据库中的结果字符串
2009-02-23 17:29:00
详谈javascript异步编程
2023-08-23 17:49:53
使用Python从零开始撸一个区块链
2021-06-02 07:28:22
![](https://img.aspxhome.com/file/2023/7/66827_0s.jpg)
python中数据爬虫requests库使用方法详解
2022-04-01 05:47:11
![](https://img.aspxhome.com/file/2023/8/76988_0s.jpg)
PHP获取类私有属性的3种方法
2023-11-20 10:32:16
![](https://img.aspxhome.com/file/2023/6/106296_0s.jpg)
python抓取网页内容并进行语音播报的方法
2021-03-08 14:02:53
PHP5 mysqli的prepare准备语句使用说明
2023-11-22 12:50:29
Typecho插件实现添加文章目录的方法详解
2023-05-25 07:19:04
教你安装python Django(图文)
2023-11-13 14:22:39
![](https://img.aspxhome.com/file/2023/0/63890_0s.png)
基于numpy实现逻辑回归
2023-06-21 10:04:25
![](https://img.aspxhome.com/file/2023/5/72745_0s.jpg)