Boltzamnn随机性神经网络

[color=green]BM网络工作规则[/color]
设网络有N个神经元,各神经元之间连接权为{wij},各神经元的输出阈值为{θi},输出为{ui},神经元的内部状态为{Hi} ,i,j=1,2,…N
取T(t=0)=T0,给{wij}, {θi} 赋 [-1,1] 内的随机值,并使wij=wji;
[list=1]
[*]从N个神经元中随机选取一个神经元i
[*]求神经元 i的内部状态,Hi=∑wijuj(t)-θi,j=1…n, j≠i
[*]按下式所示概率将神经元 i的状态更新为1
p[ui(t+1)=1]=1/(1+exp(-Hi(t)/T))
p[ui(t+1)=0]=1 - p[ui(t+1)=1]
[*]i以外的神经元输出保持不变
uj(t+1) = uj(t), j=1,2,…,N, j≠i
[*]令 t=t+1,按下式计算新的温度参数T(t+1)
T(t+1)=T0/log(t+1)——降温方案
[*]返回到1),直到T < Td(预先给定)
T0,Td凭经验给出
现已证明,按上面降温方案,能保证网络收敛于全局最小值
缺点:收敛时间太长
快速降温方案:T(t) = T0 /(1+t)
[/list]

public class Boltzmann {

int []initCell={0,0,1,0};
int cellNumber =4;
static int runTimes=1000;
double [][]w=new double [cellNumber][cellNumber];
double [] value=new double [cellNumber];
double temp=0;;
static double initTemperture =100;
static double objectTemperture = 5.00;
static double tempTemperture=initTemperture;
int [][] statusValue={{1,1,1,1},{1,1,1,0},{1,1,0,0},{1,0,0,0},{0,0,0,0},
{0,1,1,1},{0,1,1,0},{0,1,0,0},{0,0,1,1},{0,0,0,1},
{1,0,1,1},{1,0,0,1},{1,0,1,0},{0,1,0,1},{0,1,0,0},{0,1,1,0}};

int [] statusCount=new int[cellNumber*cellNumber];
double [] statusEnergy=new double[cellNumber*cellNumber];
int t=0;
double[] theoryFrequency=new double[cellNumber*cellNumber];
double[] practicalFrequency=new double[cellNumber*cellNumber];
static double z=0;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Boltzmann bm=new Boltzmann();
bm.init();
bm.calculate1(runTimes, initTemperture);
while(tempTemperture>objectTemperture){
bm.calculate2();
}
bm.calculate1(runTimes, objectTemperture);
}

private void init() {
// TODO Auto-generated method stub
for(int i=0;i<w.length;i++){
for(int j=0;j<i;j++){
w[i][j]=w[j][i]=new Random().nextInt(3)-1;
}
value[i]=new Random().nextInt(3)-1;
}
this.calculateEnergy();
}

private void calculate1(int runTimes,double temperture) {
// TODO Auto-generated method stub
for(int i=0;i<runTimes;i++){
int start=(int) (Math.random()*4);
double p1=0;
for(int j=0;j<w.length;j++){
if(j!=start)
temp +=w[start][j]*initCell[j];
}
temp -=value[start];
//if(temp>=0){
//initCell[start]=1;
//}else{
p1=1/(1+Math.exp(-temp/temperture));
if(Math.random()<p1)
initCell[start]=1;
else
initCell[start]=0;
//}

for(int j=0;j<statusValue.length;j++){
if(Arrays.equals(statusValue[j],initCell))
statusCount[j]+=1;
}
temp=0;
}

this.calculateFrequency(temperture);

System.out.println();
for(int i=0;i<theoryFrequency.length;i++){
System.out.println("状态:"+i+" 能量:"+statusEnergy[i]+" 实际概率:"+practicalFrequency[i]+" 理论概率:"+theoryFrequency[i]);
}
statusCount=new int [theoryFrequency.length];
z=0;
}

private void calculate2(){
int start=(int) (Math.random()*4);
double p1=0;
for(int i=0;i<w.length;i++){
if(i!=start)
temp +=w[start][i]*initCell[i];
}
temp -=value[start];
//if(temp>=0){
// initCell[start]=1;
//}else{
p1=1/(1+Math.exp(-temp/tempTemperture));
if(Math.random()<p1)
initCell[start]=1;
else
initCell[start]=0;
//}
t++;
tempTemperture=initTemperture*Math.log(2)/Math.log(t+1);
temp=0;

}
private void calculateEnergy(){
double q=0,e=0;
for(int i=0;i<statusValue.length;i++){

for(int j=0;j<initCell.length;j++){
for(int k=0;k<initCell.length;k++){
if(k!=j)
e+=w[j][k]*statusValue[i][j]*statusValue[i][k];
}
q+=value[j]*statusValue[i][j];
}
statusEnergy[i]=-0.5*e+q;
q=0;e=0;
}
}
private void calculateFrequency(double temperture){
for(Double b:statusEnergy){
z+=Math.exp(-b/temperture);
}
for(int i=0;i<theoryFrequency.length;i++){
theoryFrequency[i]=(1/z)*Math.exp(-statusEnergy[i]/temperture);
practicalFrequency[i]=statusCount[i]*1.0/runTimes;
}

}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值