main be469344839b cached
4 files
29.5 KB
11.7k tokens
28 symbols
1 requests
Download .txt
Repository: yx868868/GA-optimized-neural-network
Branch: main
Commit: be469344839b
Files: 4
Total size: 29.5 KB

Directory structure:
gitextract_lsom15ox/

├── BPNet1.py
├── GABPNet1.py
├── README.md
└── advertise.txt

================================================
FILE CONTENTS
================================================

================================================
FILE: BPNet1.py
================================================
# BP ,更新阈值和权重,回归预测问题最后一层不带激活函数
# coding: UTF-8
import numpy as np
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from tkinter import _flatten
# 读取txt中的数据,预处理去“,”
def load_data_wrapper(filename):
    lineData = []
    with open(filename) as txtData:
        lines = txtData.readlines()
        for line in lines:
            linedata = line.strip().split(',')
            lineData.append(linedata)
    return lineData
# 提出特征和标签,特征做输入,标签为输出
def splitData(dataset):
    Character= []
    Label = []
    for i in range(len(dataset)):
        Character.append([float(tk) for tk in dataset[i][1:-1]])
        Label.append(float(dataset[i][-1]))
    return Character, Label
#输入特征数据归一化
def max_min_norm_x(dataset):
    min_data = []
    for i in range(len(dataset)):
        min_data.append(min(dataset[i]))
    new_min = min(min_data)
    max_data = []
    for i in range(len(dataset)):
        max_data.append(max(dataset[i]))
    new_max = max(max_data)
    data = np.array(dataset)
    data_x =[]
    for x in np.nditer(data, op_flags=['readwrite']):
        #x[...] = 2 * (x -new_min)/(new_max-new_min)-1
        x[...] = (x - new_min) / (new_max - new_min)
        #print('x[...]:',x[...])
        data_x.append(x[...])
    data_x3 = []
    for index in range(0, len(data_x), 3):
        data_x3.append([data_x[index], data_x[index+1], data_x[index+2]])
    #print("data_x3:",data_x3)
    return data_x3
#输入特征数据归一化
def max_min_norm_y(dataset):
    new_min = min(dataset)
    new_max = max(dataset)
    data_y = []
    for i in range(len(dataset)):
        y = (dataset[i] -new_min)/(new_max-new_min)
        #y = 2 * (dataset[i] - new_min) / (new_max - new_min) - 1
        data_y.append(y)
        #print(y)
    return data_y
#输出特征归一化
def de_max_min_norm_y(dataset1,dataset2):
    new_min = min(dataset1)
    new_max = max(dataset1)
    de_data_y = []
    for i in range(len(dataset2)):
        y = dataset2[i] * (new_max - new_min) + new_min
        de_data_y.append(y)
    return de_data_y
# 初始化参数
# x为输入层神经元个数,y为隐层神经元个数,z输出层神经元个数
def parameter_initialization(x, y, z):
    # 隐层阈值从(-5,5)之间的随机数
    value1 = np.random.randint(-5, 5, (1, y)).astype(np.float64)

    # 输出层阈值
    value2 = np.random.randint(-5, 5, (1, z)).astype(np.float64)

    # 输入层与隐层的连接权重
    weight1 = np.random.randint(-5, 5, (x, y)).astype(np.float64)

    # 隐层与输出层的连接权重
    weight2 = np.random.randint(-5, 5, (y, z)).astype(np.float64)

    return weight1, weight2, value1, value2

#定义激活函数
def sigmoid(z):
    return 1 / (1 + np.exp(-z))
def relu(z):
    return np.where(z < 0, 0, z)

'''
weight1:输入层与隐层的连接权重
weight2:隐层与输出层的连接权重
value1:隐层阈值
value2:输出层阈值
'''
#训练过程
def train_process(dataset, labelset, weight1, weight2, value1, value2):
    # x为步长
    x = 0.05
    for i in range(len(dataset)):
        # 输入数据
        inputset = np.mat(dataset[i]).astype(np.float64)
        # 数据标签
        outputset = np.mat(labelset[i]).astype(np.float64)
        # 隐层输入
        input1 = np.dot(inputset, weight1).astype(np.float64)
        # 隐层输出
        output2 = sigmoid(input1 - value1).astype(np.float64)
        # 输出层输入
        input2 = np.dot(output2, weight2).astype(np.float64)
        # 输出层输出
        output3 = input2 - value2

        # 更新公式由矩阵运算表示 用的是平方误差
        g = outputset - output3 #最后一层直接求导 ,为输出层阈值求导
        b = np.dot(g, np.transpose(weight2))
        c = np.multiply(output2, 1 - output2)
        e = np.multiply(b, c)  # 隐藏层之间阈值

        value1_change = -x * e
        value2_change = -x * g
        weight1_change = x * np.dot(np.transpose(inputset), e)
        weight2_change = x * np.dot(np.transpose(output2), g)

        # 更新参数
        value1 += value1_change
        value2 += value2_change
        weight1 += weight1_change
        weight2 += weight2_change
    return weight1, weight2, value1, value2

def test_process(dataset, labelset, weight1, weight2, value1, value2):
    pre_data = []
    for i in range(len(dataset)):
        # 计算每一个样例通过该神经网路后的预测值
        inputset = np.mat(dataset[i]).astype(np.float64)
        outputset = np.mat(labelset[i]).astype(np.float64)
        output2 = sigmoid(np.dot(inputset, weight1) - value1)
        output3 = np.dot(output2, weight2) - value2
        output3 = output3.tolist()
        pre_data.append(output3)
        pre_data = list(_flatten(pre_data))
        # 返回预测值
    return pre_data

if __name__ == '__main__':
    #要打开的文件名
    iris_file = 'advertise.txt'
    #数据预处理
    Data = load_data_wrapper(iris_file)
    #分离特征标签值,x为数据集的feature数据,y为label.
    x, y = splitData(Data)
    #数据归一化
    x_norm = max_min_norm_x(x)
    y_norm = max_min_norm_y(y)
    #分训练和测试集
    x_train, x_test, y_train, y_test = train_test_split(x_norm, y_norm, test_size=0.3)
    #初始化权重
    weight1, weight2, value1, value2 = parameter_initialization(len(x_train[0]), 2, 1)
    #训练
    for i in range(700):
        weight1, weight2, value1, value2 = train_process(x_train, y_train, weight1, weight2, value1, value2)
    #预测
    pre = test_process(x_test, y_test, weight1, weight2, value1, value2)
    #归一化还原均方误差
    errors_std = np.std(np.array(pre) - np.array(y_test))
    pre_org = np.array(pre) * (max(y) - min(y)) + min(y)
    y_test_org = np.array(y_test) * (max(y) - min(y)) + min(y)
    errors_std_org = np.std(pre_org - y_test_org)
    
    print("errors_std:\n", errors_std)
    print("errors_std_org\n", errors_std_org)
    #显示测试图像
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    x = np.linspace(0, 60, 60)
    plt.figure(num=3, figsize=(8, 5))
    plt.plot(x, y_test)
    plt.plot(x, pre, color='red', linewidth=1, linestyle='--')
    plt.xlim((0, 60))
    plt.ylim((0, 1))
    plt.xlabel('测试样本个数')
    plt.ylabel('归一化后预测的数值')
    plt.show()




================================================
FILE: GABPNet1.py
================================================
import numpy as np
from sklearn.model_selection import train_test_split
import random
from scipy.optimize import fsolve
import matplotlib.pyplot as plt
import heapq
from tkinter import _flatten
# 读取txt中的数据,预处理去“,”
def load_data_wrapper(filename):
    lineData = []
    with open(filename) as txtData:
        lines = txtData.readlines()
        for line in lines:
            linedata = line.strip().split(',')
            lineData.append(linedata)
    return lineData
# 提出特征和标签,特征做输入,标签为输出
def splitData(dataset):
    Character= []
    Label = []
    for i in range(len(dataset)):
        Character.append([float(tk) for tk in dataset[i][1:-1]])
        Label.append(float(dataset[i][-1]))
    return Character, Label
#输入特征数据归一化
def max_min_norm_x(dataset):
    min_data = []
    for i in range(len(dataset)):
        min_data.append(min(dataset[i]))
    new_min = min(min_data)
    max_data = []
    for i in range(len(dataset)):
        max_data.append(max(dataset[i]))
    new_max = max(max_data)
    data = np.array(dataset)
    data_x =[]
    for x in np.nditer(data, op_flags=['readwrite']):
        #x[...] = 2 * (x -new_min)/(new_max-new_min)-1
        x[...] = (x - new_min) / (new_max - new_min)
        data_x.append(x[...])
    data_x3 = []
    for index in range(0, len(data_x), 3):
        data_x3.append([data_x[index], data_x[index+1], data_x[index+2]])
    return data_x3
#输出特征归一化
def max_min_norm_y(dataset):
    new_min = min(dataset)
    new_max = max(dataset)
    data_y = []
    for i in range(len(dataset)):
        y = (dataset[i] -new_min)/(new_max-new_min)
        #y = 2 * (dataset[i] - new_min) / (new_max - new_min) - 1
        data_y.append(y)
    return data_y

# 求染色体长度
def getEncodeLength(decisionvariables, delta):
     # 将每个变量的编码长度放入数组
     lengths = []
     uper = decisionvariables[0][1]
     low = decisionvariables[0][0]
     res = fsolve(lambda x: ((uper - low) / delta - 2 ** x + 1), 6)
     length0 = int(np.ceil(res[0]))
     res = fsolve(lambda x: ((uper - low) / delta - 2 ** x + 1), 2)
     length1 = int(np.ceil(res[0]))
     res = fsolve(lambda x: ((uper - low) / delta - 2 ** x + 1), 2)
     length2 = int(np.ceil(res[0]))
     res = fsolve(lambda x: ((uper - low) / delta - 2 ** x + 1), 1)
     length3= int(np.ceil(res[0]))
     lengths.append(length0)
     lengths.append(length1)
     lengths.append(length2)
     lengths.append(length3)
     return lengths,length0,length1,length2,length3
# 随机生成初始化种群
def getinitialPopulation(length,length0,length1,length2,length3, populationSize):
    chromsomes = np.zeros((populationSize, length), dtype=np.int)
    #print("len",length)  # 每个变量的值相加 11
    chromsomes0 = np.zeros((populationSize, length0), dtype=np.int)
    chromsomes1 = np.zeros((populationSize, length1), dtype=np.int)
    chromsomes2 = np.zeros((populationSize, length2), dtype=np.int)
    chromsomes3 = np.zeros((populationSize, length3), dtype=np.int)
    for popusize in range(populationSize):
        # np.random.randit()产生[0,2)之间的随机整数,第三个参数表示随机数的数量
        chromsomes[popusize, :] = np.random.randint(0, 2, length)
        chromsomes0[popusize, :] = chromsomes[popusize, :][0:6]
        chromsomes1[popusize, :] = chromsomes[popusize, :][6:8]
        chromsomes2[popusize, :] = chromsomes[popusize, :][8:10]
        chromsomes3[popusize, :] =chromsomes[popusize, :][10:11]
    return chromsomes,chromsomes0,chromsomes1,chromsomes2,chromsomes3
# 生成新种群
def getPopulation(population, populationSize):
    population0 = np.zeros((populationSize, 6), dtype=np.int)
    population1 = np.zeros((populationSize, 2), dtype=np.int)
    population2 = np.zeros((populationSize, 2), dtype=np.int)
    population3 = np.zeros((populationSize, 1), dtype=np.int)
    for popusize in range(populationSize):
        # np.random.randit()产生[0,2)之间的随机整数,第三个参数表示随机数的数量
        population0[popusize, :] = population[popusize, :][0:6]
        population1[popusize, :] = population[popusize, :][6:8]
        population2[popusize, :] = population[popusize, :][8:10]
        population3[popusize, :] = population[popusize, :][10:11]
    return population0, population1, population2, population3

 # 染色体解码得到表现形的解
def getDecode(population,population0,population1,population2,population3, encodelength, decisionvariables):
    population_decimal = (
                (population.dot(np.power(2, np.arange(sum(encodelength))[::-1])) / np.power(2, len(encodelength)) - 0.5) *
                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * (decisionvariables[0][0] +decisionvariables[0][1]))
    for i in range(population0.shape[1]):
        population_w1 = (
                (population0.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *
                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * (decisionvariables[0][0] +decisionvariables[0][1]))
    for i in range(population1.shape[1]):
        population_v1 = (
                (population1.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *
                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * (decisionvariables[0][0] + decisionvariables[0][1]))
    for i in range(population2.shape[1]):
        population_w2 = (
                (population2.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *
                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * (decisionvariables[0][0] + decisionvariables[0][1]))
    for i in range(population3.shape[1]):
        population_v2 = (
                (population3.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *
                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * ( decisionvariables[0][0] + decisionvariables[0][1]))
    return population_decimal,population_w1,population_v1,population_w2,population_v2  #(100,2) 100个种群中的两个变量转化成10进制

def getDecode1(p0,p1,p2,p3, decisionvariables):
    for i in range(len(p0)):
        population_w1 = (
                (p0.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *
                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * (decisionvariables[0][0] +decisionvariables[0][1]))
    for i in range(len(p1)):
        population_v1 = (
                (p1.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *
                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * (decisionvariables[0][0] + decisionvariables[0][1]))
    for i in range(len(p2)):
        population_w2 = (
                (p2.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *
                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * (decisionvariables[0][0] + decisionvariables[0][1]))
    for i in range(len(p3)):
        population_v2 = (
                (p3.dot(np.power(2, 0)) / np.power(2, 1) - 0.5) *
                (decisionvariables[0][1] - decisionvariables[0][0]) + 0.5 * ( decisionvariables[0][0] + decisionvariables[0][1]))
    return population_w1,population_v1,population_w2,population_v2

 # 得到每个个体的适应度值及累计概率
def getFitnessValue(func, decode,w1,v1,w2,v2):
    # 得到种群的规模和决策变量的个数
    popusize = decode.shape[0]  #100
    # 初始化适应度值空间
    fitnessValue = np.zeros((popusize, 1)) #(100,1)
    for popunum in range(popusize):
        fitnessValue[popunum] = func(x_train,y_train,w1,v1,w2,v2,3,2,1,popusize)[-1]
     # 得到每个个体被选择的概率
    probability = fitnessValue / np.sum(fitnessValue)
    # 得到每个染色体被选中的累积概率,用于轮盘赌算子使用
    cum_probability = np.cumsum(probability)
    return fitnessValue, cum_probability


 # 选择新的种群
def selectNewPopulation(decodepopu, cum_probability):
    # 获取种群的规模和
    m, n = decodepopu.shape  #100,33
    # 初始化新种群
    newPopulation = np.zeros((m, n))
    for i in range(m):
         # 产生一个0到1之间的随机数
        randomnum = np.random.random()
        # 轮盘赌选择
        for j in range(m):
            if (randomnum < cum_probability[j]):
                newPopulation[i] = decodepopu[j]
                break
    return newPopulation


 # 新种群交叉
def crossNewPopulation(newpopu, prob):
    m, n = newpopu.shape
    # uint8将数值转换为无符号整型
    numbers = np.uint8(m * prob)
     # 如果选择的交叉数量为奇数,则数量加1
    if numbers % 2 != 0:
        numbers = numbers + 1     # 初始化新的交叉种群
    updatepopulation = np.zeros((m, n), dtype=np.uint8)     # 随机生成需要交叉的染色体的索引号
    index = random.sample(range(m), numbers)     # 不需要交叉的染色体直接复制到新的种群中
    for i in range(m):
        if not index.__contains__(i):
            updatepopulation[i] = newpopu[i]
     # 交叉操作
    j = 0
    while j < numbers:
         # 随机生成一个交叉点,np.random.randint()返回的是一个列表
        crosspoint = np.random.randint(0, n, 1)
        crossPoint = crosspoint[0]
        # a = index[j]
        # b = index[j+1]
        updatepopulation[index[j]][0:crossPoint] = newpopu[index[j]][0:crossPoint]
        updatepopulation[index[j]][crossPoint:] = newpopu[index[j + 1]][crossPoint:]
        updatepopulation[index[j + 1]][0:crossPoint] = newpopu[j + 1][0:crossPoint]
        updatepopulation[index[j + 1]][crossPoint:] = newpopu[index[j]][crossPoint:]
        j = j + 2
    return updatepopulation


 # 变异操作
def mutation(crosspopulation, mutaprob):
     # 初始化变异种群
     mutationpopu = np.copy(crosspopulation)
     m, n = crosspopulation.shape
     # 计算需要变异的基因数量
     mutationnums = np.uint8(m * n * mutaprob)
     # 随机生成变异基因的位置
     mutationindex = random.sample(range(m * n), mutationnums)
     # 变异操作
     for geneindex in mutationindex:
         # np.floor()向下取整返回的是float型
         row = np.uint8(np.floor(geneindex / n))
         colume = geneindex % n
         if mutationpopu[row][colume] == 0:
             mutationpopu[row][colume] = 1
         else:
             mutationpopu[row][colume] = 0
     return mutationpopu

 # 找到重新生成的种群中适应度值最大的染色体生成新种群
def findMinPopulation(population, minevaluation, minSize):
     #将数组转换为列表
     minevalue = minevaluation.flatten()
     #maxevaluelist = maxevalue.tolist()
     minevaluelist = minevalue.tolist()
     # 找到前10个适应度最小的染色体的索引
     #maxIndex = map(maxevaluelist.index, heapq.nlargest(10, maxevaluelist))
     minIndex = map(minevaluelist.index, heapq.nsmallest(10, minevaluelist))

     index = list(minIndex)
     print("index",index)
     colume = population.shape[1] #11
     #print("colume",colume)
     # 根据索引生成新的种群
     minPopulation = np.zeros((minSize, colume))
     i = 0
     for ind in index:
         minPopulation[i] = population[ind]
         i = i + 1
     return np.uint8(minPopulation)

 # 适应度函数,神经网络训练误差最小为
def fitnessFunction(dataset, labelset, temp1,temp2,temp3,temp4,inputnum , hiddennum, outputnum,num):
    # x为步长
    x = 0.05
    if num !=0:#(输入为三维矩阵,第一维是种群数量,后两维是种群维度)
        Value1 = temp2.reshape(num, 1, hiddennum)
        Value2 = temp4.reshape(num, outputnum, outputnum)
        Weight1 = temp1.reshape(num, inputnum, hiddennum)
        Weight2 = temp3.reshape(num, hiddennum, outputnum)
        errors_net = []
        for v1, v2, w1, w2 in zip(Value1, Value2, Weight1, Weight2):
            errors = []
            for i in range(len(dataset)):
                # 输入数据
                inputset = np.mat(dataset[i]).astype(np.float64)
                # 数据标签
                outputset = np.mat(labelset[i]).astype(np.float64)
                # 隐层输入
                input1 = np.dot(inputset, w1).astype(np.float64)
                # 隐层输出
                output2 = sigmoid(input1 - v1).astype(np.float64)
                # 输出层输入
                input2 = np.dot(output2, w2).astype(np.float64)
                # 输出层输出,回归预测不带激活函数
                output3 = input2 - v2
                #误差绝对值
                error = abs(output3 - y_train[i])
                errors.append(error)

                # 更新公式由矩阵运算表示 用的是平方误差               
                g = outputset - output3  # 最后一层直接求导 ,为输出层阈值求导
                b = np.dot(g, np.transpose(w2))
                c = np.multiply(output2, 1 - output2)
                e = np.multiply(b, c)  # 隐藏层之间阈值

                value1_change = -x * e
                value2_change = -x * g
                weight1_change = x * np.dot(np.transpose(inputset), e)
                weight2_change = x * np.dot(np.transpose(output2), g) 
                v1 += value1_change
                v2 += value2_change
                w1 += weight1_change
                w2 += weight2_change
            error_net = np.array(min(errors)).flatten()
            errors_net.append(error_net)  
        return w1, w2, v1, v2, output3, error_net
    else: #输入是二维矩阵只有权重的维度
        Value1 = temp2.reshape(1, hiddennum)
        Value2 = temp4.reshape(outputnum, outputnum)
        Weight1 = temp1.reshape(inputnum, hiddennum)
        Weight2 = temp3.reshape(hiddennum, outputnum)
        for i in range(len(dataset)):
            # 输入数据
            inputset = np.mat(dataset[i]).astype(np.float64)
            # 数据标签
            outputset = np.mat(labelset[i]).astype(np.float64)
            # 隐层输入
            input1 = np.dot(inputset, Weight1).astype(np.float64)
            # 隐层输出
            output2 = sigmoid(input1 - Value1).astype(np.float64)
            # 输出层输入
            input2 = np.dot(output2, Weight2).astype(np.float64)
            # 输出层输出
            output3 = input2 - Value2

            # 更新公式由矩阵运算表示 用的是平方误差
            g = outputset - output3  # 最后一层直接求导 ,为输出层阈值求导
            b = np.dot(g, np.transpose(Weight2))
            c = np.multiply(output2, 1 - output2)
            e = np.multiply(b, c)  # 隐藏层之间阈值

            value1_change = -x * e
            value2_change = -x * g
            weight1_change = x * np.dot(np.transpose(inputset), e)
            weight2_change = x * np.dot(np.transpose(output2), g)

            # 更新参数
            Value1 += value1_change
            Value2 += value2_change
            Weight1 += weight1_change
            Weight2 += weight2_change
    return Weight1,Weight2,Value1,Value2

def parameter_initialization(opt):
    # 输入层与隐层的连接权重
    weight1 =opt[0:6]
    # 隐层与输出层的连接权重
    weight2 =opt[6:8]
    # 隐层阈值
    value1 = opt[8:10]
    # 输出层阈值
    value2 = opt[10:11]
    return weight1, weight2, value1, value2

def sigmoid(z):
    return 1 / (1 + np.exp(-z))

def test_process(dataset, labelset, weight1, weight2, value1, value2):
    pre_data = []
    for i in range(len(dataset)):
        # 计算每一个样例通过该神经网路后的预测值
        inputset = np.mat(dataset[i]).astype(np.float64)
        outputset = np.mat(labelset[i]).astype(np.float64)
        output2 = sigmoid(np.dot(inputset, weight1) - value1)
        output3 = np.dot(output2, weight2) - value2
        output3 = output3.tolist()
        pre_data.append(output3)
        pre_data = list(_flatten(pre_data))
    return pre_data


if __name__ == "__main__":
    #要打开的文件名
    iris_file = 'advertise.txt'
    #数据预处理
    Data = load_data_wrapper(iris_file)
    #分离特征标签值,x为数据集的feature数据,y为label.
    x, y = splitData(Data)
    #数据归一化
    x_norm = max_min_norm_x(x)
    #数据归一化
    y_norm = max_min_norm_y(y)    
    #分训练和测试集
    x_train, x_test, y_train, y_test = train_test_split(x_norm, y_norm, test_size=0.3)
    optimalvalue = []
    optimalvariables = []

    # 两个决策变量的上下界,多维数组之间必须加逗号
    decisionVariables = [[-5.0, 5.0]] * 4  # 神经网络参数: W1, W2, V1, V2
    # 精度
    delta = 0.0001
    # 获取染色体长度
    EncodeLength, L0, L1, L2, L3 = getEncodeLength(decisionVariables, delta)
    # 种群数量
    initialPopuSize = 10
    # 初始生成100个种群
    population, population0, population1, population2, population3 = getinitialPopulation(sum(EncodeLength), L0, L1, L2,L3, initialPopuSize)
    # 最大进化代数
    maxgeneration = 80
    # 交叉概率
    prob = 0.8
    # 变异概率
    mutationprob = 0.01
    # 新生成的种群数量
    maxPopuSize = 10

    for generation in range(maxgeneration):
        # 对种群解码得到表现形
        decode, W1, V1, W2, V2 = getDecode(population, population0, population1, population2, population3, EncodeLength, decisionVariables)
        # 得到适应度值和累计概率值
        evaluation, cum_proba = getFitnessValue(fitnessFunction, decode, W1, V1, W2, V2)
        # 选择新的种群
        newpopulations = selectNewPopulation(population, cum_proba)
        # 新种群交叉
        crossPopulations = crossNewPopulation(newpopulations, prob)
        # 变异操作
        mutationpopulation = mutation(crossPopulations, mutationprob)
        # 将父母和子女合并为新的种群
        totalpopulation = np.vstack((population, mutationpopulation))
        w11, v11, w22, v22 = getPopulation(totalpopulation, totalpopulation.shape[0])

        # 最终解码
        final_decode, W11, V11, W22, V22 = getDecode(totalpopulation, w11, v11, w22, v22, EncodeLength,decisionVariables)
        # 适应度评估
        final_evaluation, final_cumprob = getFitnessValue(fitnessFunction, final_decode, W11, V11, W22, V22)

        # 选出适应度最大的100个重新生成种群
        population = findMinPopulation(totalpopulation, final_evaluation, maxPopuSize)
        # 找到本轮中适应度最小的值
        optimalvalue.append(np.min(final_evaluation))
        index = np.where(final_evaluation == min(final_evaluation))
        optimalvariables.append((totalpopulation[index[0][0]]).tolist())
    #找出适应度的最小值
    optimalval = np.min(optimalvalue)
    #找出适应最小值所对应的索引
    index = np.where(optimalvalue == min(optimalvalue))
    optimalvar = optimalvariables[index[0][0]]
    optimalvar = np.array(optimalvar)
    #把这个个体还原成神经网络的权重、阈值
    weight11, weight21, value11, value21 = parameter_initialization(optimalvar)
    weight1, weight2, value1, value2 = getDecode1(weight11, weight21, value11, value21, decisionVariables)
    #训练
    for i in range(700):
        Weight1, Weight2, Value1, Value2 = fitnessFunction(x_train, y_train, weight1, weight2, value1, value2, 3, 2, 1, 0)
    #预测
    pre = test_process(x_test, y_test, Weight1, Weight2, Value1, Value2)
   
    #均方误差
    errors_std = np.std(np.array(pre) - np.array(y_test))
    #归一化还原均方误差
    pre_org = np.array(pre) * (max(y)-min(y)) + min(y)
    y_test_org = np.array(y_test) * (max(y) - min(y)) + min(y)
    errors_std_org = np.std(pre_org - y_test_org)
    print("errors_std:\n", errors_std)
    print("errors_std_org\n", errors_std_org)
    #显示测试图像
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    x = np.linspace(0, 60, 60)
    plt.figure(num=3, figsize=(8, 5))
    plt.plot(x, y_test)
    plt.plot(x, pre, color='red', linewidth=1, linestyle='--')
    plt.xlim((0, 60))
    plt.ylim((0, 1))
    plt.xlabel('测试样本个数')
    plt.ylabel('归一化后预测的数值')
    plt.show()


================================================
FILE: README.md
================================================
# GA-optimized-neural-network
python 用GA算法优化BP神经网络,预测回归问题

神经网络部分:
网络结构三层:(3,2,1)

数据集:
实验的数据集为:advertise.txt (三个特征输入,一个输出)
其数据形式如下所示:(即求前三个数与最后一个数的关系)
一共有200条数据,训练集和测试集的比例为7:3

1,230.1,37.8,69.2,22.1

2,44.5,39.3,45.1,10.4

3,17.2,45.9,69.3,9.3

4,151.5,41.3,58.5,18.5

5,180.8,10.8,58.4,12.9

用GA算法优化BP神经网络的权值和阈值:
种群数量10,迭代80次,交叉概率0.8,变异概率0.01,BP神经网络学习率:0.05,迭代500次:
测试样本60个的平均无误差,errors_std_org: 1.5342603366697878
![Iamge](https://github.com/yx868868/GA-optimized-neural-network/blob/main/pic/500%E6%AC%A1.png)

迭代700次
测试样本60个的平均无误差:errors_std_org:1.0408958068854353
![Iamge](https://github.com/yx868868/GA-optimized-neural-network/blob/main/pic/700%E6%AC%A1.png)

单独用BP神经网络,学习率:0.05,迭代500次:
测试样本60个的平均无误差,errors_std_org:3.2695353501231272
![Iamge](https://github.com/yx868868/GA-optimized-neural-network/blob/main/pic/BP500.png)

单独用BP神经网络,学习率:0.05,迭代700次:
测试样本60个的平均无误差,errors_std_org:1.812
![Iamge](https://github.com/yx868868/GA-optimized-neural-network/blob/main/pic/BP700%E6%AC%A14.png)



================================================
FILE: advertise.txt
================================================
1,230.1,37.8,69.2,22.1
2,44.5,39.3,45.1,10.4
3,17.2,45.9,69.3,9.3
4,151.5,41.3,58.5,18.5
5,180.8,10.8,58.4,12.9
6,8.7,48.9,75,7.2
7,57.5,32.8,23.5,11.8
8,120.2,19.6,11.6,13.2
9,8.6,2.1,1,4.8
10,199.8,2.6,21.2,10.6
11,66.1,5.8,24.2,8.6
12,214.7,24,4,17.4
13,23.8,35.1,65.9,9.2
14,97.5,7.6,7.2,9.7
15,204.1,32.9,46,19
16,195.4,47.7,52.9,22.4
17,67.8,36.6,114,12.5
18,281.4,39.6,55.8,24.4
19,69.2,20.5,18.3,11.3
20,147.3,23.9,19.1,14.6
21,218.4,27.7,53.4,18
22,237.4,5.1,23.5,12.5
23,13.2,15.9,49.6,5.6
24,228.3,16.9,26.2,15.5
25,62.3,12.6,18.3,9.7
26,262.9,3.5,19.5,12
27,142.9,29.3,12.6,15
28,240.1,16.7,22.9,15.9
29,248.8,27.1,22.9,18.9
30,70.6,16,40.8,10.5
31,292.9,28.3,43.2,21.4
32,112.9,17.4,38.6,11.9
33,97.2,1.5,30,9.6
34,265.6,20,0.3,17.4
35,95.7,1.4,7.4,9.5
36,290.7,4.1,8.5,12.8
37,266.9,43.8,5,25.4
38,74.7,49.4,45.7,14.7
39,43.1,26.7,35.1,10.1
40,228,37.7,32,21.5
41,202.5,22.3,31.6,16.6
42,177,33.4,38.7,17.1
43,293.6,27.7,1.8,20.7
44,206.9,8.4,26.4,12.9
45,25.1,25.7,43.3,8.5
46,175.1,22.5,31.5,14.9
47,89.7,9.9,35.7,10.6
48,239.9,41.5,18.5,23.2
49,227.2,15.8,49.9,14.8
50,66.9,11.7,36.8,9.7
51,199.8,3.1,34.6,11.4
52,100.4,9.6,3.6,10.7
53,216.4,41.7,39.6,22.6
54,182.6,46.2,58.7,21.2
55,262.7,28.8,15.9,20.2
56,198.9,49.4,60,23.7
57,7.3,28.1,41.4,5.5
58,136.2,19.2,16.6,13.2
59,210.8,49.6,37.7,23.8
60,210.7,29.5,9.3,18.4
61,53.5,2,21.4,8.1
62,261.3,42.7,54.7,24.2
63,239.3,15.5,27.3,15.7
64,102.7,29.6,8.4,14
65,131.1,42.8,28.9,18
66,69,9.3,0.9,9.3
67,31.5,24.6,2.2,9.5
68,139.3,14.5,10.2,13.4
69,237.4,27.5,11,18.9
70,216.8,43.9,27.2,22.3
71,199.1,30.6,38.7,18.3
72,109.8,14.3,31.7,12.4
73,26.8,33,19.3,8.8
74,129.4,5.7,31.3,11
75,213.4,24.6,13.1,17
76,16.9,43.7,89.4,8.7
77,27.5,1.6,20.7,6.9
78,120.5,28.5,14.2,14.2
79,5.4,29.9,9.4,5.3
80,116,7.7,23.1,11
81,76.4,26.7,22.3,11.8
82,239.8,4.1,36.9,12.3
83,75.3,20.3,32.5,11.3
84,68.4,44.5,35.6,13.6
85,213.5,43,33.8,21.7
86,193.2,18.4,65.7,15.2
87,76.3,27.5,16,12
88,110.7,40.6,63.2,16
89,88.3,25.5,73.4,12.9
90,109.8,47.8,51.4,16.7
91,134.3,4.9,9.3,11.2
92,28.6,1.5,33,7.3
93,217.7,33.5,59,19.4
94,250.9,36.5,72.3,22.2
95,107.4,14,10.9,11.5
96,163.3,31.6,52.9,16.9
97,197.6,3.5,5.9,11.7
98,184.9,21,22,15.5
99,289.7,42.3,51.2,25.4
100,135.2,41.7,45.9,17.2
101,222.4,4.3,49.8,11.7
102,296.4,36.3,100.9,23.8
103,280.2,10.1,21.4,14.8
104,187.9,17.2,17.9,14.7
105,238.2,34.3,5.3,20.7
106,137.9,46.4,59,19.2
107,25,11,29.7,7.2
108,90.4,0.3,23.2,8.7
109,13.1,0.4,25.6,5.3
110,255.4,26.9,5.5,19.8
111,225.8,8.2,56.5,13.4
112,241.7,38,23.2,21.8
113,175.7,15.4,2.4,14.1
114,209.6,20.6,10.7,15.9
115,78.2,46.8,34.5,14.6
116,75.1,35,52.7,12.6
117,139.2,14.3,25.6,12.2
118,76.4,0.8,14.8,9.4
119,125.7,36.9,79.2,15.9
120,19.4,16,22.3,6.6
121,141.3,26.8,46.2,15.5
122,18.8,21.7,50.4,7
123,224,2.4,15.6,11.6
124,123.1,34.6,12.4,15.2
125,229.5,32.3,74.2,19.7
126,87.2,11.8,25.9,10.6
127,7.8,38.9,50.6,6.6
128,80.2,0,9.2,8.8
129,220.3,49,3.2,24.7
130,59.6,12,43.1,9.7
131,0.7,39.6,8.7,1.6
132,265.2,2.9,43,12.7
133,8.4,27.2,2.1,5.7
134,219.8,33.5,45.1,19.6
135,36.9,38.6,65.6,10.8
136,48.3,47,8.5,11.6
137,25.6,39,9.3,9.5
138,273.7,28.9,59.7,20.8
139,43,25.9,20.5,9.6
140,184.9,43.9,1.7,20.7
141,73.4,17,12.9,10.9
142,193.7,35.4,75.6,19.2
143,220.5,33.2,37.9,20.1
144,104.6,5.7,34.4,10.4
145,96.2,14.8,38.9,11.4
146,140.3,1.9,9,10.3
147,240.1,7.3,8.7,13.2
148,243.2,49,44.3,25.4
149,38,40.3,11.9,10.9
150,44.7,25.8,20.6,10.1
151,280.7,13.9,37,16.1
152,121,8.4,48.7,11.6
153,197.6,23.3,14.2,16.6
154,171.3,39.7,37.7,19
155,187.8,21.1,9.5,15.6
156,4.1,11.6,5.7,3.2
157,93.9,43.5,50.5,15.3
158,149.8,1.3,24.3,10.1
159,11.7,36.9,45.2,7.3
160,131.7,18.4,34.6,12.9
161,172.5,18.1,30.7,14.4
162,85.7,35.8,49.3,13.3
163,188.4,18.1,25.6,14.9
164,163.5,36.8,7.4,18
165,117.2,14.7,5.4,11.9
166,234.5,3.4,84.8,11.9
167,17.9,37.6,21.6,8
168,206.8,5.2,19.4,12.2
169,215.4,23.6,57.6,17.1
170,284.3,10.6,6.4,15
171,50,11.6,18.4,8.4
172,164.5,20.9,47.4,14.5
173,19.6,20.1,17,7.6
174,168.4,7.1,12.8,11.7
175,222.4,3.4,13.1,11.5
176,276.9,48.9,41.8,27
177,248.4,30.2,20.3,20.2
178,170.2,7.8,35.2,11.7
179,276.7,2.3,23.7,11.8
180,165.6,10,17.6,12.6
181,156.6,2.6,8.3,10.5
182,218.5,5.4,27.4,12.2
183,56.2,5.7,29.7,8.7
184,287.6,43,71.8,26.2
185,253.8,21.3,30,17.6
186,205,45.1,19.6,22.6
187,139.5,2.1,26.6,10.3
188,191.1,28.7,18.2,17.3
189,286,13.9,3.7,15.9
190,18.7,12.1,23.4,6.7
191,39.5,41.1,5.8,10.8
192,75.5,10.8,6,9.9
193,17.2,4.1,31.6,5.9
194,166.8,42,3.6,19.6
195,149.7,35.6,6,17.3
196,38.2,3.7,13.8,7.6
197,94.2,4.9,8.1,9.7
198,177,9.3,6.4,12.8
199,283.6,42,66.2,25.5
200,232.1,8.6,8.7,13.4
Download .txt
gitextract_lsom15ox/

├── BPNet1.py
├── GABPNet1.py
├── README.md
└── advertise.txt
Download .txt
SYMBOL INDEX (28 symbols across 2 files)

FILE: BPNet1.py
  function load_data_wrapper (line 8) | def load_data_wrapper(filename):
  function splitData (line 17) | def splitData(dataset):
  function max_min_norm_x (line 25) | def max_min_norm_x(dataset):
  function max_min_norm_y (line 47) | def max_min_norm_y(dataset):
  function de_max_min_norm_y (line 58) | def de_max_min_norm_y(dataset1,dataset2):
  function parameter_initialization (line 68) | def parameter_initialization(x, y, z):
  function sigmoid (line 84) | def sigmoid(z):
  function relu (line 86) | def relu(z):
  function train_process (line 96) | def train_process(dataset, labelset, weight1, weight2, value1, value2):
  function test_process (line 131) | def test_process(dataset, labelset, weight1, weight2, value1, value2):

FILE: GABPNet1.py
  function load_data_wrapper (line 9) | def load_data_wrapper(filename):
  function splitData (line 18) | def splitData(dataset):
  function max_min_norm_x (line 26) | def max_min_norm_x(dataset):
  function max_min_norm_y (line 46) | def max_min_norm_y(dataset):
  function getEncodeLength (line 57) | def getEncodeLength(decisionvariables, delta):
  function getinitialPopulation (line 76) | def getinitialPopulation(length,length0,length1,length2,length3, populat...
  function getPopulation (line 92) | def getPopulation(population, populationSize):
  function getDecode (line 106) | def getDecode(population,population0,population1,population2,population3...
  function getDecode1 (line 128) | def getDecode1(p0,p1,p2,p3, decisionvariables):
  function getFitnessValue (line 148) | def getFitnessValue(func, decode,w1,v1,w2,v2):
  function selectNewPopulation (line 163) | def selectNewPopulation(decodepopu, cum_probability):
  function crossNewPopulation (line 180) | def crossNewPopulation(newpopu, prob):
  function mutation (line 209) | def mutation(crosspopulation, mutaprob):
  function findMinPopulation (line 229) | def findMinPopulation(population, minevaluation, minSize):
  function fitnessFunction (line 251) | def fitnessFunction(dataset, labelset, temp1,temp2,temp3,temp4,inputnum ...
  function parameter_initialization (line 333) | def parameter_initialization(opt):
  function sigmoid (line 344) | def sigmoid(z):
  function test_process (line 347) | def test_process(dataset, labelset, weight1, weight2, value1, value2):
Condensed preview — 4 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (35K chars).
[
  {
    "path": "BPNet1.py",
    "chars": 5936,
    "preview": "# BP ,更新阈值和权重,回归预测问题最后一层不带激活函数\r\n# coding: UTF-8\r\nimport numpy as np\r\nfrom sklearn.model_selection import train_test_spli"
  },
  {
    "path": "GABPNet1.py",
    "chars": 18582,
    "preview": "import numpy as np\r\nfrom sklearn.model_selection import train_test_split\r\nimport random\r\nfrom scipy.optimize import fsol"
  },
  {
    "path": "README.md",
    "chars": 998,
    "preview": "# GA-optimized-neural-network\npython 用GA算法优化BP神经网络,预测回归问题\n\n神经网络部分:\n网络结构三层:(3,2,1)\n\n数据集:\n实验的数据集为:advertise.txt (三个特征输入,一个"
  },
  {
    "path": "advertise.txt",
    "chars": 4727,
    "preview": "1,230.1,37.8,69.2,22.1\r\n2,44.5,39.3,45.1,10.4\r\n3,17.2,45.9,69.3,9.3\r\n4,151.5,41.3,58.5,18.5\r\n5,180.8,10.8,58.4,12.9\r\n6,8"
  }
]

About this extraction

This page contains the full source code of the yx868868/GA-optimized-neural-network GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 4 files (29.5 KB), approximately 11.7k tokens, and a symbol index with 28 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!