IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> 深度学习,函数总结 -> 正文阅读

[人工智能]深度学习,函数总结


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

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
  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2021-10-02 14:40:58  更:2021-10-02 14:41:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/22 0:35:45-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码