示例#1
0
文件: letnet5.py 项目: BOTBOK/cupcake
def test_train():
    image_data, label_data = readfile()
    im = get_image(image_data)
    label = get_label(label_data)

    a = FeatureMap(32, 1, 1)
    w1 = FilterLayer(5, 1, 6, 1)
    b = FeatureMap(28, 6, 1)
    c = FeatureMap(14, 6, 1)
    w2 = FilterLayer(5, 6, 16, 1)
    d = FeatureMap(10, 16, 1)
    e = FeatureMap(5, 16, 1)
    w3 = FilterLayer(5, 16, 120, 1)
    f = FeatureMap(1, 120, 1)

    connection_map(a, b, w1)
    connection_map(b, c)
    connection_map(c, d, w2)
    connection_map(d, e)
    connection_map(e, f, w3)

    net = Net(120, [84], 10)

    for i in range(10):
        print(i)

        a.set_out_val(ConOp.add_zero_circle(im[i].reshape((1, 28, 28)), 2))
        label_v = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        label_v[label[i]] = 1

        b.cal_net_out()
        b.cal_out_val()
        c.pooling_cal_net_val()
        c.pooling_cal_out_val()
        d.cal_net_out()
        d.cal_out_val()
        e.pooling_cal_net_val()
        e.pooling_cal_out_val()
        f.cal_net_out()
        f.cal_out_val()

        con_out = f.out_val
        net_input = con_out.reshape(120)
        net.cal_out(net_input)

        net.cal_err_term(label_v)
        net.cal_err_out()

        err_out_val = np.array(net.err_out).reshape((120, 1, 1))
        f.set_err_term(err_out_val)
        e.cal_err_out()
        e.cal_err_term()
        d.get_err_out_from_pooling()
        d.cal_err_term()
        c.cal_err_out()
        c.cal_err_term()
        b.get_err_out_from_pooling()
        b.cal_err_term()

        w1.cal_dw()
        w2.cal_dw()
        e.cal_dw()
        c.cal_dw()
        net.cal_dw()

        w1.update_w(0.01)
        w2.update_w(0.01)
        e.update_w(0.01)
        c.update_w(0.01)
        net.update_w(0.01)

    for i in range(10):
        a.set_out_val(ConOp.add_zero_circle(im[i].reshape((1, 28, 28)), 2))
        label_v = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        label_v[label[i]] = 1

        b.cal_net_out()
        b.cal_out_val()
        c.pooling_cal_net_val()
        c.pooling_cal_out_val()
        d.cal_net_out()
        d.cal_out_val()
        e.pooling_cal_net_val()
        e.pooling_cal_out_val()
        f.cal_net_out()
        f.cal_out_val()

        con_out = f.out_val
        net_input = con_out.reshape(120)
        print(net.cal_out(net_input))
示例#2
0
文件: letnet5.py 项目: BOTBOK/cupcake
def test_train1():
    image_data, label_data = readfile()
    im = get_image(image_data)
    label = get_label(label_data)

    a = FeatureMap(32, 1, 1)
    w1 = FilterLayer(5, 1, 6, 1)
    b = FeatureMap(28, 6, 1)
    c = FeatureMap(14, 6, 1)
    w2 = FilterLayer(5, 6, 16, 1)
    d = FeatureMap(10, 16, 1)
    e = FeatureMap(5, 16, 1)
    w3 = FilterLayer(5, 16, 120, 1)
    f = FeatureMap(1, 120, 1)

    connection_map(a, b, w1)
    connection_map(b, c)
    connection_map(c, d, w2)
    connection_map(d, e)
    connection_map(e, f, w3)

    net = Net(120, [84], 10)

    for i in range(1000):
        a.set_out_val(ConOp.add_zero_circle(im[i].reshape((1, 28, 28)), 2))
        label_v = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        label_v[label[i]] = 1

        b.cal_net_out()
        b.cal_out_val()
        c.pooling_cal_net_val()
        c.pooling_cal_out_val()
        d.cal_net_out()
        d.cal_out_val()
        e.pooling_cal_net_val()
        e.pooling_cal_out_val()
        f.cal_net_out()
        f.cal_out_val()

        con_out = f.out_val
        net_input = con_out.reshape(120)
        net.cal_out(net_input)

        net.cal_err_term(label_v)
        net.cal_err_out()

        err_out_val = np.array(net.err_out).reshape((120, 1, 1))
        f.set_err_term(err_out_val)
        e.cal_err_out()
        e.cal_err_term()
        d.get_err_out_from_pooling()
        d.cal_err_term()
        c.cal_err_out()
        c.cal_err_term()
        b.get_err_out_from_pooling()
        b.cal_err_term()

        w1.cal_dw()

        w_list = w1.w_list
        dw = w1.dw
        for i in range(len(w_list)):
            deep, row, col = w_list[i].shape
            for index_deep in range(deep):
                for r in range(row):
                    for co in range(col):
                        es = 0.00001
                        w_list[i][index_deep][r][co] += es
                        w1.set_w(w_list)

                        b.cal_net_out()
                        b.cal_out_val()
                        c.pooling_cal_net_val()
                        c.pooling_cal_out_val()
                        d.cal_net_out()
                        d.cal_out_val()
                        e.pooling_cal_net_val()
                        e.pooling_cal_out_val()
                        f.cal_net_out()
                        f.cal_out_val()

                        con_out = f.out_val
                        net_input = con_out.reshape(120)
                        out1 = net.cal_out(net_input)
                        err1 = cal_err(out1, label_v)

                        w_list[i][index_deep][r][co] -= 2 * es
                        w1.set_w(w_list)

                        b.cal_net_out()
                        b.cal_out_val()
                        c.pooling_cal_net_val()
                        c.pooling_cal_out_val()
                        d.cal_net_out()
                        d.cal_out_val()
                        e.pooling_cal_net_val()
                        e.pooling_cal_out_val()
                        f.cal_net_out()
                        f.cal_out_val()

                        con_out = f.out_val
                        net_input = con_out.reshape(120)
                        out2 = net.cal_out(net_input)
                        err2 = cal_err(out2, label_v)

                        w_list[i][index_deep][r][co] += es
                        w1.set_w(w_list)

                        print((err1 - err2) / (2 * es))
                        print(dw[i][index_deep][r][co])

    for i in range(10):
        label_v = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        label_v[label[i]] = 1
        print(label[i])
        print(net.cal_out(im[i]))