全连接神经网络代码分段详解
时间: 2023-07-29 12:09:13 浏览: 171
全连接神经网络是一种常见的人工神经网络,也被称为多层感知机(Multilayer Perceptron,简称 MLP)。它由多个全连接层组成,每个全连接层的神经元与前一层的所有神经元相连。以下是一个Python实现的全连接神经网络代码分段详解:
1.导入必要的库和模块
```python
import numpy as np
import matplotlib.pyplot as plt
```
2.定义激活函数
```python
def relu(x):
return np.maximum(x, 0)
```
3.初始化权重和偏置
```python
def initialize_parameters(layer_dims):
parameters = {}
L = len(layer_dims)
for l in range(1, L):
parameters['W' + str(l)] = np.random.randn(layer_dims[l], layer_dims[l-1]) * 0.01
parameters['b' + str(l)] = np.zeros((layer_dims[l], 1))
return parameters
```
4.前向传播
```python
def forward_propagation(X, parameters):
caches = []
A = X
L = len(parameters) // 2
for l in range(1, L):
A_prev = A
Z = np.dot(parameters['W' + str(l)], A_prev) + parameters['b' + str(l)]
A = relu(Z)
cache = (A_prev, parameters['W' + str(l)], parameters['b' + str(l)], Z)
caches.append(cache)
AL = np.dot(parameters['W' + str(L)], A) + parameters['b' + str(L)]
return AL, caches
```
5.计算代价函数
```python
def compute_cost(AL, Y):
m = Y.shape[1]
cost = (-1/m) * np.sum(Y*np.log(AL) + (1-Y)*np.log(1-AL))
cost = np.squeeze(cost)
return cost
```
6.反向传播
```python
def backward_propagation(AL, Y, caches):
grads = {}
L = len(caches)
m = AL.shape[1]
dAL = - (np.divide(Y, AL) - np.divide(1 - Y, 1 - AL))
current_cache = caches[L-1]
grads['dA' + str(L)], grads['dW' + str(L)], grads['db' + str(L)] = linear_activation_backward(dAL, current_cache, 'sigmoid')
for l in reversed(range(L-1)):
current_cache = caches[l]
dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads['dA' + str(l+2)], current_cache, 'relu')
grads['dA' + str(l+1)] = dA_prev_temp
grads['dW' + str(l+1)] = dW_temp
grads['db' + str(l+1)] = db_temp
return grads
```
7.更新参数
```python
def update_parameters(parameters, grads, learning_rate):
L = len(parameters) // 2
for l in range(L):
parameters['W' + str(l+1)] = parameters['W' + str(l+1)] - learning_rate * grads['dW' + str(l+1)]
parameters['b' + str(l+1)] = parameters['b' + str(l+1)] - learning_rate * grads['db' + str(l+1)]
return parameters
```
8.训练模型
```python
def nn_model(X, Y, layer_dims, num_iterations, learning_rate):
parameters = initialize_parameters(layer_dims)
costs = []
for i in range(num_iterations):
AL, caches = forward_propagation(X, parameters)
cost = compute_cost(AL, Y)
grads = backward_propagation(AL, Y, caches)
parameters = update_parameters(parameters, grads, learning_rate)
if i % 100 == 0:
print("Cost after iteration {}: {}".format(i, cost))
costs.append(cost)
plt.plot(costs)
plt.ylabel('Cost')
plt.xlabel('Iterations (per hundreds)')
plt.title("Learning rate =" + str(learning_rate))
plt.show()
return parameters
```
以上就是一个简单的全连接神经网络的代码分段详解。其中,initialize_parameters函数用于初始化权重和偏置,forward_propagation函数用于进行前向传播,compute_cost函数用于计算代价函数,backward_propagation函数用于进行反向传播,update_parameters函数用于更新参数,nn_model函数用于训练模型。
阅读全文