天天看点

tensorflow单层神经网络实现手写数字识别部分重要代码完整代码

目录

  • 部分重要代码
    • 1.构建单层神经网络模型
    • 2.待优化列表
    • 3.定义模型和方法
    • 4.预测
  • 完整代码

此篇博客与上一博客 手写数字识别 类似,只是稍微改变了模型,原文最后会附上完整代码。

这篇博客仍然感谢大佬的指导,我只是大佬的搬运工,大佬原文博客如下:单层神经网络实现手写识别

部分重要代码

1.构建单层神经网络模型

注意:全连接只能接收一维的输入,本次使用的图片是28*28
# 构建一层神经网络模型
# 定义第一层隐藏层权重和偏执项变量
Input_Dim = 784 # 全连接只能接收一维的输入,图片是28*28
H1_NN = 64 # 这个参数可以随意设置
W1 = tf.Variable(tf.random.normal(shape=(Input_Dim, H1_NN)), dtype=tf.float32) # shape为初始值,形状维度要匹配上
B1 = tf.Variable(tf.zeros(H1_NN), dtype=tf.float32)

# 定义输出层权重和偏执项变量
Output_Dim = 10
W2 = tf.Variable(tf.random.normal(shape=(H1_NN,Output_Dim)),dtype=tf.float32)
B2 = tf.Variable(tf.zeros(Output_Dim),dtype=tf.float32)
           

2.待优化列表

层数越多参数越多,参数都要写在这个列表里
# 待优化列表
# 层数越多参数越多,都要写在这个列表里
W = [W1, W2]
B = [B1, B2]
           

3.定义模型和方法

# 定义模型的前向计算
def model(w, x, b):
  x = tf.matmul(x, w[0]) + b[0] # 先运算再激活再运算
  x = tf.nn.relu(x) # 激活
  x = tf.matmul(x, w[1]) + b[1]
  return tf.nn.softmax(x)

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

# 准确率
def accuracy(w,x,y,b):
  pred = model(w,x,b)
  acc = tf.equal(tf.argmax(pred,axis=1),tf.argmax(y,axis=1))
  return tf.reduce_mean(tf.cast(acc,dtype=tf.float32))

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

4.预测

# 预测
def predict(x, w, b):
  pred = model(w, x, b)
  pred_ = tf.argmax(pred, axis=1)
  return pred_

import numpy as np
id = np.random.randint(0,len(test_x)) # 随机生成一个验证id
# 预测值
pred = predict(test_x,W,B)[id]
# 真实值
true = test_labels[id]
print("真实值为:", true)
print("预测值值为:", pred.numpy())
           

完整代码

import tensorflow as tf

# 导入数据集
mnist = tf.keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# 展示图片
import matplotlib.pyplot as plt

def plot_image(image):
  plt.imshow(image, cmap='binary')
  plt.show()

plot_image(train_images[0])

# 划分数据集
total_num = len(train_images)
split_valid = 0.2
train_num = int((total_num * (1-split_valid)))

# 数据集划分为训练集和验证集
# 训练集
train_x = train_images[:train_num]
train_y = train_labels[:train_num]
# 验证集
valid_x = train_images[train_num:]
valid_y = train_labels[train_num:]
# 测试集
test_x = test_images
test_y = test_labels

# 数据塑形+归一化
train_x = tf.cast(train_x.reshape(-1, 28*28)/255.0,dtype=tf.float32)
valid_x = tf.cast(valid_x.reshape(-1, 28*28)/255.0,dtype=tf.float32)
test_x = tf.cast(test_x.reshape(-1, 28*28)/255.0,dtype=tf.float32)

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

# 构建一层神经网络模型
# 定义第一层隐藏层权重和偏执项变量
Input_Dim = 784 # 全连接只能接收一维的输入,图片是28*28
H1_NN = 64 # 这个参数可以随意设置
W1 = tf.Variable(tf.random.normal(shape=(Input_Dim, H1_NN)), dtype=tf.float32) # shape为初始值,形状维度要匹配上
B1 = tf.Variable(tf.zeros(H1_NN), dtype=tf.float32)

# 定义输出层权重和偏执项变量
Output_Dim = 10
W2 = tf.Variable(tf.random.normal(shape=(H1_NN,Output_Dim)),dtype=tf.float32)
B2 = tf.Variable(tf.zeros(Output_Dim),dtype=tf.float32)

# 待优化列表
# 层数越多参数越多,都要写在这个列表里
W = [W1, W2]
B = [B1, B2]

# 定义模型的前向计算
def model(w, x, b):
  x = tf.matmul(x, w[0]) + b[0] # 先运算再激活再运算
  x = tf.nn.relu(x) # 激活
  x = tf.matmul(x, w[1]) + b[1]
  return tf.nn.softmax(x)

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

# 准确率
def accuracy(w,x,y,b):
  pred = model(w,x,b)
  acc = tf.equal(tf.argmax(pred,axis=1),tf.argmax(y,axis=1))
  return tf.reduce_mean(tf.cast(acc,dtype=tf.float32))

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

# 定义超参数
train_epochs = 20
learning_rate = 0.01
batch_size = 50

total_steps = train_num // batch_size
train_loss_list = []
valid_loss_list = []
trian_acc_list = []
valide_acc_list = []

# 优化器
optimizer = tf.optimizers.Adam(learning_rate=learning_rate)

# 开始神经网络
for epoch in range(train_epochs):
  for step in range(total_steps):
    xs = train_x[step*batch_size:(step+1)*batch_size]
    ys = train_y[step*batch_size:(step+1)*batch_size]
    grads = grad(W, xs, ys, B)
    optimizer.apply_gradients(zip(grads,[W[0],B[0],W[1],B[1]])) # 优化器优化
  trian_loss = loss(W, train_x, train_y, B).numpy()
  valid_loss = loss(W, valid_x, valid_y, B).numpy()
  train_accuracy = accuracy(W, train_x, train_y, B).numpy()
  valid_accuracy = accuracy(W, valid_x, valid_y, B).numpy()
  trian_acc_list.append(train_accuracy)
  valide_acc_list.append(valid_accuracy)
  train_loss_list.append(trian_loss)
  valid_loss_list.append(valid_loss)
  print(
    f'{epoch + 1}:trian_loss:{trian_loss}valid_loss:{valid_loss}train_accuracy:{train_accuracy}valid_accuracy:{valid_accuracy}')

# 损失图像
plt.plot(train_loss_list,'r')
plt.plot(valid_loss_list,'b')

# 准确率图像
plt.plot(trian_acc_list,'r')
plt.plot(valide_acc_list,'b')

# 预测
def predict(x, w, b):
  pred = model(w, x, b)
  pred_ = tf.argmax(pred, axis=1)
  return pred_

import numpy as np
id = np.random.randint(0,len(test_x)) # 随机生成一个验证id
# 预测值
pred = predict(test_x,W,B)[id]
# 真实值
true = test_labels[id]
print("真实值为:", true)
print("预测值值为:", pred.numpy())

           
tensorflow单层神经网络实现手写数字识别部分重要代码完整代码
tensorflow单层神经网络实现手写数字识别部分重要代码完整代码
tensorflow单层神经网络实现手写数字识别部分重要代码完整代码
tensorflow单层神经网络实现手写数字识别部分重要代码完整代码

再次感谢大佬!

深度学习真有意思希望我以后也这么觉得哈哈哈哈哈哈哈哈哈

继续阅读