利用矩阵运算,快速实现单隐层,sigmoid激活函数BP神经网络,下面是源码
# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
class bpNN:
def __init__(self, numH, numO, learning_rate, n_iteration):
self.numH = numH
self.numO = numO
self.learning_rate = learning_rate
self.n_iteration = n_iteration
self.weight_H = 0
self.b_H = 0
self.weight_O = 0
self.b_O = 0
def __ini_weight_b(self, x): #初始化权重和偏置
w1 = np.random.random([self.numH, x.shape[0]]) * 0.01
b1 = np.zeros([self.numH, 1])
w2 = np.random.random([self.numO, self.numH]) * 0.01
b2 = np.zeros([self.numO, 1])
return w1, b1, w2, b2
def sigmoid(self, value): #sigmoid函数
y =