TensorFlow2.0 前向传播 -- 测试集正确率统计

TensorFlow2.0 前向传播 – 测试集正确率统计

1.导入与设置
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets
print(tf.__version__)
2.加载数据集
# 加载数据集
(x, y), (x_test, y_test) = datasets.mnist.load_data()
print(x.shape)
print(y.shape)
print(x_test.shape)
print(y_test.shape)
3.图片预处理
x = tf.convert_to_tensor(x, dtype = tf.float32)
y = tf.convert_to_tensor(y, dtype = tf.int32)
print(x.dtype)
print(y.dtype)

x_test = tf.convert_to_tensor(x_test, dtype = tf.float32)
y_test = tf.convert_to_tensor(y_test, dtype = tf.int32)
print(x_test.dtype)
print(y_test.dtype)

# 查看X的最小值 最大值
print(tf.reduce_min(x), tf.reduce_max(x))
print(tf.reduce_min(y), tf.reduce_max(y))

# 图像预处理
# x[0, 255] -> [0, 1]
x = x / 255.0
x_test = x_test / 255.0
print(tf.reduce_min(x), tf.reduce_max(x))
print(tf.reduce_min(x_test), tf.reduce_max(x_test))

在这里插入图片描述

4.数据集制作 变量初始化
# 制作 Dataset
train_db = tf.data.Dataset.from_tensor_slices((x, y)).batch(128)
test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(128)

# 查看数据集 shape
train_iter = iter(train_db)
sample = next(train_iter)
print("batch:", sample[0].shape, sample[1].shape)

test_iter = iter(test_db)
test_sample = next(test_iter)
print("test batch:", test_sample[0].shape, test_sample[1].shape)

# 初始化 参数 学习率
lr = 1e-3

# 随机初始化 参数
# [b, 784] ==> [b, 256] ==> [b, 128] ==> [b, 10]
# [dim_in, dim_out], [dim_out]
w1 = tf.Variable(tf.random.truncated_normal([784, 256], stddev=0.1))
b1 = tf.Variable(tf.zeros([256]))
w2 = tf.Variable(tf.random.truncated_normal([256, 128], stddev=0.1))
b2 = tf.Variable(tf.zeros([128]))
w3 = tf.Variable(tf.random.truncated_normal([128, 10], stddev=0.1))
b3 = tf.Variable(tf.zeros([10]))
5.数据集制作 测试集 准确率统计
# 循环训练(外层循环)
for epoch in range(10):
    # 数据集循环(内层循环)
    for step, (x, y) in enumerate(train_db):
        # x:[128, 28, 28]
        # y:[128]
        
        # 维度变换
        # x:[128, 28, 28] ==> [128, 28*28]
        x = tf.reshape(x, [-1, 28*28])
        
        # 记住并更新 参数变量
        with tf.GradientTape() as tape: # tf.Variable
            
            # 第一层网络 矩阵相乘 自动broadcast
            h1 = x @ w1 + tf.broadcast_to(b1, [x.shape[0], 256])
            # x:[b, 28 * 28]
            # h1 = x @ w1 + b1
            # [b, 784] @ [784, 256] + [256] ==> [b, 256] + [256] 
            # ==> [b, 256] + [b, 256]
            
            # 添加激活函数
            h1 = tf.nn.relu(h1)
            
            # 第二层网络 矩阵相乘 + 激活函数
            # [b, 256] ==> [b, 128]
            h2 = h1 @ w2 + b2
            h2 = tf.nn.relu(h2)
            
            # 第三层网络 输出层 矩阵相乘
            # [b, 128] ==> [b, 10]
            out = h2 @ w3 + b3

            # 计算误差
            # out [b, 10] 
            # y:[b]
            y_onehot = tf.one_hot(y, depth=10)

            # 均方差  mse = Mean(sum(y-out)^2)
            loss = tf.square(y_onehot - out)
            loss = tf.reduce_mean(loss)
            
        # 计算梯度 
        grads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3])
        
        # 自减 更新参数变量(学习率 * 梯度变换)
        # w1 = w1 - lr * w1_grads
        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])
        
        # 打印部分数据
        if step % 100 == 0:
            print("epoch=", epoch, "step=", step, " loss:", float(loss))
    
    # 使用测试集 测试正确率
    total_correct = 0  # 统计正确的总个数
    total_num = 0      # 处理样本数
    for step, (x, y) in enumerate(test_db):
        # 维度变换 [b, 28, 28] ==> [b, 28*28]
        x = tf.reshape(x, [-1, 28*28])
        
        # 前向传播 矩阵处理
        h1 = tf.nn.relu(x @ w1 + b1) 
        h2 = tf.nn.relu(h1 @ w2 + b2)
        out = h2 @ w3 + b3
        # 输出结果 out [b, 10]  实数类型 ==> 总和为1 [0, 1]的实数类型 
        prob = tf.nn.softmax(out, axis=1)
        # 概率最大索引 [b, 10] ==> [b]
        pred = tf.argmax(prob, axis=1)
        # 类型变换 tf.int64 ==> tf.int32
        pred = tf.cast(pred, dtype=tf.int32)
        # 与结果比较 y [b]  ==> [b] bool类型 ==> [b] int32类型
        correct = tf.cast(tf.equal(pred, y), dtype=tf.int32)
        # 降维统计正确数量 
        correct_sum = tf.reduce_sum(correct)
        
        # 累加统计 正确总数 
        total_correct += correct_sum
        # 累加统计 处理总数  [b]
        total_num += x.shape[0]
    
    # 计算准确率 并打印
    acc = total_correct / total_num
    print("acc:", acc.numpy())

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值