参考书:深度学习入门:基于Python的理论与实现
源代码 -> common -> functions.py
1.神经网络传递时,使用激活函数:sigmoid、relu(最近主要使用)
2.输出层主要使用:恒等函数(回归问题)、softmax函数(分类问题,输出可以理解为概率)
3.神经网络学习中使用损失函数作为指标:均方误差、交叉熵误差,值越小越好
# coding: utf-8
import numpy as np
def identity_function(x): # 恒等函数(P64),输出层使用,适合回归问题
return x
def step_function(x): # 阶跃函数(P44):输入数组x,大于0的元素输出1,小于1的元素输出0
return np.array(x > 0, dtype=np.int)
def sigmoid(x): # sigmoid函数(P45),激活函数,神经元传递时使用
return 1 / (1 + np.exp(-x))
def sigmoid_grad(x):
return (1.0 - sigmoid(x)) * sigmoid(x)
def relu(x): # 返回0和x中最大的一个(P49),激活函数,神经元传递时使用,最近比较火
return np.maximum(0, x)
def relu_grad(x):
grad = np.zeros(x)
grad[x>=0] = 1
return grad
def softmax(x): # softmax函数(P64),输出层使用,适合分类问题
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 mean_squared_error(y, t): # 均方误差(P86)
return 0.5 * np.sum((y-t)**2)
def cross_entropy_error(y, t): # 交叉熵误差(P88)
if y.ndim == 1:
t = t.reshape(1, t.size)
y = y.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
def softmax_loss(X, t):
y = softmax(X)
return cross_entropy_error(y, t)