轻松学习神经网络3:构建前向传播和反向传播预测结果

coding: utf-8

正向传播和反向传播的测试

import numpy as np

trace = False
trace_forward = False

1、构建正向传播和方向传播的类

class FC:
”’
This class is not thread safe.
”’
#初始化输入和输出的维度,初始化w、b和lr值
def init(self, in_num, out_num, lr=0.1):
self._in_num = in_num
self._out_num = out_num
self.w = np.random.randn(in_num, out_num)
#self.w = np.ones((in_num, out_num))
self.b = np.zeros((out_num, 1))
self.lr = lr

#构建sigmoid函数
def _sigmoid(self, in_data):
    return 1 / (1 + np.exp(-in_data))
    #return in_data

#实现前向传播
def forward(self, in_data):
    self.topVal = self._sigmoid(np.dot(self.w.T, in_data) + self.b)
    if trace_forward:
        print('=== topVal {0} ==='.format(self.topVal.shape))
        print(self.topVal)
    self.bottomVal = in_data
    return self.topVal

#实现反向传播
def backward(self, loss):
    residual_z = loss * self.topVal * (1 - self.topVal)#计算residual_z的值
    grad_w = np.dot(self.bottomVal, residual_z.T)
    grad_b = np.sum(residual_z)
    self.w -= self.lr * grad_w
    self.b -= self.lr * grad_b
    residual_x = np.dot(self.w, residual_z)
    if trace:
        print('=== z {0}==='.format(residual_z.shape))
        print(residual_z)
        print('=== grad_w {0}==='.format(grad_w.shape))
        print(grad_w)
        print('=== grad_b {0}==='.format(grad_b.shape))
        print(grad_b)
        print('=== self.w {0}==='.format(self.w.shape))
        print(self.w)
        print('=== self.b {0} ==='.format(self.b.shape))
        print(self.b)
        print('=== residual {0} ==='.format(residual_x.shape))
        print(residual_x)
    return residual_x

2、构建损失函数类

class SquareLoss:
”’
Same as above, not thread safe
”’
#损失函数的前向传播
def forward(self, y, t):
self.loss = y - t
if trace:
print(‘=== Loss ===’.format(self.loss.shape))
print(self.loss)
#num1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值