TensorFlow 2.0全连接单隐藏层网络建模实现

该博客详细介绍了如何使用TensorFlow 2.0构建一个全连接的单隐藏层神经网络,包括数据加载、划分、预处理、模型构建、损失函数、优化器选择以及模型训练等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、载入数据

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

print("Tensorflow版本是:",tf.__version__)

mnist = tf.keras.datasets.mnist
(train_images,train_labels),(test_images,test_labels)=mnist.load_data()

2、数据集划分

total_num = len(train_images)
valid_split = 0.2   #验证集的比例占20%
train_num = int(total_num*(1-valid_split))#训练集的数目

train_x = train_images[:train_num]#前部分给训练集
train_y = train_labels[:train_num]

valid_x = train_images[train_num:]#后20%给验证集
valid_y = train_labels[train_num:]

test_x = test_images
test_y = test_labels

3、数据塑性

train_x = train_x.reshape(-1,784)
valid_x = valid_x.reshape(-1,784)
test_x = test_x.reshape(-1,784)

4、特征数据归一化

train_x = tf.cast(train_x/255.0,tf.float32)
valid_x = tf.cast(valid_x/255.0,tf.float32)
test_x = tf.cast(test_x/255.0,tf.float32)

5、标签数据独热编码

#对标签数据进行独热编码
train_y = tf.one_hot(train_y,depth=10)
valid_y = tf.one_hot(valid_y,depth=10)
test_y = tf.one_hot(test_y,depth=10)

构建模型:全连接单隐含层神经网络

6、创建待优化变量

#定义第一层隐蔽层权重和偏置项变量
Input_Dim = 784
H1_NN = 64
W1=tf.Variable(tf.random.normal([Input_Dim,H1_NN],mean=0.0,stddev=1.0,dtype=tf.float32))

B1=tf.Variable(tf.zeros([H1_NN]),dtype=tf.float32)

Input_Dim = 10
W2=tf.Variable(tf.random.normal([H1_NN,Input_Dim],mean=0.0,stddev=1.0,dtype=tf.float32))

B2=tf.Variable(tf.zeros([Input_Dim]),dtype=tf.float32)

W=[W1,W2]
B=[B1,B2]

7、定义模型前向计算

def model(x,w,b):
    x = tf.matmul(x,w[0])+b[0]
    x = tf.nn.relu(x)
    x = tf.matmul(x,w[1])+b[1]

    pred = tf.nn.softmax(x)
    return pred

8、定义损失函数

def loss(x,y,w,b):
    pred = model(x,w,b)
    loss_ = tf.keras.losses.categorical_crossentropy(y_true=y,y_pred = pred)
    return tf.reduce_mean(loss_)

9、设置训练超参数

training_epochs=20#训练轮数
batch_size=50#单次训练样本数
learning_rate=0.01

10、定义梯度计算函数

def grad(x,y,w,b):
    with tf.GradientTape() as tape:
        loss_=loss(x,y,w,b)
    return tape.gradient(loss_, [w[0],b[0],w[1],b[1]])#返回梯度向量

11、选择优化器

optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate)

12、定义准确率

def accuracy(x,y,w,b):
    pred=model(x,w,b)
    correct_prediction = tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
    return tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

13、训练模型

steps = int(train_num / batch_size)  # 一轮训练有多少批次
 
loss_list_train = []  # 用于保存训练集loss值的列表
loss_list_valid = [] # 用于保存验证集loss值的列表
acc_list_train = []  # 用于保存训练集Acc值的列表
acc_list_valid = []  # 用于保存验证集Acc值的列表
 
for epoch in range(training_epochs):
    for step in range(steps):
        xs = train_x[step * batch_size:(step + 1) * batch_size]
        ys = train_y[step * batch_size:(step + 1) * batch_size]
        grads = grad(xs, ys, W, B)  # 计算梯度
        optimizer.apply_gradients(zip(grads, [W[0],B[0],W[1],B[1]]))  # 优化器根据梯度自动调整变量w和b
 
    loss_train = loss(train_x, train_y, W, B).numpy()  # 计算当前轮训练损失
    loss_valid = loss(valid_x, valid_y, W, B).numpy()  # 计算当前轮验证损失
    acc_train = accuracy(train_x, train_y,W,B).numpy()
    acc_valid = accuracy(valid_x, valid_y, W, B).numpy()
    loss_list_train.append(loss_train)
    loss_list_valid.append(loss_valid)
    acc_list_train.append(acc_train)
    acc_list_valid.append(acc_valid)
    print("epoch={:3d}, train_loss={:.4f}, train_acc={:.4f}, val_loss={:.4f}, val_acc={:.4f}".format(epoch + 1, loss_train, acc_train, loss_valid, acc_valid))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值