《深度学习的数学》涌井良幸等著第四章的python代码实现

该代码实现了一个简单的神经网络模型,包括初始化权重、前向传播、反向传播和权重更新等步骤。数据从Excel文件读取,使用Sigmoid激活函数,模型结构为[12,3,2],并进行了1000次迭代训练。
摘要由CSDN通过智能技术生成
import numpy as np
import pandas as pd


def jiedian_new(qj, j):
    jiedian_w = []
    jiedian_b = 0
    if j == 0 and qj == 12:
        jiedian_w.append(0.49038557898997)
        jiedian_w.append(0.348475676796501)
        jiedian_w.append(0.0725879008695083)
        jiedian_w.append(0.837472826850604)
        jiedian_w.append(-0.0706798311519743)
        jiedian_w.append(-3.6169369170322)
        jiedian_w.append(-0.53557819719488)
        jiedian_w.append(-0.0228584789393108)
        jiedian_w.append(-1.71745249082217)
        jiedian_w.append(-1.45563751579807)
        jiedian_w.append(-0.555799932254451)
        jiedian_w.append(0.852476539980059)
        jiedian_b = -0.185002356132065
    if j == 1 and qj == 12:
        jiedian_w.append(0.442372911956926)
        jiedian_w.append(-0.536877487857221)
        jiedian_w.append(1.00782536916829)
        jiedian_w.append(1.07196001297575)
        jiedian_w.append(-0.732814485632708)
        jiedian_w.append(0.822959617857012)
        jiedian_w.append(-0.453282364154155)
        jiedian_w.append(-0.0138979392949318)
        jiedian_w.append(-0.0274233258563056)
        jiedian_w.append(-0.426670298661898)
        jiedian_w.append(1.87560275441379)
        jiedian_w.append(-2.30528048189891)
        jiedian_b = 0.525676844318642
    if j == 2 and qj == 12:
        jiedian_w.append(0.654393041569443)
        jiedian_w.append(-1.38856820257739)
        jiedian_w.append(1.24648311661583)
        jiedian_w.append(0.0572877158406771)
        jiedian_w.append(-0.183237472237546)
        jiedian_w.append(-0.74305066513479)
        jiedian_w.append(-0.460930664925325)
        jiedian_w.append(0.331118557255208)
        jiedian_w.append(0.449470835925128)
        jiedian_w.append(-1.29645372413246)
        jiedian_w.append(1.56850561324256)
        jiedian_w.append(-0.470667153317658)
        jiedian_b = -1.16862269778991
    if j == 0 and qj == 3:
        jiedian_w.append(0.3880031194962)
        jiedian_w.append(0.803384989025837)
        jiedian_w.append(0.0292864334994403)
        jiedian_b = -1.43803971240614
    if j == 1 and qj == 3:
        jiedian_w.append(0.0254467679708455)
        jiedian_w.append(-0.790397993881956)
        jiedian_w.append(1.55313793058729)
        jiedian_b = -1.37933790823328
    jiedian_z = 0
    jiedian_a = 0.5
    jiedian_al = []
    for i1 in range(qj):
        jiedian_al.append(np.random.uniform(-10, 10))
    jiedian_w = np.asarray(jiedian_w)
    jiedian_al = np.asarray(jiedian_al)
    jiedian_delta = 1
    jiedian_gradew = np.asarray([])
    jiedian_gradeb = np.asarray([])
    return np.asarray([jiedian_al, jiedian_w, jiedian_b, jiedian_z, jiedian_a,
                       jiedian_delta, jiedian_gradew, jiedian_gradeb], dtype=object)


def new_net(net_layers):
    net_jiedianlist_d = []
    net_before = None
    for i in range(len(net_layers)):
        net_jiedianlist_x = []
        i1 = net_layers[i]
        for j in range(i1):
            net_w = net_before if net_before is not None else 0
            net_jiedian = jiedian_new(net_w, j)
            net_jiedianlist_x.append(net_jiedian)
        net_jiedianlist_x = np.asarray(net_jiedianlist_x, dtype=object)
        net_jiedianlist_d.append(net_jiedianlist_x)
        net_before = net_layers[i]
    net_jiedianlist_d = np.asarray(net_jiedianlist_d, dtype=object)
    return net_jiedianlist_d


def getxy():
    datax_pd = pd.read_excel('wl.xlsx')
    # 10 14 ... 264
    x_xh = 10
    x_xhl = []
    for i in range(64):
        x_xhl.append(x_xh)
        x_xh = x_xh + 4
    x_d = []
    x_x1 = 1
    x_x = []
    y_x = []
    for label, value in datax_pd.items():
        if x_x1 == 4:
            y_x.append(value[7])
            x_x1 = 1
        if x_x1 == 3:
            for j in range(3, 7):
                x_x.append(value[j])
            x_x1 = 4
        if x_x1 == 2:
            for j in range(3, 7):
                x_x.append(value[j])
            x_x1 = 3
        for x_xh in x_xhl:
            if label.endswith(str(x_xh)):
                for j in range(3, 7):
                    x_x.append(value[j])
                x_x1 = 2
                break
        if len(x_x) == 12:
            x_d.append(x_x)
            x_x = []
    x_d_zl = []
    for x_d1 in x_d:
        x_d_xl = []
        x_d_xl.append(x_d1[0])
        x_d_xl.append(x_d1[4])
        x_d_xl.append(x_d1[8])
        x_d_xl.append(x_d1[1])
        x_d_xl.append(x_d1[5])
        x_d_xl.append(x_d1[9])
        x_d_xl.append(x_d1[2])
        x_d_xl.append(x_d1[6])
        x_d_xl.append(x_d1[10])
        x_d_xl.append(x_d1[3])
        x_d_xl.append(x_d1[7])
        x_d_xl.append(x_d1[11])
        x_d_zl.append(x_d_xl)
    datax = np.asarray(x_d_zl)
    y_d = []
    for y_x1 in y_x:
        if y_x1 == 0:
            y_x_l = [1, 0]
            y_d.append(y_x_l)
        else:
            y_x_l = [0, 1]
            y_d.append(y_x_l)
    datay = np.asarray(y_d)
    return datax, datay


def sigmoid(s):
    """激活函数
    """
    if s > 10000:
        return 1
    if s < -10000:
        return 0
    if s >= 0:
        return 1 / (1 + np.exp(-s))
    else:
        return np.exp(s) / (1 + np.exp(s))


def danceng(dc_jiedianlist, x, y_ac):
    # 每层节点 分为 al(上一层输入a) w b z a
    before_layer = None
    rsy = []
    for layer in range(len(dc_jiedianlist)):
        layer1 = dc_jiedianlist[layer]
        if before_layer is None:
            for i, jiedian1 in enumerate(layer1):
                jiedian1[3] = x[i]
                jiedian1[4] = x[i]
        if before_layer is not None:
            al1 = []
            for i, jiedian in enumerate(before_layer):
                al1.append(jiedian[4])
            for i, jiedian in enumerate(layer1):
                w = np.asarray(jiedian[1]).reshape(1, -1)
                al2 = np.asarray(al1).reshape(-1, 1)
                z = np.dot(w, al2) + jiedian[2]
                z1 = np.sum(w * al2) + jiedian[2]
                z = z[0][0]
                a = sigmoid(z)
                if layer == len(dc_jiedianlist) - 1:
                    rsy.append(a)
                jiedian[0] = al2
                jiedian[3] = z
                jiedian[4] = a
        before_layer = layer1
    deltalayerbefore = None
    for layer in range(len(dc_jiedianlist)):
        layer1 = dc_jiedianlist[len(dc_jiedianlist) - 1 - layer]
        if len(dc_jiedianlist) - 1 - layer == 0:
            break
        if deltalayerbefore is None:
            y_aclx = [x for x in y_ac]
            for i, jiedian1 in enumerate(layer1):
                jiedian1[5] = (jiedian1[4]-y_aclx[i]) * jiedian1[4] * (1 - jiedian1[4])
                allist = jiedian1[0].ravel()
                #  jiedian61 = jiedian1[6].ravel()
                #  jiedian62 = (jiedian1[5] * allist).ravel()
                #  jiedian63 = jiedian62 if len(jiedian61) == 0 else np.asarray(jiedian61, jiedian62)
                jiedian1[6] = np.append(jiedian1[6], jiedian1[5] * allist, axis=0)
                jiedian1[7] = np.append(jiedian1[7], jiedian1[5])
        else:
            for i, jiedian1 in enumerate(layer1):
                w_ql = []
                delta_ql = []
                for _, jiedian1_b in enumerate(deltalayerbefore):
                    w_ql.append(jiedian1_b[1][i])
                    delta_ql.append(jiedian1_b[5])
                w_qln = np.asarray(w_ql).reshape(1, -1)
                delta_qln = np.asarray(delta_ql).reshape(-1, 1)
                z = np.dot(w_qln, delta_qln)
                z1 = z[0][0]
                jiedian1[5] = z1 * jiedian1[4] * (1 - jiedian1[4])
                allist = jiedian1[0].ravel()
                jiedian1[6] = np.append(jiedian1[6], jiedian1[5] * allist, axis=0)
                jiedian1[7] = np.append(jiedian1[7], jiedian1[5])
        deltalayerbefore = layer1
    rc = 0
    for ri in range(len(rsy)):
        rc = rc + 1/2*(rsy[ri] - y_ac[ri])**2
    return rc, rsy, dc_jiedianlist


def updatewb(upnet, learning_rate):
    for layer in range(len(upnet)):
        layer1 = upnet[layer]
        if layer > 0:
            for i, jiedian in enumerate(layer1):
                w = jiedian[1]
                b = jiedian[2]
                dw = np.sum(jiedian[6].reshape(-1, len(w)),axis=0)
                db = np.sum(jiedian[7])
                jiedian[1] = w - dw * learning_rate
                jiedian[2] = b - db * learning_rate
                jiedian[6] = np.asarray([])
                jiedian[7] = np.asarray([])
    return upnet


cxlayer = [12, 3, 2]
cxnet = new_net(cxlayer)
cxx, cxy = getxy()
for zongcishu in range(1000):
    jscj = 0
    for dancenghangshu in range(cxx.shape[0]):
        jsc, jsy, cxnet = danceng(cxnet, cxx[dancenghangshu], cxy[dancenghangshu])
        jscj = jscj + jsc
    learning_rate = 0.2
    cxnet = updatewb(cxnet, learning_rate)
    print(cxnet)
    if zongcishu == 49:
        print('a')

测试结果与书中附的excel计算结果相同

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值