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

深度学习,函数总结

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

深度学习,函数总结

深度学习,阅读笔记
  • def 函数定义:
    • 阶跃函数:隐藏层激活函数
    • 平滑函数:隐藏层激活函数
    • softmax函数:输出层激活函数
    • 输出损失函数:
    • ReLU函数:激活函数
    • 推测,输出函数:
    • 识别精度:
    • 初始化:
    • 损失函数:
    • 均方误差:
    • 交叉熵误差:
    • mini-batch版交叉熵误差
    • 交叉熵误差,非one-hot表示:
    • 数值微分:求导
    • 偏导数(全部变量的偏导数汇总而成的向量称为梯度):
    • 梯度法:

从感知机到神经网络到神经网络学习
函数形式:

def 函数定义: 阶跃函数:隐藏层激活函数
def step_function(x):
 	return np.array(x > 0, dtype=np.int)
平滑函数:隐藏层激活函数
def sigmoid(x):
    return 1 / (1 + np.exp(-x))
softmax函数:输出层激活函数
def softmax(a):
    c = np.max(a)
    exp_a = np.exp(a-c)
    sum_exp_a = sum(exp_a)
    y = exp_a/sum_exp_a
    return y

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 softmax_loss(X, t):
    y = softmax(X)
    return cross_entropy_error(y, t)
ReLU函数:激活函数
def relu(x):
 	return np.maximum(0, x)
推测,输出函数:
def predict(self, x):
    W1, W2 = self.params['W1'], self.params['W2']
    b1, b2 = self.params['b1'], self.params['b2']

    a1 = np.dot(x, W1) + b1
    z1 = sigmoid(a1)
    a2 = np.dot(z1, W2) + b2
    y = softmax(a2)
    
    return y
识别精度:
def accuracy(self, x, t):
    y = self.predict(x)
    y = np.argmax(y, axis=1)
    t = np.argmax(t, axis=1)
    
    accuracy = np.sum(y == t) / float(x.shape[0])
    return accuracy
初始化:
def __init__(self, input_size, hidden_size, output_size, weight_init_std=0.01):
    # 初始化权重
    self.params = {}
    self.params['W1'] = weight_init_std * np.random.randn(input_size, hidden_size)
    self.params['b1'] = np.zeros(hidden_size)
    self.params['W2'] = weight_init_std * np.random.randn(hidden_size, output_size)
    self.params['b2'] = np.zeros(output_size)
损失函数:
def loss(self, x, t):
    y = self.predict(x)
    
    return cross_entropy_error(y, t)
均方误差:
def mean_squared_error(y,t):
    return 0.5*np.sum((y-t)**2)
交叉熵误差:
def cross_entropy_error(y,t):
    delta = 1e-7
    return -np.sum(t*np.log(y+delta))
mini-batch版交叉熵误差
def cross_entropy_error(y,t):
    if y.ndim == 1:
        t = t.reshape(1,t.size)
        y = y.reshape(1,y.size)
    bitch_size = y.shape[0]
    return -np.sum(t*np.log(y+1e-7))/bitch_size
交叉熵误差,非one-hot表示:
def cross_entropy_error(y, t):
     if y.ndim == 1:
        t = t.reshape(1, t.size)
        y = y.reshape(1, y.size)
     batch_size = y.shape[0]
     return -np.sum(np.log(y[np.array(batch_size),t]+1e-7))/batch_size
    
def cross_entropy_error(y, t):
    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 numerical_diff(f, x):
     h = 1e-4 # 0.0001
     return (f(x+h) - f(x-h)) / (2*h)
偏导数(全部变量的偏导数汇总而成的向量称为梯度):
def numerical_gradient(f, x):
     h = 1e-4 # 0.0001
     grad = np.zeros_like(x) # 生成和x形状相同的数组
     for idx in range(x.size):
     tmp_val = x[idx]
     # f(x+h)的计算
     x[idx] = tmp_val + h
     fxh1 = f(x)
     # f(x-h)的计算
     x[idx] = tmp_val - h
     fxh2 = f(x)
     grad[idx] = (fxh1 - fxh2) / (2*h)
     x[idx] = tmp_val # 还原值
 return grad
梯度法:
def gradient_descent(f, init_x, lr=0.01, step_num=100):
     x = init_x
     for i in range(step_num):
         grad = numerical_gradient(f, x)
         x -= lr * grad
 return x
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/283556.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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