粒子群算法优化BP神经网络

刚学粒子群算法,然后用粒子群算法优化神经网络的隐藏节点数,代码写的不是太好,如果代码有问题,请大家多多指教。第一次写博客,多多包涵。

本文的粒子群算法用的是标准粒子群算法,权重更新采用线性递减策略。

这是SSE图


粒子群算法优化BP神经网络_第1张图片

import numpy as np
import random 
import matplotlib.pyplot as plt
import neurolab as nl
train_x =[]
d=[]
samplescount=1000
myrndsmp=np.random.rand(samplescount)
for yb_i in xrange(0,samplescount):
    train_x.append([myrndsmp[yb_i]*4*np.pi-2*np.pi])
for yb_i in xrange(0,samplescount):
    d.append(np.sin(train_x[yb_i])*0.5+np.cos(train_x[yb_i])*0.5)
myinput=np.array(train_x)   
mytarget=np.array(d)
#PSO参数设置
class PSO():
    def __init__(self,max_iter):
        #self.w = 0.8  
        self.c1 = 2   
        self.c2 = 2   
        self.pN =10               #粒子数量
        self.dim = 1              #搜索维度
        self.max_iter = max_iter    #迭代次数
        self.X = np.ones((self.pN,self.dim))       #所有粒子的位置和速度
        self.V = np.zeros((self.pN,self.dim))
        self.pbest = np.zeros((self.pN,self.dim))   #个体经历的最佳位置和全局最佳位置
        self.gbest = np.zeros((1,self.dim))
        self.p_fit = np.zeros(self.pN)              #每个个体的历史最佳适应值
        self.fit = 1e10             #全局最佳适应值
        self.wmax=0.9
        self.wmin=0.4
#目标函数
    def fun(self,err):
        fitness=err
        return fitness
#初始化种群
    def init_Population(self):
        for i in range(self.pN):
            for j in range(self.dim):
                self.X[i][j] = random.uniform(1,20)
                self.V[i][j] = random.uniform(0,2)
            self.pbest[i] = self.X[i]
          
            for x in self.pbest[i]:
                return x
            bpnet = nl.net.newff([[-2*np.pi, 2*np.pi]], [int(x)+1, 1])
            err = bpnet.train(myinput, mytarget, epochs=800, show=100, goal=0.02)
            #out=net.sim(input)
            tmp = self.fun(err)
            self.p_fit[i] = tmp
            if(tmp < self.fit):
                self.fit = tmp
                self.gbest = self.X[i]
    
#更新粒子位置
    def iterator(self):
        fitness = []
        for t in range(self.max_iter):
            w=self.wmax-(self.wmax-self.wmin)*(float(t)/self.max_iter)
            for i in range(self.pN):
                for x in self.pbest[i]:
                    return x
                print x
                bpnet = nl.net.newff([[-2*np.pi, 2*np.pi]], [int(x)+1, 1])
                err = bpnet.train(myinput, mytarget, epochs=800, show=100, goal=0.02)
                temp = self.fun(err)
                if(temp



你可能感兴趣的:(粒子群算法优化BP神经网络)