tensorflow 2.0 实战-mnist 手写全连接

3.6 张量前向传播

  • 向前传播
  • 计算误差
  • 计算梯度
  • 更新梯度
  • 完整代码及注解

我们已经学到

  1. tensor 创建
  2. 索引和切片
  3. reshape and broadcasting
  4. 数学操作

实战前向传播:

  1. o u t = r e l u { r e l u { r e l u { X @ W 1 + b 1 } @ W 2 + b 2 } @ W 3 + b 3 } out = relu\{relu\{relu\{X@W_1+b_1 \}@W_2+b_2 \}@W_3+b_3\} out=relu{relu{relu{X@W1+b1}@W2+b2}@W3+b3}
  2. p r e d = a r g s m a x ( o u t ) pred = argsmax(out) pred=argsmax(out)
  3. L o s s = M S E ( o u t , l a b e l ) Loss = MSE(out, label) Loss=MSE(out,label)
  4. minimize loss

向前传播

r e l u { X @ W ! + b 1 } relu\{X@W_!+b_1\} relu{X@W!+b1}

w1 = tf.Variable(tf.random.truncated_normal([784, 512], stddev=0.1))
b1 = tf.Variable(tf.zeros([512]))
for epoch in range(10):
    for step, (x, y) in enumerate(train_db):
        x = tf.reshape(x, [-1,28*28])
        with tf.GradientTape() as tape:
            h1 = x@w1+tf.broadcast_to(b1, [x.shape[0], 512])
            h1 = tf.nn.relu(h1)

r e l u { r e l u { X @ W 1 + b 1 } @ W 2 + b 2 } relu\{relu\{X@W_1+b_1\}@W_2+b_2\} relu{relu{X@W1+b1}@W2+b2}

w1 = tf.Variable(tf.random.truncated_normal([784, 512], stddev=0.1))
b1 = tf.Variable(tf.zeros([512]))
w2 = tf.Variable(tf.random.truncated_normal([512, 256], stddev=0.1))
b2 = tf.Variable(tf.zeros([256]))
for epoch in range(10):
    for step, (x, y) in enumerate(train_db):
        x = tf.reshape(x, [-1,28*28])
        with tf.GradientTape() as tape:
            h1 = x@w1+tf.broadcast_to(b1, [x.shape[0], 512])
            h1 = tf.nn.relu(h1)
            h2 = h1@w2 + b2
            h2 = tf.nn.relu(h2)

o u t = r e l u { r e l u { r e l u { X @ W 1 + b 1 } @ W 2 + b 2 } @ W 3 + b 3 } out = relu\{relu\{relu\{X@W_1+b_1 \}@W_2+b_2 \}@W_3+b_3\} out=relu{relu{relu{X@W1+b1}@W2+b2}@W3+b3}

w1 = tf.Variable(tf.random.truncated_normal([784, 512], stddev=0.1))  # stddev 设置标准差 防止梯度弥散
b1 = tf.Variable(tf.zeros([512]))
w2 = tf.Variable(tf.random.truncated_normal([512, 256], stddev=0.1))
b2 = tf.Variable(tf.zeros([256]))
w3 = tf.Variable(tf.random.truncated_normal([256, 10], stddev=0.1))
b3 = tf.Variable(tf.zeros([10]))
for epoch in range(10):
    for step, (x, y) in enumerate(train_db):
        x = tf.reshape(x, [-1,28*28])
        with tf.GradientTape() as tape:
            h1 = x@w1+tf.broadcast_to(b1, [x.shape[0], 512])
            h1 = tf.nn.relu(h1)
            h2 = h1@w2 + b2
            h2 = tf.nn.relu(h2)
            out = h2@w3 + b3

计算误差

y_onehot = tf.one_hot(y, depth=10)
loss = tf.square(y_onehot - out)
loss = tf.reduce_mean(loss)  # scalar

计算梯度

grads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3])

更新梯度

# w1 = w1 - lr * w1_grad
w1.assign_sub(lr * grads[0])
b1.assign_sub(lr * grads[1])
w2.assign_sub(lr * grads[2])
b2.assign_sub(lr * grads[3])
w3.assign_sub(lr * grads[4])
b3.assign_sub(lr * grads[5])

完整代码及注解

import tensorflow as tf
from tensorflow.keras import datasets
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# load minst data
# x: [60k, 28, 28],
# y: [60k]
(x, y), _ = datasets.mnist.load_data()
# x: [0~255] => [0~1.]
x = tf.convert_to_tensor(x, dtype=tf.float32) / 255.
y = tf.convert_to_tensor(y, dtype=tf.int32)

print (x.shape, y.shape, x.dtype, y.dtype)
print (tf.reduce_min(x), tf.reduce_max(x))
print (tf.reduce_min(y), tf.reduce_max(y))

batch_size = 200
train_db = tf.data.Dataset.from_tensor_slices((x,y)).batch(batch_size)
train_iter = iter(train_db)
sample = next(train_iter)
print ('x_batch_size:',sample[0].shape,  'y_batch_size:', sample[1].shape)


# [b, 784] => [b, 512] => [b, 256] => [b, 10]
# w:[in_dim, out_dim], b:[dim_out]

w1 = tf.Variable(tf.random.truncated_normal([784, 512], stddev=0.1))  # stddev 设置标准差 防止梯度弥散
b1 = tf.Variable(tf.zeros([512]))
w2 = tf.Variable(tf.random.truncated_normal([512, 256], stddev=0.1))
b2 = tf.Variable(tf.zeros([256]))
w3 = tf.Variable(tf.random.truncated_normal([256, 10], stddev=0.1))
b3 = tf.Variable(tf.zeros([10]))

lr = 1e-3

for epoch in range(10):
    for step, (x, y) in enumerate(train_db):
        # x:[b, 28, 28]
        # y: [b]

        # [b, 28, 28] => [b, 28*28]
        x = tf.reshape(x, [-1,28*28])


        with tf.GradientTape() as tape:  # 默认跟踪 tf.Variable 变量
            # x: [b, 28*28]
            # h1 = x@w1 + b1
            # [b, 784]@[784, 512] + [512] => [b, 512] + [512] => [b, 512] + [b, 512]
            h1 = x@w1+tf.broadcast_to(b1, [x.shape[0], 512])
            h1 = tf.nn.relu(h1)
            # [b, 512] => [b, 256]
            h2 = h1@w2 + b2
            h2 = tf.nn.relu(h2)
            # [b, 256] => [b, 10]
            out = h2@w3 + b3

            # compute loss
            # out: [b, 10]
            # y: [b] => [b, 10]

            y_onehot = tf.one_hot(y, depth=10)

            # mse = mean(sum(y-out)^2)
            # [b, 10]
            loss = tf.square(y_onehot - out)
            loss = tf.reduce_mean(loss)

        # compute gradients
        grads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3])
        # print(grads)
        # w1 = w1 - lr * w1_grad
        w1.assign_sub(lr * grads[0])   # 原地更新  继续为 Variable 变量
        b1.assign_sub(lr * grads[1])
        w2.assign_sub(lr * grads[2])
        b2.assign_sub(lr * grads[3])
        w3.assign_sub(lr * grads[4])
        b3.assign_sub(lr * grads[5])

        if step%100 == 0:
            print ('epoch:', epoch, 'step:', step, 'loss:', float(loss))

注解:

  1. 初始化权值变量使用切断的正态分布并将标准差设置为0.01,是为了防止梯度弥散现象。
epoch: 0 step: 0 loss: 345733.0
epoch: 0 step: 100 loss: nan
epoch: 0 step: 200 loss: nan
  1. with tf.GradientTape() as tape: 只跟踪 tf.Variable 变量的梯度,因此在初始化以及更新梯度时,应该始终为 tf.Variable 变量。
w1 = tf.Variable(tf.random.truncated_normal([784, 512], stddev=0.1))  # stddev 设置标准差 防止梯度弥散
b1 = tf.Variable(tf.zeros([512]))
w2 = tf.Variable(tf.random.truncated_normal([512, 256], stddev=0.1))
b2 = tf.Variable(tf.zeros([256]))
w3 = tf.Variable(tf.random.truncated_normal([256, 10], stddev=0.1))
b3 = tf.Variable(tf.zeros([10]))
# w1 = w1 - lr * w1_grad
w1.assign_sub(lr * grads[0])   # 原地更新  继续为 Variable 变量
b1.assign_sub(lr * grads[1])
w2.assign_sub(lr * grads[2])
b2.assign_sub(lr * grads[3])
w3.assign_sub(lr * grads[4])
b3.assign_sub(lr * grads[5])

你可能感兴趣的:(tensorflow,深度学习,深度学习,tensorflow)