用numpy搭建三层全连接神经网络
时间: 2024-12-12 21:12:01 浏览: 14
好的,我很乐意为您介绍如何使用numpy搭建三层全连接神经网络。以下是具体步骤:
1. 导入必要的库:
```python
import numpy as np
```
2. 定义激活函数及其导数:
```python
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return sigmoid(x) * (1 - sigmoid(x))
```
3. 初始化网络参数:
```python
input_layer_size = 3 # 输入层神经元数量
hidden_layer_size = 5 # 隐藏层神经元数量
output_layer_size = 1 # 输出层神经元数量
np.random.seed(42)
W1 = np.random.randn(input_layer_size, hidden_layer_size)
b1 = np.zeros((1, hidden_layer_size))
W2 = np.random.randn(hidden_layer_size, output_layer_size)
b2 = np.zeros((1, output_layer_size))
```
4. 定义前向传播:
```python
def forward_propagation(X):
Z1 = np.dot(X, W1) + b1
A1 = sigmoid(Z1)
Z2 = np.dot(A1, W2) + b2
A2 = sigmoid(Z2)
cache = (Z1, A1, Z2, A2)
return A2, cache
```
5. 定义损失函数:
```python
def compute_loss(Y_hat, Y):
m = Y.shape[0]
logprobs = np.multiply(np.log(Y_hat), Y) + np.multiply((1 - Y), np.log(1 - Y_hat))
loss = -np.sum(logprobs) / m
return loss
```
6. 定义反向传播:
```python
def backward_propagation(X, Y, cache):
m = X.shape[0]
Z1, A1, Z2, A2 = cache
dZ2 = A2 - Y
dW2 = np.dot(A1.T, dZ2) / m
db2 = np.sum(dZ2, axis=0, keepdims=True) / m
dA1 = np.dot(dZ2, W2.T)
dZ1 = dA1 * sigmoid_derivative(Z1)
dW1 = np.dot(X.T, dZ1) / m
db1 = np.sum(dZ1, axis=0, keepdims=True) / m
grads = {"dW1": dW1, "db1": db1, "dW2": dW2, "db2": db2}
return grads
```
7. 定义更新参数函数:
```python
def update_parameters(grads, learning_rate):
W1 -= learning_rate * grads["dW1"]
b1 -= learning_rate * grads["db1"]
W2 -= learning_rate * grads["dW2"]
b2 -= learning_rate * grads["db2"]
```
8. 训练模型:
```python
def train(X, Y, iterations=10000, learning_rate=0.01):
for i in range(iterations):
Y_hat, cache = forward_propagation(X)
loss = compute_loss(Y_hat, Y)
grads = backward_propagation(X, Y, cache)
update_parameters(grads, learning_rate)
if i % 1000 == 0:
print(f"Iteration {i}, Loss: {loss:.4f}")
```
9. 使用模型进行预测:
```python
def predict(X):
Y_hat, _ = forward_propagation(X)
return Y_hat > 0.5
```
通过以上步骤,我们就使用numpy搭建了一个简单的三层全连接神经网络。这个网络包含一个输入层,一个隐藏层和一个输出层。
阅读全文