博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
神经网络对比
阅读量:6192 次
发布时间:2019-06-21

本文共 3630 字,大约阅读时间需要 12 分钟。

 

1. 只能分两类

层数固定不变 层数可以变化
''' 11行神经网络①  固定三层,两类'''# 只适合 0, 1 两类。若不是,要先转化import numpy as npX = np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]])y = np.array([0,1,1,0]).reshape(-1,1) # 此处reshape是为了便于算法简洁实现wi = 2*np.random.randn(3,5) - 1wh = 2*np.random.randn(5,1) - 1for j in range(10000):    li = X    lh = 1/(1+np.exp(-(np.dot(li,wi))))    lo = 1/(1+np.exp(-(np.dot(lh,wh))))        lo_delta = (y - lo)*(lo*(1-lo))    lh_delta = np.dot(lo_delta, wh.T) * (lh * (1-lh))        wh += np.dot(lh.T, lo_delta)    wi += np.dot(li.T, lh_delta)    print('训练结果:', lo)
'''11行神经网络① 层数可变,两类'''# 只适合 0, 1 两类。若不是,要先转化import numpy as npX = np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]])y = np.array([0,1,1,0]).reshape(-1,1) # 此处reshape是为了便于算法简洁实现neurals = [3,15,1] w = [np.random.randn(i,j) for i,j in zip(neurals[:-1], neurals[1:])] + [None]l = [None] * len(neurals)l_delta = [None] * len(neurals)for j in range(1000):    l[0] = X    for i in range(1, len(neurals)):        l[i] = 1 / (1 + np.exp(-(np.dot(l[i-1], w[i-1]))))            l_delta[-1] = (y - l[-1]) * (l[-1] * (1 - l[-1]))    for i in range(len(neurals)-2, 0, -1):        l_delta[i] = np.dot(l_delta[i+1], w[i].T) * (l[i] * (1 - l[i]))            for i in range(len(neurals)-2, -1, -1):        w[i] += np.dot(l[i].T, l_delta[i+1])        print('训练结果:', l[-1])

 

 

2.可以分多类

层数固定不变 层数可以变化
'''11行神经网络① 固定三层,多类'''import numpy as npX = np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]])#y = np.array([0,1,1,0]) # 可以两类y = np.array([0,1,2,3])  # 可以多类wi = np.random.randn(3,5)wh = np.random.randn(5,4) # 改bh = np.random.randn(1,5)bo = np.random.randn(1,4) # 改epsilon = 0.01    # 学习速率lamda = 0.01      # 正则化强度for j in range(1000):    li = X    lh = np.tanh(np.dot(li, wi) + bh)     # tanh 函数    lo = np.exp(np.dot(lh, wh) + bo)    probs = lo / np.sum(lo, axis=1, keepdims=True)    # 后向传播    lo_delta = np.copy(probs)    lo_delta[range(X.shape[0]), y] -= 1    lh_delta = np.dot(lo_delta, wh.T) * (1 - np.power(lh, 2))     # 更新权值、偏置    wh -= epsilon * (np.dot(lh.T, lo_delta) + lamda * wh)    wi -= epsilon * (np.dot(li.T, lh_delta) + lamda * wi)        bo -= epsilon * np.sum(lo_delta, axis=0, keepdims=True)    bh -= epsilon * np.sum(lh_delta, axis=0)    print('训练结果:', np.argmax(probs, axis=1))

 

'''11行神经网络① 层数可变,多类'''import numpy as npX = np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]])#y = np.array([0,1,1,0]) # 可以两类y = np.array([0,1,2,3])  # 可以多类neurals = [3, 10, 8, 4] w = [np.random.randn(i,j) for i,j in zip(neurals[:-1], neurals[1:])] + [None]b = [None] + [np.random.randn(1,j) for j in neurals[1:]]l = [None] * len(neurals)l_delta = [None] * len(neurals)epsilon = 0.01    # 学习速率lamda = 0.01      # 正则化强度for j in range(1000):    # 前向传播    l[0] = X    for i in range(1, len(neurals)-1):        l[i] = np.tanh(np.dot(l[i-1], w[i-1]) + b[i]) # tanh 函数        l[-1] = np.exp(np.dot(l[-2], w[-2]) + b[-1])    probs = l[-1] / np.sum(l[-1], axis=1, keepdims=True)    # 后向传播    l_delta[-1] = np.copy(probs)    l_delta[-1][range(X.shape[0]), y] -= 1    for i in range(len(neurals)-2, 0, -1):        l_delta[i] = np.dot(l_delta[i+1], w[i].T) * (1 - np.power(l[i], 2)) # tanh 函数的导数    # 更新权值、偏置    b[-1] -= epsilon * np.sum(l_delta[-1], axis=0, keepdims=True)    for i in range(len(neurals)-2, -1, -1):        w[i] -= epsilon * (np.dot(l[i].T, l_delta[i+1]) + lamda * w[i])        if i == 0: break        b[i] -= epsilon * np.sum(l_delta[i], axis=0)        # 打印损失    if j % 100 == 0:        loss = np.sum(-np.log(probs[range(X.shape[0]), y]))        loss += lamda/2 * np.sum([np.sum(np.square(wi)) for wi in w[:-1]]) # 可选        loss *= 1/X.shape[0]  # 可选        print('loss:', loss)print('训练结果:', np.argmax(probs, axis=1))

 

 

转载地址:http://vjgda.baihongyu.com/

你可能感兴趣的文章
D.6661 - Equal Sum Sets
查看>>
确保每一步的业务代码都能够正确执行。
查看>>
etcd:从应用场景到实现原理的全方位解读
查看>>
python基础===map和zip的用法
查看>>
【UVA 11426】gcd之和 (改编)
查看>>
iOS调用系统相册、相机 显示中文标题
查看>>
SEU第一次训练练习题
查看>>
重新定义日志成员
查看>>
Ansible安装
查看>>
poj3270
查看>>
python的requests模块参数详解
查看>>
-sh: ./helloworld: not found
查看>>
eclipse - The superclass "javax.servlet.http.HttpServlet" was not found on the Java
查看>>
数学 FZU 2074 Number of methods
查看>>
.NET MVC与三层架构
查看>>
5.IntellijIDEA常用快捷键总结
查看>>
The type org.springframework.core.io.support.ResourcePatternResolver cannot be resolved. It is ind
查看>>
[Luogu]中位数
查看>>
Json详解2-对上一篇文章(Json)例子的个人操作过程
查看>>
WINDOWS8.1企业版官方下载
查看>>