深度学习入门

2 感知机

1感知机的特点

美国学者Frank Rosenblatt 1957年提出
多个输入,一个输出。

2一些术语的意义

权重:决定该信号的重要性
偏置:决定神经元被激活的难易程度
阈值:神经元被激活的临界值

3四种逻辑门的代码
import numpy as np

def AND(x1,x2):
    w = np.array([0.5,0.5])
    x = np.array([x1,x2])
    b = -0.9
    if np.sum(w*x)+b > 0:
        return 1
    else :
        return 0

def NOAND(x1,x2):
    w = np.array([-0.5,-0.5])
    x = np.array([x1,x2])
    b = 0.9
    if np.sum(w*x)+b > 0:
        return 1
    else :
        return 0

def OR(x1,x2):
    w = np.array([1,1])
    x = np.array([x1,x2])
    b = -0.9
    if np.sum(w*x)+b > 0:
        return 1
    else :
        return 0


def XOR(x1,x2):
    s1 = NOAND(x1,x2)
    s2 = OR(x1,x2)
    return(AND(s1,s2))

if __name__ == "__main__":
    X1 = [0,1]
    X2 = [0,1]
    for x1 in X1:
        for x2 in X2:
            print(AND(x1,x2),NOAND(x1,x2),OR(x1,x2),XOR(x1,x2))

3 神经网络

1专有概念
激活函数:将输入信号的总和转化为输出信号,Sigmoid,Relu。

注意:隐藏层和最终输出层的激活函数不一定相同。

正规化:将数据限定到某个范围的处理
预处理:对网络的输入数据进行某种既定的转换
2 Active_function.py
import numpy as np
import matplotlib.pyplot as plt

def setp_function(x):
    y = x > 0
    return y.astype(np.int)

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

def Relu(x):
    y = np.maximum(0,x)
    return y 

def softmax(a):
    c = np.max(a)  #防止a过大,计算机无法计算
    exp_a = np.exp(a-c)
    sum_a = np.sum(exp_a)
    return exp_a/sum_a

if __name__ == "__main__":
    x = np.arange(-5,5,0.1)
    y1 = setp_function(x)
    y2 = sigmoid(x)
    y3 = Relu(x)

    plt.figure()
    plt.plot(x,y1)
    plt.ylim(-0.1,1.1)
    plt.plot(x,y2)
    plt.plot(x,y3)
    plt.show()
3三层神经网络前向传播.py
import numpy as np
import active_fun

#输入
x = np.array([1.0,0.5])
w1 = np.array([[0.1,0.3,0.5],[0.2,0.4,0.6]])
b1 = np.array([0.1,0.2,0.3])

A1 = np.dot(x,w1)+b1
z1 = active_fun.sigmoid(A1)

w2 = np.array([[0.1,0.4],[0.2,0.5],[0.3,0.6]])
b2 = np.array([0.1,0.2])

A2 = np.dot(z1,w2)+b2
z2 = active_fun.sigmoid(A2)

w3 = np.array([[0.1,0.3],[0.2,0.4]])
b3 = np.array([0.1,0.2])

A3 = np.dot(z2,w3)+b3
Y = A3  #输出层的激活函数另有讲究
print(Y)

4 神经网络的训练

学习:从数据中自动获取最优权重参数的过程
泛化能力:处理未被观察过的数据的能力
损失函数:寻找最优权重参数的指标
one-shot表示:正确解标签为1,其他标签为0
两种损失函数
import numpy as np

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

def cross_entroy_error(y,t): #实际上只计算正确解的自然对数
    delta = 1e-7  #防止log 0 = 负无穷大
    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+delta))/batch_size

梯度:全部变量的偏导数合在一起的向量,指示的方向是各点处函数值减少最多的方向。
神经网络的梯度:指损失函数关于权重的梯度,即函数为损失函数,变量为权重。
超参数:需要人工设定,一般需要测试调整。
SGD:随机梯度下降法,关于损失函数和权重之间的方法。
epoch:次数,所有数据被“看过”的次数。
求偏导.py
import numpy as np

def numerical_gradient(f,x):
    h = 0.0001
    grad = np.zeros_like(x)
    for idx in range(x.size):
        tmp_val = x[idx]
        x[idx] = tmp_val+h
        fxh1 = f(x)
        
        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 -= grad*lr  #为啥是负号,梯度下降法,找最小值
        
    
    return x 

if __name__ == "__main__":
    def func1(x):
        f = (x[0]-1)**2+(x[1]-2)**3+x[2]**2

        return f

    #grad = numerical_gradient(func1,np.array([2.0,3.0,5.0]))
    #print(grad)
    min_num = gradient_descent(func1,np.array([1.0,0.0,-1.0]),lr = 0.01,step_num= 100) 
    print(min_num)
    

5误差反向传播法

各类层实现.py
import numpy as np
from 神经网络.active_fun import softmax
from 神经网络的学习.cross_function import cross_entroy_error
 
class Mullayer:
    def __init__(self):
        self.x = None
        self.y = None

    def forward(self,x,y):
        self.x = x
        self.y = y
        return self.x*self.y

    def backward(self,dout):
        dx = dout*self.y 
        dy = dout*self.x
         
        return dx,dy


class AddLayer:
    def forward(self,x,y):

        return x+y

    def backward(self,dout):
        dx = dout
        dy = dout

        return dx,dy

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

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

    def forward(self,x):
        out = 1/(1+np.exp(-x))
        self.out = out
        return out
    
    def backward(self,dout):
        dx = dout*(1-self.out)*self.out
        return dx


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

    def forward(self,x):
        self.x = x
        out = np.dot(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

class SoftmaxAndCrossEE:
    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_entroy_error(self.y,t)

        return self.loss

    def backward(self,dout = 1):
        bacth_size = self.t.shape[0]
        dx = (self.y-self.x)/bacth_size

        return dx


if __name__ == "__main__":

'''Relu
    x = np.array([[1.0,-0.5],[-2.0,3.0]])
    y = np.array([[3.0,2.0],[2.0,4.0]])

    print(x)
    test = Relu()
    out = test.forward(x)
    print(out)
    out1 = test.backward(y)
    print(out1)
'''

'''MulLayer()
    apple = 100
    num = 2
    tax = 1.1

    apple_layer = Mullayer()
    tax_layer = Mullayer()

    #forward mullayer
    apple_money = apple_layer.forward(apple,num)
    price = tax_layer.forward(apple_money,tax)

    print(price)

    #backward
    dprice = 1
    dapple_price,dtax = tax_layer.backward(dprice)
    dapple,dnum = apple_layer.backward(dapple_price)

    print(dapple_price,dnum,dtax)
'''

6与学习相关的技巧

参数更新的方法:
SDG: 根据权重的梯度。优点是实现简单,缺点是梯度方向只代表当前处的函数值减少最多的方向,并不一定是整个函数的最小值处。
Momentum: 在SDG的基础上增加了与梯度成正相关的力的考虑。
AdaGrad:在SGD的基础上改变学习效率,会为参数的每一个元素调整学习效率。在此基础发展的算法还有RMSProp。
Adam:综合了以上三种。
目前使用较多的是Adam。并没有最好的参数更新方法,要因情况而定。
权重的初始值
Relu激活函数使用He初始值
S型曲线激活函数(sigmoid,tanh),初始值使用Xavier初始值
初值影响了隐藏层的激活值的广度,要求广度要尽可能的大,这样误差反向传播法才能区别,更好的学习。
强制改变激活值的算法:Batch Normalization
过拟合
产生原因:数据少或参数多。
解决办法:权值衰减,Dropout(随机删减神经元)

7卷积神经网络

全连接:相邻神经元之间都有连接。
填充:向输入的数据周围填入固定的数据,比如0.
步幅:卷积核的前进步伐。

卷积后矩阵大小计算
输入矩阵(H,W)卷积核(FH,FW)
填充为P,步幅为S。
输出矩阵
OH = (H+2P-FH)/S+1
OW = (W+2P-FW)/S+1

池化:缩小矩阵长,高方向的空间处理。

Max层:目标区域中最大值,代替此区域。
Average层:目标区域中所有数据的均值,代替此区域。:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丶玉面小蛟龙

花了钱的就是不一样

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值