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