1.使用 Numpy 编写一个神经网络,并开发 BP 算法。两个隐层,第 一个隐层 4 个神经元,第二个隐层 8 个神经元,输入是 4 个神经 元,输出是 2 个神经元。激活函数用 ReLu。并对 data.csv 数据集进 行训练和测试,计算精度。
时间: 2023-11-27 12:50:55 浏览: 57
神经网络理论实验:使用numpy创建单个神经元,利用BP算法进行学习。.zip
好的,我们可以按照下面的步骤来实现这个神经网络和 BP 算法:
1. 导入必要的包和数据集
2. 初始化权重和偏置
3. 定义 ReLU 激活函数
4. 定义前向传播函数
5. 定义损失函数
6. 定义反向传播函数
7. 定义梯度下降函数
8. 训练模型并计算精度
首先,我们需要导入必要的库和数据集:
```python
import numpy as np
import pandas as pd
data = pd.read_csv('data.csv')
X = data.iloc[:, :-2].values
y = data.iloc[:, -2:].values
```
接下来,我们需要初始化权重和偏置:
```python
def initialize_parameters():
np.random.seed(1)
W1 = np.random.randn(4, 4) * 0.01
b1 = np.zeros((4, 1))
W2 = np.random.randn(8, 4) * 0.01
b2 = np.zeros((8, 1))
W3 = np.random.randn(2, 8) * 0.01
b3 = np.zeros((2, 1))
parameters = {"W1": W1,
"b1": b1,
"W2": W2,
"b2": b2,
"W3": W3,
"b3": b3}
return parameters
```
然后,我们定义 ReLU 激活函数:
```python
def relu(Z):
A = np.maximum(0,Z)
return A
```
接着,我们定义前向传播函数,将数据集输入到神经网络中,进行前向传播计算:
```python
def forward_propagation(X, parameters):
# retrieve parameters
W1 = parameters['W1']
b1 = parameters['b1']
W2 = parameters['W2']
b2 = parameters['b2']
W3 = parameters['W3']
b3 = parameters['b3']
# layer 1
Z1 = np.dot(W1, X) + b1
A1 = relu(Z1)
# layer 2
Z2 = np.dot(W2, A1) + b2
A2 = relu(Z2)
# output layer
Z3 = np.dot(W3, A2) + b3
A3 = sigmoid(Z3)
cache = {"A1": A1,
"Z1": Z1,
"A2": A2,
"Z2": Z2,
"A3": A3,
"Z3": Z3}
return A3, cache
```
接下来,我们定义损失函数,计算预测值与真实值之间的误差:
```python
def compute_cost(A3, Y):
m = Y.shape[1]
cost = (-1/m) * np.sum(np.multiply(Y, np.log(A3)) + np.multiply(1 - Y, np.log(1 - A3)))
return cost
```
然后,我们定义反向传播函数,计算每一层的梯度:
```python
def backward_propagation(parameters, cache, X, Y):
m = X.shape[1]
# retrieve parameters
W1 = parameters['W1']
W2 = parameters['W2']
W3 = parameters['W3']
# retrieve cache
A1 = cache['A1']
A2 = cache['A2']
A3 = cache['A3']
# output layer
dZ3 = A3 - Y
dW3 = (1/m) * np.dot(dZ3, A2.T)
db3 = (1/m) * np.sum(dZ3, axis=1, keepdims=True)
# layer 2
dZ2 = np.multiply(np.dot(W3.T, dZ3), relu_derivative(A2))
dW2 = (1/m) * np.dot(dZ2, A1.T)
db2 = (1/m) * np.sum(dZ2, axis=1, keepdims=True)
# layer 1
dZ1 = np.multiply(np.dot(W2.T, dZ2), relu_derivative(A1))
dW1 = (1/m) * np.dot(dZ1, X.T)
db1 = (1/m) * np.sum(dZ1, axis=1, keepdims=True)
gradients = {"dW1": dW1,
"db1": db1,
"dW2": dW2,
"db2": db2,
"dW3": dW3,
"db3": db3}
return gradients
```
接下来,我们定义梯度下降函数,用来更新参数:
```python
def update_parameters(parameters, gradients, learning_rate):
# retrieve parameters
W1 = parameters['W1']
b1 = parameters['b1']
W2 = parameters['W2']
b2 = parameters['b2']
W3 = parameters['W3']
b3 = parameters['b3']
# retrieve gradients
dW1 = gradients['dW1']
db1 = gradients['db1']
dW2 = gradients['dW2']
db2 = gradients['db2']
dW3 = gradients['dW3']
db3 = gradients['db3']
# update parameters
W1 = W1 - learning_rate * dW1
b1 = b1 - learning_rate * db1
W2 = W2 - learning_rate * dW2
b2 = b2 - learning_rate * db2
W3 = W3 - learning_rate * dW3
b3 = b3 - learning_rate * db3
parameters = {"W1": W1,
"b1": b1,
"W2": W2,
"b2": b2,
"W3": W3,
"b3": b3}
return parameters
```
最后,我们定义一个训练模型的函数,并使用 data.csv 数据集进行训练和测试:
```python
def train(X, y, n_h1, n_h2, num_iterations, learning_rate):
n_x = X.shape[0]
n_y = y.shape[0]
# initialize parameters
parameters = initialize_parameters()
for i in range(num_iterations):
# forward propagation
A3, cache = forward_propagation(X, parameters)
# compute cost
cost = compute_cost(A3, y)
# backward propagation
gradients = backward_propagation(parameters, cache, X, y)
# update parameters
parameters = update_parameters(parameters, gradients, learning_rate)
if i % 1000 == 0:
print("Cost after iteration {}: {}".format(i, cost))
# predict labels for training set
predictions, _ = forward_propagation(X, parameters)
predictions = np.round(predictions)
accuracy = np.mean(predictions == y)
print("Training accuracy: {}".format(accuracy))
return parameters
```
现在我们可以开始训练模型了:
```python
parameters = train(X.T, y.T, n_h1=4, n_h2=8, num_iterations=10000, learning_rate=0.01)
```
最后,我们可以计算模型在数据集上的精度:
```python
# predict labels for test set
test_data = np.array([[0.5, 0.5, 0.5, 0.5], [0.1, 0.2, 0.3, 0.4]])
predictions, _ = forward_propagation(test_data.T, parameters)
predictions = np.round(predictions)
print("Test predictions: {}".format(predictions.T))
```
以上就是使用 Numpy 编写的神经网络和 BP 算法,在 data.csv 数据集上的训练和测试。
阅读全文