深度学习入门(1)

19 篇文章 0 订阅

1.感知机是神经网络的起源。
2.偏置决定了神经元被激活的容易程度。具体表现我为 y = wx + b b为偏置项 w为权重

3.常用激活函数及实现
1)阶越函数

y = x>0?1:0

实现:

def step_function(x):
    return np.array(x > 0, dtype=np.int)

2)sigmoid函数

y =  1/(1+exp(-x))

实现:

def sigmoid(x):
    return 1 / (1 + np.exp(-x))  

3)ReLu函数

y = x>0?x:0

实现:

def relu(x):
    return np.maximum(0, x)

4.输出层设计(softmax,)

Yk = exp(Ak)/sum(exp(Ai))

通常会大数溢出,
因此做个改动,都减去最大值,归1化处理。
实现:

def softmax(x):
    x = x - np.max(x) 
    return np.exp(x) / np.sum(np.exp(x))

5 . 感知机与神经网络的区别
主要是激活函数不一样,感知机用的不连续的阶越函数,而神经网络一般用连续的RELU或者sigmoid函数。

6.批处理学习一下

7.损失函数
表示神经网络的恶劣程度,性能有多好。常用的损失函数有:
<1>均方误差
实现:

def mean_squared_error(y, t):
    return 0.5 * np.sum((y-t)**2)

<2>交差商误差

def cross_entropy_error(y, t):
   delta = 1e-7
   return -np.sum(t * np.log(y + delta))

误差的值越小,表示与真实值越相近。
8.mini-batch学习
onehot 编码

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(t*np.log(y + 1e-7))/batch_size

包含非onehot编码

def cross_entropy_error1(y, t):
    if y.ndim == 1:
        t = t.reshape(1, t.size)
        y = y.reshape(1, y.size)
        
    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

损失函数:
在进行神经网络的学习时,不能将识别精度作为指标。因为如果以识别精度为指标,则参数的导数在绝大多数地方都会变为0.

9.误差反向传播
激活层的实现:
1)RELU

class Relu:
    def __init__(self):
        self.mask = None

    def forward(self, x):
        self.mask = (x <= 0) #萃取小于等于0的数的坐标
        out = x.copy()
        out[self.mask] = 0  #将小于等于0的数置0

        return out

    def backward(self, dout):
        dout[self.mask] = 0 #反向传播小于等于0 为0  大于零,原样
        dx = dout

        return dx

2)Sigmoid

class Sigmoid:
    def __init__(self):
        self.out = None

    def forward(self, x):
        out = sigmoid(x)
        self.out = out
        return out

    def backward(self, dout):
        dx = dout * (1.0 - self.out) * self.out
        return dx

3)Affine层

class Affine:
    def __init__(self, W, b):
        self.W =W
        self.b = b
        
        self.x = None
        self.dW = None
        self.db = None

    def forward(self, x):
        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)
        
        return dx

4)softmax-with-loss

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

10.学习算法步骤
神经网络存在合适的权重和偏转,调整权重和偏转以便拟合训练数据的过程称为学习,神经网络的学习主要分成如下4个步骤:

步骤1  mini-batch
从训练数据中挑选一部分数据,称为mini-batch
步骤2  计算梯度
为了减小mini-batch的损失函数的值,需要求出各个权重参数的梯度。
步骤3  更新参数
将权重参数沿梯度减小的方向进行微小更新。
步骤4  重复
重复步骤1,步骤2,步骤3,直至训练完成。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值