Tensorflow笔记【四】之搭建神经网络并对比

一、如何搭建神经网络

在搭建神经网络中,需要通过训练集训练搭建的神经网络,训练完成后需要通过验证集测试我们神经网络训练的效果。

总体流程如下图所示:
Tensorflow笔记【四】之搭建神经网络并对比_第1张图片

二、代码

代码流程:
1、获取数据【x:特征  y:标签】
2、将数据转为tf格式,并将特征和标签配对做batch
3、确定神经网络层数,并初始化神经网络参数w,b,学习率,轮次等
4、循环将数据开始训练。记录loss,每隔20步打印训练的loss信息
5、预测结果,将6*6,每0.01步长的数据喂入模型测试结果,
6、读入红蓝点,画出分割线,不包含正则化

① 单层神经网络

如果采用单层神经网络,没有激活函数,对于边界是非直线分类的问题,则无法拟合,且单层神经网络不收敛,无法找到边界线,
Tensorflow笔记【四】之搭建神经网络并对比_第2张图片

import tensorflow as tf
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

# 1、获取数据【x:特征  y:标签】
df = pd.read_csv('../class2/dot.csv')
x_data = np.array(df[['x1', 'x2']])
y_data = np.array(df['y_c'])

# 垂直(行)按顺序堆叠数组
x_train = np.vstack(x_data).reshape(-1, 2)
y_train = np.vstack(y_data).reshape(-1, 1)

Y_c = [['red' if y else 'blue'] for y in y_train]

# 2、将数据转为tf格式,并将特征和标签配对做batch
x_train = tf.cast(x_train, dtype=tf.float32)
y_train = tf.cast(y_train, dtype=tf.float32)
train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32)

# 3、确定神经网络层数,并初始化神经网络参数w,b,学习率,轮次等
# 如果是单层神经网络,
w1 = tf.Variable(tf.random.normal([2, 1]), dtype=tf.float32)
b1 = tf.Variable(tf.constant(0.01, shape=[1]))

lr = 0.005
epoch = 800

# 4、循环将数据开始训练。记录loss,每隔20步打印训练的loss信息
for epoch in range(epoch):
    for step, (x_train, y_train) in enumerate(train_db):
        with tf.GradientTape() as tape:
            y_ = tf.matmul(x_train, w1) + b1
            # y_ = tf.nn.relu(y_)

            loss = tf.reduce_mean(tf.square(y_train - y_))

        variables = [w1, b1]
        grads = tape.gradient(loss, variables)

        w1.assign_sub(lr * grads[0])
        b1.assign_sub(lr * grads[1])

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

# 5、预测结果,将6*6,每0.01步长的数据喂入模型测试结果,
print("*******predict*******")
# xx在-3到3之间以步长为0.01,yy在-3到3之间以步长0.01,生成间隔数值点
xx, yy = np.mgrid[-3:3:.1, -3:3:.1]
grid = np.c_[xx.ravel(), yy.ravel()]  # 连接两个矩阵
grid = tf.cast(grid, tf.float32)

probs = []

for x_test in grid:
    y_ = tf.matmul([x_test], w1) + b1
    # y_ = tf.nn.relu(y_)
    probs.append(y_)

x1 = x_data[:, 0]
x2 = x_data[:, 1]

probs = np.array(probs).reshape(xx.shape)
plt.scatter(x1, x2, color=np.squeeze(Y_c))

plt.contour(xx, yy, probs)
plt.show()

② 加入激活函数

如果加入激活函数relu函数,直线发生了弯曲,但仍不能拟合,同时该神经网络也不收敛,无法找到边界
Tensorflow笔记【四】之搭建神经网络并对比_第3张图片

# 1、获取数据【x:特征  y:标签】
df = pd.read_csv('../class2/dot.csv')
x_data = np.array(df[['x1', 'x2']])
y_data = np.array(df['y_c'])

# 垂直(行)按顺序堆叠数组
x_train = np.vstack(x_data).reshape(-1, 2)
y_train = np.vstack(y_data).reshape(-1, 1)

Y_c = [['red' if y else 'blue'] for y in y_train]

# 2、将数据转为tf格式,并将特征和标签配对做batch
x_train = tf.cast(x_train, dtype=tf.float32)
y_train = tf.cast(y_train, dtype=tf.float32)
train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32)

# 3、确定神经网络层数,并初始化神经网络参数w,b,学习率,轮次等
# 输入层为2个神经元,隐藏层为11个神经元,1层隐藏层,输出层为1个神经元
w1 = tf.Variable(tf.random.normal([2, 11]), dtype=tf.float32)
b1 = tf.Variable(tf.constant(0.01, shape=[11]))

w2 = tf.Variable(tf.random.normal([11, 1]), dtype=tf.float32)
b2 = tf.Variable(tf.constant(0.01, shape=[1]))

lr = 0.005
epoch = 800

# 4、循环将数据开始训练。记录loss,每隔20步打印训练的loss信息
for epoch in range(epoch):
    for step, (x_train, y_train) in enumerate(train_db):
        with tf.GradientTape() as tape:
            y_ = tf.matmul(x_train, w1) + b1
            # y_ = tf.nn.relu(y_)
            y_ = tf.matmul(y_, w2) + b2

            loss = tf.reduce_mean(tf.square(y_train - y_))

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

        w1.assign_sub(lr * grads[0])
        b1.assign_sub(lr * grads[1])
        w2.assign_sub(lr * grads[2])
        b2.assign_sub(lr * grads[3])

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

# 5、预测结果,将6*6,每0.01步长的数据喂入模型测试结果,
print("*******predict*******")
# xx在-3到3之间以步长为0.01,yy在-3到3之间以步长0.01,生成间隔数值点
xx, yy = np.mgrid[-3:3:.1, -3:3:.1]
grid = np.c_[xx.ravel(), yy.ravel()]  # 连接两个矩阵
grid = tf.cast(grid, tf.float32)

probs = []

for x_test in grid:
    y_ = tf.matmul([x_test], w1) + b1
    # y_ = tf.nn.relu(y_)
    y_ = tf.matmul(y_, w2) + b2
    probs.append(y_)

x1 = x_data[:, 0]
x2 = x_data[:, 1]
# 6、读入红蓝点,画出分割线,不包含正则化
probs = np.array(probs).reshape(xx.shape)
plt.scatter(x1, x2, color=np.squeeze(Y_c))

plt.contour(xx, yy, probs, levels=[.5])
plt.show()

③两层神经网络,中间无激活函数

使用两层神经网络,模型收敛,可以找到边界,但无法拟合,训练效果很差

Tensorflow笔记【四】之搭建神经网络并对比_第4张图片

④ 两层神经网络,中间添加激活函数

两层神经网络,增加激活函数,发生过拟合
Tensorflow笔记【四】之搭建神经网络并对比_第5张图片

# 1、获取数据【x:特征  y:标签】
df = pd.read_csv('../class2/dot.csv')
x_data = np.array(df[['x1', 'x2']])
y_data = np.array(df['y_c'])

# 垂直(行)按顺序堆叠数组
x_train = np.vstack(x_data).reshape(-1, 2)
y_train = np.vstack(y_data).reshape(-1, 1)

Y_c = [['red' if y else 'blue'] for y in y_train]

# 2、将数据转为tf格式,并将特征和标签配对做batch
x_train = tf.cast(x_train, dtype=tf.float32)
y_train = tf.cast(y_train, dtype=tf.float32)
train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32)

# 3、确定神经网络层数,并初始化神经网络参数w,b,学习率,轮次等
# 输入层为2个神经元,隐藏层为11个神经元,1层隐藏层,输出层为1个神经元
w1 = tf.Variable(tf.random.normal([2, 11]), dtype=tf.float32)
b1 = tf.Variable(tf.constant(0.01, shape=[11]))

w2 = tf.Variable(tf.random.normal([11, 1]), dtype=tf.float32)
b2 = tf.Variable(tf.constant(0.01, shape=[1]))

lr = 0.005
epoch = 800

# 4、循环将数据开始训练。记录loss,每隔20步打印训练的loss信息
for epoch in range(epoch):
    for step, (x_train, y_train) in enumerate(train_db):
        with tf.GradientTape() as tape:
            y_ = tf.matmul(x_train, w1) + b1
            # 增加激活函数
            y_ = tf.nn.relu(y_)
            y_ = tf.matmul(y_, w2) + b2

            loss = tf.reduce_mean(tf.square(y_train - y_))

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

        w1.assign_sub(lr * grads[0])
        b1.assign_sub(lr * grads[1])
        w2.assign_sub(lr * grads[2])
        b2.assign_sub(lr * grads[3])

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

# 5、预测结果,将6*6,每0.01步长的数据喂入模型测试结果,
print("*******predict*******")
# xx在-3到3之间以步长为0.01,yy在-3到3之间以步长0.01,生成间隔数值点
xx, yy = np.mgrid[-3:3:.1, -3:3:.1]
grid = np.c_[xx.ravel(), yy.ravel()]  # 连接两个矩阵
grid = tf.cast(grid, tf.float32)

probs = []

for x_test in grid:
    y_ = tf.matmul([x_test], w1) + b1
    # 增加激活函数
    y_ = tf.nn.relu(y_)
    y_ = tf.matmul(y_, w2) + b2
    probs.append(y_)

x1 = x_data[:, 0]
x2 = x_data[:, 1]
# 6、读入红蓝点,画出分割线,不包含正则化
probs = np.array(probs).reshape(xx.shape)
plt.scatter(x1, x2, color=np.squeeze(Y_c))

plt.contour(xx, yy, probs, levels=[.5])
plt.show()

⑤ 两层神经网络,添加激活函数,正则化优化

缓解了过拟合现象
Tensorflow笔记【四】之搭建神经网络并对比_第6张图片

# 1、获取数据【x:特征  y:标签】
df = pd.read_csv('../class2/dot.csv')
x_data = np.array(df[['x1', 'x2']])
y_data = np.array(df['y_c'])

# 垂直(行)按顺序堆叠数组
x_train = np.vstack(x_data).reshape(-1, 2)
y_train = np.vstack(y_data).reshape(-1, 1)

Y_c = [['red' if y else 'blue'] for y in y_train]

# 2、将数据转为tf格式,并将特征和标签配对做batch
x_train = tf.cast(x_train, dtype=tf.float32)
y_train = tf.cast(y_train, dtype=tf.float32)
train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32)

# 3、确定神经网络层数,并初始化神经网络参数w,b,学习率,轮次等
# 输入层为2个神经元,隐藏层为11个神经元,1层隐藏层,输出层为1个神经元
w1 = tf.Variable(tf.random.normal([2, 11]), dtype=tf.float32)
b1 = tf.Variable(tf.constant(0.01, shape=[11]))

w2 = tf.Variable(tf.random.normal([11, 1]), dtype=tf.float32)
b2 = tf.Variable(tf.constant(0.01, shape=[1]))

lr = 0.005
epoch = 800

# 4、循环将数据开始训练。记录loss,每隔20步打印训练的loss信息
for epoch in range(epoch):
    for step, (x_train, y_train) in enumerate(train_db):
        with tf.GradientTape() as tape:
            h1 = tf.matmul(x_train, w1) + b1
            # 增加激活函数
            h1 = tf.nn.relu(h1)
            y_ = tf.matmul(h1, w2) + b2

            loss_mse = tf.reduce_mean(tf.square(y_train - y_))

            # 添加l2正则化
            loss_regularization = []
            # tf.nn.l2_loss(w)=sum(w ** 2) / 2
            loss_regularization.append(tf.nn.l2_loss(w1))
            loss_regularization.append(tf.nn.l2_loss(w2))
            # 求和
            # 例:x=tf.constant(([1,1,1],[1,1,1]))
            #   tf.reduce_sum(x)
            # >>>6
            loss_regularization = tf.reduce_sum(loss_regularization)
            loss = loss_mse + 0.03 * loss_regularization

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

        w1.assign_sub(lr * grads[0])
        b1.assign_sub(lr * grads[1])
        w2.assign_sub(lr * grads[2])
        b2.assign_sub(lr * grads[3])

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

# 5、预测结果,将6*6,每0.01步长的数据喂入模型测试结果,
print("*******predict*******")
# xx在-3到3之间以步长为0.01,yy在-3到3之间以步长0.01,生成间隔数值点
xx, yy = np.mgrid[-3:3:.1, -3:3:.1]
grid = np.c_[xx.ravel(), yy.ravel()]  # 连接两个矩阵
grid = tf.cast(grid, tf.float32)

probs = []

for x_test in grid:
    h1 = tf.matmul([x_test], w1) + b1
    # 增加激活函数
    h1 = tf.nn.relu(h1)
    y_ = tf.matmul(h1, w2) + b2
    probs.append(y_)

x1 = x_data[:, 0]
x2 = x_data[:, 1]
# 6、读入红蓝点,画出分割线,不包含正则化
probs = np.array(probs).reshape(xx.shape)
plt.scatter(x1, x2, color=np.squeeze(Y_c))

plt.contour(xx, yy, probs, levels=[.5])
plt.show()

你可能感兴趣的:(深度学习,tensorflow,神经网络,python)