当前位置:   article > 正文

BP神经网络算法 Python实现_bp神经网络python代码

bp神经网络python代码

BP神经网络算法
实验要求:

  1. 掌握BP神经网络算法的实现
  2. 掌握利用BP神经网络对样本数据分类,并观察训练时间及迭代次数对结果的影响

Python 代码

# -*- coding: utf-8 -*-
# @Time    : 2022/10/25 22:48
# @Author  : Norman Mises
# @FileName: train.py
# @Software: PyCharm

import math
import time

import numpy as np
import pandas as pd
import yaml


def train(dat, id):
    x = dat
    global d
    d = [-1 for _ in range(num_layers[-1])]  # 希望输出值
    d[id] = 1
    # print(d)

    global y
    y[0] = x
    for i in range(1, n):  # 每一层
        for j in range(num_layers[i]):  # 每一个
            net = sum([weights[i][j][k] * y[i - 1][k] for k in range(num_layers[i - 1])])
            f = lambda net_: 1 / (1 + math.e ** ((theta[i][j] - net_) / h0))
            y[i][j] = f(net)

    # for i in y[-1]:
    #     print(f'{i:.6f}', end=',')

    Ep = 0.5 * sum([(d[i] - y[-1][i]) ** 2 for i in range(num_layers[-1])])
    # print(f'{Ep}')
    if Ep > 0.50001:
        return False
    else:
        return True


def correct():
    global delta
    for i in range(n - 1, 0, -1):  # 每一层
        for j in range(num_layers[i]):
            if i == n - 1:
                delta[i][j] = y[i][j] * (1 - y[i][j]) * (d[j] - y[-1][j])
            else:
                delta[i][j] = y[i][j] * (1 - y[i][j]) * sum(
                    [delta[i + 1][q] * weights[i + 1][q][j] for q in range(num_layers[i + 1])])
            for k in range(num_layers[i - 1]):
                weights[i][j][k] += eta * delta[i][j] * y[i - 1][k]


def test(x):
    out = y
    out[0] = x
    for i in range(1, n):  # 每一层
        for j in range(num_layers[i]):  # 每一个
            net = sum([weights[i][j][k] * out[i - 1][k] for k in range(num_layers[i - 1])])
            f = lambda net_: 1 / (1 + math.e ** ((theta[i][j] - net_) / h0))
            out[i][j] = f(net)

    for i in range(num_layers[-1]):
        print(f'第 {i} 类:{out[-1][i]:.6f}')


if __name__ == "__main__":
    with open('testcases/training_data.yaml', 'r', encoding='utf-8') as f:
        data_yaml = yaml.load(f, yaml.FullLoader)
        print(data_yaml['data'][0])

    label = data_yaml['label'][0]
    df = pd.DataFrame(data_yaml['data'][0])
    data = (df - df.min()) / (df.max() - df.min())
    data = data.values
    print(data)
    print(label)

    h0 = float(input(f'h0: '))
    eta = float(input(f'eta: '))
    n = int(input('层数:'))
    num_layers = []
    weights = []
    theta = []  # 神经元阈值
    delta = []
    d = []
    y = []
    print('每层神经元数:')
    for i in range(n):
        num_layers.append(int(input(f'第 {i} 层:')))
        theta.append(np.random.rand(num_layers[i]))
        delta.append(np.zeros(num_layers[i]))
        y.append(np.zeros(num_layers[i]))
        # y.append([] for _ in range(num_layers[i]))
        if i > 0:
            weights.append(np.random.rand(num_layers[i], num_layers[i - 1]))
        else:
            weights.append([])

    print(f'每层神经元与其低一层的连接权:')
    for i in weights:
        print(i)

    print(f'神经元阈值:')
    for i in theta:
        print(i)
    times = int(input(f'训练次数上限(-1 为无上限):'))
    start = time.time()
    cnt = 1
    while not train(data[cnt % len(data)], label[cnt % len(data)]):
        if times != -1 and cnt >= times:
            break
        cnt += 1
        correct()
    end = time.time()

    print(f'训练结束后每层神经元与其低一层的连接权:')
    for i in weights:
        print(i)
    print(f'训练次数:{cnt}')
    print(f'训练时间:{(end - start):.6f} s')

    try:
        while True:
            print(f'输入测试数据:')
            t = input().split(',')
            t = [float(i.strip()) for i in t]
            print(t)
            test(t)
    except:
        print('测试结束')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132

相关数据文件

# .\testcases\training_data.yaml
data:
- [[99, 63], [128, 71], [175, 88], [206, 102], [143, 146], [188, 114], [206, 129], [110, 99], [142, 91], [188, 94], [982, 390], [1007, 388], [990, 414], [1000, 441], [1044, 400], [1057, 419], [985, 444], [1011, 424], [1052, 425], [1056, 427]]
label:
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
# .\testcases\test_data.yaml
data:
- [[100, 65], [110, 60], [180, 70], [220, 110], [130, 150], [200, 110], [190, 135], [114, 90], [150, 90], [200, 80], [950, 400], [1010, 370], [980, 450], [1000, 410], [1077, 500], [1007, 450], [1001, 454], [1022, 450], [1055, 450], [1076, 440]]

  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/94835
推荐阅读
相关标签
  

闽ICP备14008679号