mnist手写数字识别库 训练例程
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, optimizers, datasets
# 训练集 和 测试集 x是图片数字 y是 数字大小
(x, y), (x_val, y_val) = datasets.mnist.load_data()
x = 2 * tf.convert_to_tensor(x, dtype=tf.float32) / 255. - 1
y = tf.convert_to_tensor(y, dtype=tf.int32)
y = tf.one_hot(y, depth=10) # [0,0,0,0,0,0,0,0,0,1]--->9
print(x.shape, y.shape)
train_dataset = tf.data.Dataset.from_tensor_slices((x, y))
print(train_dataset)
#分成200份
train_dataset = train_dataset.batch(200)
# 创建三层 784->512->256->10
model = keras.Sequential([
layers.Dense(512, activation='relu'),
layers.Dense(256, activation='relu'),
layers.Dense(10)])
'''
优化器 optimizers
SGD 随机梯度下降法,支持动量参数,支持学习衰减率,支持Nesterov动量
learning_rate:大或等于0的浮点数,学习率
momentum:大或等于0的浮点数,动量参数
decay:大或等于0的浮点数,每次更新后的学习率衰减值
nesterov:布尔值,确定是否使用Nesterov动量
'''
# 随机梯度下降法,支持动量参数,支持学习衰减率,支持Nesterov动量
optimizer = optimizers.SGD(learning_rate=0.001)
def train_epoch(epoch):
# Step4.loop
for step, (x, y) in enumerate(train_dataset):
with tf.GradientTape() as tape:
# [b, 28, 28] => [b, 784]
x = tf.reshape(x, (-1, 28 * 28))
# Step1. compute output
# [b, 784] => [b, 10]
out = model(x)
# Step2. compute loss
# reduce_sum按照求和的方式对矩阵降维
loss = tf.reduce_sum(tf.square(out - y)) / x.shape[0]
# Step3. optimize and update w1, w2, w3, b1, b2, b3
grads = tape.gradient(loss, model.trainable_variables)
# w' = w - lr * grad
optimizer.apply_gradients(zip(grads, model.trainable_variables))
if step % 299 == 0:
print(epoch, step, 'loss:', loss.numpy())
def train():
for epoch in range(50):
train_epoch(epoch)
if __name__ == '__main__':
train()
创建张量常用
tf.constant()
tf.convert_to_tensor()
tf.ones()
tf.zeros()
tf.fill()
tf.ones_like()
tf.random.normal()
tf.random.uniform()
tf.range()
tf.cast()
tf.Variable()
x=tf.random.normal([4,784])
net=layers.Dense(10)
net(x).shape
net.kernel.shape
net.bias.shape