深度学习入门笔记2-深度学习所需要的计算函数与层
通过学习我们知道:全连接神经网络的实现呢,是有数据集与神经网络组成的。
神经网络又是由输入层、中间的全连接层以及最后的输出层组成的。
整个网络标志成一个多维的矩阵,神经网络各个节点包括一些偏置和权重就是矩阵里的元素。
而每一层于每一层之间的连接是通过一些,独特设计好的计算函数来达到可以使神经网络完成功能的效果。
那么这一篇我们来实现一下这些计算函数
我们先来列举一下我们需要实现的东西都有哪些
- 我们的实现由网络和数据集组成:
数据集我们已经实现完了现在我们来看我们的网络,我们这次来完成它的基层的计算函数和层。
- 首先是左侧基本的计算函数
# 此模块用于实现激活函数,损失函数以及层的误差反向传播算法
import numpy as np
# 激活函数--------------------------------------------------
# 恒等函数-输出层
def identity_function(x):
"""
(统一写法,在输出层对于回归问题用恒等函数,
分类问题用softmax函数,
避免以后想改的时候看不懂自己的代码O(∩_∩)O~)
"""
return x
# softmax函数
def softmax(x):
if x.ndim == 2:
x = x.T
x = x - np.max(x, axis=0)
y = np.exp(x) / np.sum(np.exp(x), axis=0)
return y.T
x = x - np.max(x)
return np.exp(x) / np.sum(np.exp(x))
# 阶跃函数
def step_funciton(x):
"""
输入大于0输出1,否则输出0
"""
return np.array(x > 0, dtype=np.int)
# S型函数
def sigmod(x):
return 1 / (1 + np.exp(-x))
# ReLu函数
def relu(x):
return np.maximum(0, x)
# 损失函数--------------------------------------------------
# 均方误差
def mean_squared_error(y, t):
return 0.5 * np.sum((y - t) ** 2)
# 交叉熵误差
def cross_entropy_error(y, t):
if y.ndim == 1:
t = t.reshape(1, t.size)
y = t.reshape(1, y.size)
# 监督数据是one_hot_vector的情况
if t.size == y.size:
t = t.argmax(axis=1)
batch_size = y.shape[0]
return -np.sum(np.log(y[np.arange(batch_size), t] + 1e-7)) / batch_size
- 然后我们来实现这些层的基本功能以及反向传播算法
代码:
# 层的实现--------------------------------------------------
# ReLu层
class Relu:
def __init__(self):
self.mask = None
def forward(self, x):
self.mask = x <= 0
out = x.copy()
out[self.mask] = 0
return out
def backward(self, dout):
dout[self.mask] = 0
dx = dout
return dx
# Sigmoid层
class Sigmoid:
def __init__(self):
self.out = None
def forward(self, x):
out = sigmod(x)
self.out = out
return out
def backward(self, dout):
dx = dout * (1.0 - self.out) * self.out
return dx
# Affine层
class Affine:
def __init__(self, W, b):
self.W = W
self.b = b
self.x = None
self.original_x_shape = None
self.dW = None
self.db = None
def forward(self, x):
self.original_x_shape = x.shape
x = x.reshape(x.shape[0], -1)
self.x = x
out = np.dot(self.x, self.W) + self.b
return out
def backward(self, dout):
dx = np.dot(dout, self.W.T)
self.dW = np.dot(self.x.T, dout)
self.db = np.sum(dout, axis=0)
dx = dx.reshape(*self.original_x_shape)
return dx
# SoftmaxWithLoss层:
class SoftmaxWithLoss:
def __init__(self):
self.loss = None
self.y = None
self.t = None
def forward(self, x, t):
self.t = t
self.y = softmax(x)
self.loss = cross_entropy_error(self.y, self.t)
return self.loss
def backward(self, dout=1):
batch_size = self.t.shape[0]
if self.t.size == self.y.size:
dx = (self.y - self.t) / batch_size
else:
dx = self.y.copy()
dx[np.arange(batch_size), self.t] -= 1
dx = dx / batch_size
return dx