栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

手写神经网络及反向传播推导-python 版

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

手写神经网络及反向传播推导-python 版

# 訓練神经网络进行性别的判定
import numpy as np
data = np.array([
  [-2, -1],  # Alice
  [25, 6],   # Bob
  [17, 4],   # Charlie
  [-15, -6], # Diana
])
all_y_trues = np.array([
  1, # Alice
  0, # Bob
  0, # Charlie
  1, # Diana
])
# 激活函数
def sigmoid(x):
    return 1/(1+np.exp(-x))

def deriv_sigmoid(x):
    fx = sigmoid(x)
    return fx * (1 - fx)
def loss_mse(y_true, y_pred):
# 均方误差 损失函数
    return  ((y_true - y_pred) ** 2).mean()
    
class Neural(object):
    def __init__(self):
        
        self.w1 = np.random.normal()
        self.w2 = np.random.normal()
        self.w3 = np.random.normal()
        self.w4 = np.random.normal()
        self.w5 = np.random.normal()
        self.w6 = np.random.normal()

        # 截距项,Biases
        self.b1 = np.random.normal()
        self.b2 = np.random.normal()
        self.b3 = np.random.normal()
        self.rate = 0.1
    def feed_forward(self, x):
        # 正向传播, 用于评估 损失函数
        h1 = sigmoid(self.w1 * x[0] + self.w2 * x[1] + self.b1)
        h2 = sigmoid(self.w3 * x[0] + self.w4 * x[1] + self.b2)
        o1 = sigmoid((self.w5 * h1 + self.w6 * h2 + self.b3))
        return o1
    def train(self, data, y_labels):
        learn_rate = 0.1
        epochs = 1000
        # 每次训练,将先正向传播,在计算损失函数, 然后反向传播,更新参数, ,在正向传播 目的就是为了降低损失函数的值
        for i in range(epochs):
            for (x, y_label) in zip(data, y_labels):
            #1、正向传播
                sum_h1 = self.w1 * x[0] + self.w2 * x[1] + self.b1
                h1 = sigmoid(sum_h1)

                sum_h2 = self.w3 * x[0] + self.w4 * x[1] + self.b2
                h2 = sigmoid(sum_h2)

                sum_o1 = self.w5 * h1 + self.w6 * h2 + self.b3
                o1 = sigmoid(sum_o1)
                
            #2、计算损失函数
                # 损失函数求导 1/n(y_true - y_pred) ** 2 = 0 + (-1) * 2 * (y_true - y_pred)
                dL_j = -2 * (y_label - o1)
            #3、 反向传播
                #dw1... dw6 
                #求j 的值
                #j = f(w5h1 + w6h2 + b3)
                #dj/dw5 = h1 * f' dj/dw6 = h2 * f'
                #dL/w5 = dL/j * dj/w5
                dl_w5 = dL_j * h1 * deriv_sigmoid(sum_o1)
                dl_w6 = dL_j * h2 * deriv_sigmoid(sum_o1)
                dl_b3 = dL_j * deriv_sigmoid(sum_o1)
                #dl/h1 = dL/dj * dj/dh1 = da_j * w5
                dl_h1 = dL_j * self.w5 * deriv_sigmoid(sum_o1)
                dl_h2 = dL_j * self.w6 * deriv_sigmoid(sum_o1)
                
                #h1 = f(w1* x1 + w2 * x2 + b1)
                #h2 = f(w3 * x1 + w4 * x2 + b2)
                #dl/w1 = dl/j * dj/h1 * dh1/dw1
                dl_w1 = dL_j * dl_h1 * x[0] * deriv_sigmoid(sum_h1)
                dl_w2 = dL_j * dl_h1 * x[1] * deriv_sigmoid(sum_h1)
                dl_b1 = dL_j * dl_h1 * deriv_sigmoid(sum_h1)
                dl_w3 = dL_j * dl_h2 * x[0] * deriv_sigmoid(sum_h2)
                dl_w4 = dL_j * dl_h2 * x[1] * deriv_sigmoid(sum_h2)
                dl_b2 = dL_j * dl_h2 * deriv_sigmoid(sum_h2)
                #注: 因为b的求导都为1 所以直接更新为rate * b
                #4、 更新w, b
                self.w1 -= dl_w1 * learn_rate 
                self.w2 -= dl_w2 * learn_rate
                self.w3 -= dl_w3 * learn_rate
                self.w4 -= dl_w4 * learn_rate
                self.w5 -= dl_w5 * learn_rate
                self.w6 -= dl_w6 * learn_rate
                self.b1 -= dl_b1 * learn_rate
                self.b2 -= dl_b2 * learn_rate
                self.b3 -= dl_b3 * learn_rate
            if i % 10 == 0:
                y_preds = np.apply_along_axis(self.feed_forward, 1, data)
                loss = loss_mse(y_labels, y_preds)
                print("Epoch %d loss: %.3f" % (i, loss))
    # def __str__(self):
        # pass
       # return 'weightst:%snbiast:%fn' % (np.array([self.w1, self.w2, self.w3, self.w4, self.w5, self.w6]), np.array([self.b1, self.b2, self.b3]))
                
neural = Neural()
neural.train(data, all_y_trues)
print(neural)
emily = np.array([-7, -3]) # 128 pounds, 63 inches
frank = np.array([20, 2])  # 155 pounds, 68 inches
print("Emily: %.3f" % neural.feed_forward(emily)) # 0.951 - F
print("Frank: %.3f" % neural.feed_forward(frank)) # 0.039 - M

参考文章:点击

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/965951.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号