写一段神经网络、反向传播的代码,完整的
时间: 2023-05-25 11:06:49 浏览: 45
//神经网络反向传播的代码:import numpy as np#定义sigmoid函数
def sigmoid(x):
return 1/(1+np.exp(-x))#定义预测函数
def predict(features, weights):
z = np.dot(features, weights)
return sigmoid(z)#定义损失函数
def loss(features, labels, weights):
observations = len(labels)
predictions = predict(features, weights)
class1_cost = -labels*np.log(predictions)
class2_cost = (1-labels)*np.log(1-predictions)
cost = class1_cost - class2_cost
cost = cost.sum()/observations
return cost#定义梯度下降函数
def update_weights(features, labels, weights, lr):
observations = len(labels)
predictions = predict(features, weights)
derivative = np.dot(features.T, labels - predictions)
weights -= lr * derivative/observations
return weights#定义训练函数
def train(features, labels, weights, lr, iters):
cost_history = []
for i in range(iters):
weights = update_weights(features, labels, weights, lr)
cost = loss(features, labels, weights)
cost_history.append(cost)
return weights, cost_history
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)