误差反向传播法的实现 输出使用后4000个样本训练得到模型在测试集上的结果,参数设置为:batch:300;iters_num:2000;lambda:0.1
时间: 2023-11-28 09:47:18 浏览: 72
误差反向传播法(Backpropagation)是一种常用的神经网络训练方法,它通过计算神经网络输出与真实标签之间的误差,然后根据误差大小来更新神经网络中的参数。
具体实现步骤如下:
1. 初始化神经网络参数,包括权重和偏置。
2. 从训练数据中随机选择一批样本(batch),将这些样本输入神经网络,计算输出结果。
3. 计算输出结果与真实标签之间的误差,使用误差函数(如均方误差)来衡量误差大小。
4. 根据误差大小来计算梯度,即误差对于每个参数的偏导数。
5. 使用梯度下降法来更新神经网络中的参数,即将每个参数的值减去对应的学习率乘以梯度。
6. 重复步骤2-5,直到达到指定的迭代次数(iters_num)或误差达到指定的阈值。
代码实现如下:
```
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_grad(x):
return (1 - sigmoid(x)) * sigmoid(x)
class TwoLayerNet:
def __init__(self, input_size, hidden_size, output_size):
self.params = {}
self.params['W1'] = 0.01 * np.random.randn(input_size, hidden_size)
self.params['b1'] = np.zeros(hidden_size)
self.params['W2'] = 0.01 * np.random.randn(hidden_size, output_size)
self.params['b2'] = np.zeros(output_size)
def predict(self, x):
W1, b1, W2, b2 = self.params['W1'], self.params['b1'], self.params['W2'], self.params['b2']
z1 = np.dot(x, W1) + b1
a1 = sigmoid(z1)
z2 = np.dot(a1, W2) + b2
y = z2
return y
def loss(self, x, t):
y = self.predict(x)
loss = np.mean((y - t) ** 2) + 0.5 * lambda_reg * (np.sum(self.params['W1'] ** 2) + np.sum(self.params['W2'] ** 2))
return loss
def accuracy(self, x, t):
y = self.predict(x)
accuracy = np.mean((y > 0.5) == (t == 1)) * 100
return accuracy
def numerical_gradient(self, x, t):
h = 1e-4
grads = {}
for param_name in self.params:
param = self.params[param_name]
grad = np.zeros_like(param)
for i in range(param.shape[0]):
for j in range(param.shape[1]):
tmp_val = param[i,j]
param[i,j] = tmp_val + h
f1 = self.loss(x, t)
param[i,j] = tmp_val - h
f2 = self.loss(x, t)
grad[i,j] = (f1 - f2) / (2 * h)
param[i,j] = tmp_val
grads[param_name] = grad
return grads
def gradient(self, x, t):
W1, b1, W2, b2 = self.params['W1'], self.params['b1'], self.params['W2'], self.params['b2']
grads = {}
batch_num = x.shape[0]
# forward
z1 = np.dot(x, W1) + b1
a1 = sigmoid(z1)
z2 = np.dot(a1, W2) + b2
y = z2
# backward
delta2 = y - t
grads['W2'] = np.dot(a1.T, delta2)
grads['b2'] = np.sum(delta2, axis=0)
delta1 = np.dot(delta2, W2.T) * sigmoid_grad(z1)
grads['W1'] = np.dot(x.T, delta1)
grads['b1'] = np.sum(delta1, axis=0)
# add regularization
grads['W2'] += lambda_reg * W2
grads['W1'] += lambda_reg * W1
return grads
def fit(self, x_train, y_train, x_test, y_test, batch_size=100, epochs=10, learning_rate=0.1, lambda_reg=0.1):
self.lambda_reg = lambda_reg
train_loss_list = []
train_acc_list = []
test_acc_list = []
train_size = x_train.shape[0]
iter_per_epoch = max(train_size / batch_size, 1)
for epoch in range(epochs):
perm = np.random.permutation(train_size)
for i in range(0, train_size, batch_size):
x_batch = x_train[perm[i:i+batch_size]]
y_batch = y_train[perm[i:i+batch_size]]
grads = self.gradient(x_batch, y_batch)
for param_name in self.params:
self.params[param_name] -= learning_rate * grads[param_name]
train_loss = self.loss(x_train, y_train)
train_loss_list.append(train_loss)
train_acc = self.accuracy(x_train, y_train)
train_acc_list.append(train_acc)
test_acc = self.accuracy(x_test, y_test)
test_acc_list.append(test_acc)
print("epoch: %d, train_loss: %f, train_acc: %f, test_acc: %f" % (epoch+1, train_loss, train_acc, test_acc))
return train_loss_list, train_acc_list, test_acc_list
# 读取数据
x_train = np.load('x_train.npy')
y_train = np.load('y_train.npy')
x_test = np.load('x_test.npy')
y_test = np.load('y_test.npy')
# 构建神经网络模型
input_size = x_train.shape[1]
hidden_size = 100
output_size = 1
net = TwoLayerNet(input_size, hidden_size, output_size)
# 训练神经网络模型
batch_size = 300
iters_num = 2000
lambda_reg = 0.1
train_loss_list, train_acc_list, test_acc_list = net.fit(x_train, y_train, x_test, y_test, batch_size, iters_num, lambda_reg)
# 输出测试集结果
test_acc = net.accuracy(x_test, y_test)
print("Test accuracy: %f" % test_acc)
```
在输出后4000个样本训练得到模型在测试集上的结果时,我们只需要将训练集中的前4000个样本用来训练模型,然后再使用测试集来测试模型的准确率即可。具体代码如下:
```
# 取出训练集中的前4000个样本
x_train = x_train[:4000]
y_train = y_train[:4000]
# 训练神经网络模型
batch_size = 300
iters_num = 2000
lambda_reg = 0.1
train_loss_list, train_acc_list, test_acc_list = net.fit(x_train, y_train, x_test, y_test, batch_size, iters_num, lambda_reg)
# 输出测试集结果
test_acc = net.accuracy(x_test, y_test)
print("Test accuracy: %f" % test_acc)
```
阅读全文