import tensorflow as tf from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPool2D, Dropoutfrom tensorflow.keras import Model# 在GPU上运算时,因为cuDNN库本身也有自己的随机数生成器,所以即使tf设置了seed,也不会每次得到相同的结果tf.random.set_seed(100)mnist = tf.keras.datasets.mnist(X_train, y_train), (X_test, y_test) = mnist.load_data()X_train, X_test = X_train/255.0, X_test/255.0# 将特征数据集从(N,32,32)转变成(N,32,32,1),因为Conv2D需要(NHWC)四阶张量结构X_train = X_train[..., tf.newaxis] X_test = X_test[..., tf.newaxis]batch_size = 64# 手动生成mini_batch数据集train_ds = tf.data.Dataset.from_tensor_slices((X_train, y_train)).shuffle(10000).batch(batch_size)test_ds = tf.data.Dataset.from_tensor_slices((X_test, y_test)).batch(batch_size)class Deep_CNN_Model(Model): def __init__(self): super(Deep_CNN_Model, self).__init__() self.conv1 = Conv2D(32, 5, activation='relu') self.pool1 = MaxPool2D() self.conv2 = Conv2D(64, 5, activation='relu') self.pool2 = MaxPool2D() self.flatten = Flatten() self.d1 = Dense(128, activation='relu') self.dropout = Dropout(0.2) self.d2 = Dense(10, activation='softmax') def call(self, X): # 无需在此处增加training参数状态。只需要在调用Model.call时,传递training参数即可 X = self.conv1(X) X = self.pool1(X) X = self.conv2(X) X = self.pool2(X) X = self.flatten(X) X = self.d1(X) X = self.dropout(X) # 无需在此处设置training状态。只需要在调用Model.call时,传递training参数即可 return self.d2(X)model = Deep_CNN_Model()loss_object = tf.keras.losses.SparseCategoricalCrossentropy()optimizer = tf.keras.optimizers.Adam()train_loss = tf.keras.metrics.Mean(name='train_loss')train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')test_loss = tf.keras.metrics.Mean(name='test_loss')test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')# TODO:定义单批次的训练和预测操作@tf.functiondef train_step(images, labels): ...... @tf.functiondef test_step(images, labels): ...... # TODO:执行完整的训练过程EPOCHS = 10for epoch in range(EPOCHS)补全代码
时间: 2024-03-29 15:42:01 浏览: 128
ANACONDA +Cuda及cuDNN+Tensorflow-gpu版本+keras安装步骤的ppt
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPool2D, Dropout
from tensorflow.keras import Model
# 在GPU上运算时,因为cuDNN库本身也有自己的随机数生成器,所以即使tf设置了seed,也不会每次得到相同的结果
tf.random.set_seed(100)
# 加载MNIST数据集并进行预处理
mnist = tf.keras.datasets.mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train, X_test = X_train/255.0, X_test/255.0
X_train = X_train[..., tf.newaxis]
X_test = X_test[..., tf.newaxis]
# 定义batch_size并手动生成mini_batch数据集
batch_size = 64
train_ds = tf.data.Dataset.from_tensor_slices((X_train, y_train)).shuffle(10000).batch(batch_size)
test_ds = tf.data.Dataset.from_tensor_slices((X_test, y_test)).batch(batch_size)
# 定义深度CNN模型
class Deep_CNN_Model(Model):
def __init__(self):
super(Deep_CNN_Model, self).__init__()
self.conv1 = Conv2D(32, 5, activation='relu')
self.pool1 = MaxPool2D()
self.conv2 = Conv2D(64, 5, activation='relu')
self.pool2 = MaxPool2D()
self.flatten = Flatten()
self.d1 = Dense(128, activation='relu')
self.dropout = Dropout(0.2)
self.d2 = Dense(10, activation='softmax')
def call(self, X, training=False):
X = self.conv1(X)
X = self.pool1(X)
X = self.conv2(X)
X = self.pool2(X)
X = self.flatten(X)
X = self.d1(X)
X = self.dropout(X, training=training)
return self.d2(X)
# 实例化模型,定义损失函数和优化器
model = Deep_CNN_Model()
loss_object = tf.keras.losses.SparseCategoricalCrossentropy()
optimizer = tf.keras.optimizers.Adam()
# 定义训练和测试过程中的度量指标
train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')
# 定义单批次的训练和预测操作
@tf.function
def train_step(images, labels):
with tf.GradientTape() as tape:
# 计算模型预测结果和损失函数值
predictions = model(images, training=True)
loss = loss_object(labels, predictions)
# 计算梯度并优化模型参数
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
# 更新训练过程中的度量指标
train_loss(loss)
train_accuracy(labels, predictions)
@tf.function
def test_step(images, labels):
# 计算模型预测结果和损失函数值
predictions = model(images, training=False)
t_loss = loss_object(labels, predictions)
# 更新测试过程中的度量指标
test_loss(t_loss)
test_accuracy(labels, predictions)
# 执行完整的训练过程
EPOCHS = 10
for epoch in range(EPOCHS):
# 在每轮训练开始时,重置度量指标的值
train_loss.reset_states()
train_accuracy.reset_states()
test_loss.reset_states()
test_accuracy.reset_states()
# 遍历训练数据集中的每个mini_batch进行训练
for images, labels in train_ds:
train_step(images, labels)
# 遍历测试数据集中的每个mini_batch进行预测
for test_images, test_labels in test_ds:
test_step(test_images, test_labels)
# 输出每轮训练和测试过程中的度量指标
template = 'Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, Test Accuracy: {}'
print(template.format(epoch+1,
train_loss.result(),
train_accuracy.result()*100,
test_loss.result(),
test_accuracy.result()*100))
阅读全文