玻尔兹曼机MATLAB仿真

最近人工神经网络要交作业,仿真玻尔兹曼机
我吧自己的代码共享一下,造福一下,下一代

function [result_p1,result_p0]=find_q(w12,w13,w23,theat1,theat2,theat3,T)
%寻找出u1
%s0=000
v1=0;v2=0;v3=0;
u1_s0=w12*v2+w13*v3-theat1;
u2_s0=w12*v1+w23*v3-theat2;
u3_s0=w13*v1+w23*v2-theat3;
%s1=001 v1,v2,v3=001
v1=0;v2=0;v3=1;
u1_s1=w12*v2+w13*v3-theat1;
u2_s1=w12*v1+w23*v3-theat2;
u3_s1=w13*v1+w23*v2-theat3;
%s2=010
v1=0;v2=1;v3=0;
u1_s2=w12*v2+w13*v3-theat1;
u2_s2=w12*v1+w23*v3-theat2;
u3_s2=w13*v1+w23*v2-theat3;
%s3=011
v1=0;v2=1;v3=1;
u1_s3=w12*v2+w13*v3-theat1;
u2_s3=w12*v1+w23*v3-theat2;
u3_s3=w13*v1+w23*v2-theat3;
%s4=100
v1=1;v2=0;v3=0;
u1_s4=w12*v2+w13*v3-theat1;
u2_s4=w12*v1+w23*v3-theat2;
u3_s4=w13*v1+w23*v2-theat3;
%s5=101
v1=1;v2=0;v3=1;
u1_s5=w12*v2+w13*v3-theat1;
u2_s5=w12*v1+w23*v3-theat2;
u3_s5=w13*v1+w23*v2-theat3;
%s6=110
v1=1;v2=1;v3=0;
u1_s6=w12*v2+w13*v3-theat1;
u2_s6=w12*v1+w23*v3-theat2;
u3_s6=w13*v1+w23*v2-theat3;
%s7=111
v1=1;v2=1;v3=1;
u1_s7=w12*v2+w13*v3-theat1;
u2_s7=w12*v1+w23*v3-theat2;
u3_s7=w13*v1+w23*v2-theat3;
%将结果显示出来

result_s0_u1=1/(1+exp(-u1_s0/T));
result_s0_u2=1/(1+exp(-u2_s0/T));
result_s0_u3=1/(1+exp(-u3_s0/T));
result_s0=[result_s0_u1;result_s0_u2;result_s0_u3];

result_s1_u1=1/(1+exp(-u1_s1/T));
result_s1_u2=1/(1+exp(-u2_s1/T));
result_s1_u3=1/(1+exp(-u3_s1/T));
result_s1=[result_s1_u1;result_s1_u2;result_s1_u3];

result_s2_u1=1/(1+exp(-u1_s2/T));
result_s2_u2=1/(1+exp(-u2_s2/T));
result_s2_u3=1/(1+exp(-u3_s2/T));
result_s2=[result_s2_u1;result_s2_u2;result_s2_u3];

result_s3_u1=1/(1+exp(-u1_s3/T));
result_s3_u2=1/(1+exp(-u2_s3/T));
result_s3_u3=1/(1+exp(-u3_s3/T));
result_s3=[result_s3_u1;result_s3_u2;result_s3_u3];

result_s4_u1=1/(1+exp(-u1_s4/T));
result_s4_u2=1/(1+exp(-u2_s4/T));
result_s4_u3=1/(1+exp(-u3_s4/T));
result_s4=[result_s4_u1;result_s4_u2;result_s4_u3];

result_s5_u1=1/(1+exp(-u1_s5/T));
result_s5_u2=1/(1+exp(-u2_s5/T));
result_s5_u3=1/(1+exp(-u3_s5/T));
result_s5=[result_s5_u1;result_s5_u2;result_s5_u3];

result_s6_u1=1/(1+exp(-u1_s6/T));
result_s6_u2=1/(1+exp(-u2_s6/T));
result_s6_u3=1/(1+exp(-u3_s6/T));
result_s6=[result_s6_u1;result_s6_u2;result_s6_u3];

result_s7_u1=1/(1+exp(-u1_s7/T));
result_s7_u2=1/(1+exp(-u2_s7/T));
result_s7_u3=1/(1+exp(-u3_s7/T));
result_s7=[result_s7_u1;result_s7_u2;result_s7_u3];
%为了求出p0所做的矩阵
E=[1;1;1];
%这样可以求出他们的激活概率
result_p1=[result_s0,result_s1,result_s2,result_s3,result_s4,result_s5,result_s6,result_s7];
result_p0=[E-result_s0,E-result_s1,E-result_s2,E-result_s3,E-result_s4,E-result_s5,E-result_s6,E-result_s7];
function [result_sum_P]=find_q2(result_p1,result_p0)%在某一个确定的温度下求状态转移图
%s000的整个变换过程
us0_0=(result_p0(1,1)+result_p0(2,1)+result_p0(3,1))/3;%000000
us0_1=(result_p0(1,1)*0+result_p0(2,1)*0+result_p1(3,1)*1)/3;%000001
us0_2=(result_p0(1,1)*0+result_p1(2,3)*1+result_p0(3,1)*0)/3;%000010
us0_3=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%000011
us0_4=(result_p1(1,1)*1+result_p0(1,1)*0+result_p0(3,1)*0)/3;%000100
us0_5=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%000101
us0_6=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%000110
us0_7=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%000111
ust0=[us0_0;us0_1;us0_2;us0_3;us0_4;us0_5;us0_6;us0_7];


%s=001的整个变换过程
us1_0=(result_p0(1,2)*0+result_p0(2,2)*0+result_p0(3,2)*1)/3;%001000
us1_1=(result_p0(1,2)+result_p0(2,2)+result_p1(3,2))/3;%001001
us1_2=(result_p0(1,1)*0+result_p1(2,3)*0+result_p0(3,1)*0)/3;%001010
us1_3=(result_p0(1,1)*0+result_p1(2,4)*1+result_p0(1,1)*0)/3;%001011
us1_4=(result_p1(1,1)*0+result_p0(1,1)*0+result_p0(3,1)*0)/3;%001100
us1_5=(result_p1(1,6)*1+result_p0(1,1)*0+result_p0(1,1)*0)/3;%001101
us1_6=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%001110
us1_7=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%001111
ust1=[us1_0;us1_1;us1_2;us1_3;us1_4;us1_5;us1_6;us1_7];


%s=010的整个变换过程
us2_0=(result_p0(1,3)*0+result_p0(2,3)*1+result_p0(3,3)*0)/3;%010000 %解释:result_p(2,3);其中2表示第v2个位置发生了变换,3表示个这个位置发生变换后 *1 表示触发
us2_1=(result_p0(1,1)*0+result_p0(2,1)*0+result_p1(3,2)*0)/3;%010001
us2_2=(result_p0(1,3)+result_p1(2,3)+result_p0(3,3))/3;%010010
us2_3=(result_p0(1,1)*0+result_p0(1,1)*0+result_p1(3,3)*1)/3;%010011
us2_4=(result_p1(1,1)*0+result_p0(1,1)*0+result_p0(3,1)*0)/3;%010100
us2_5=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%010101
us2_6=(result_p1(1,3)*1+result_p0(1,1)*0+result_p0(1,1)*0)/3;%010110
us2_7=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%010111
ust2=[us2_0;us2_1;us2_2;us2_3;us2_4;us2_5;us2_6;us2_7];

%s=011的整个变换过程
us3_0=(result_p0(1,1)*0+result_p0(2,1)*0+result_p0(3,1)*0)/3;%011000
us3_1=(result_p0(1,1)*0+result_p0(2,4)*1+result_p1(3,2)*0)/3;%011001
us3_2=(result_p0(1,1)*0+result_p1(2,3)*0+result_p0(3,4)*1)/3;%011010
us3_3=(result_p0(1,4)+result_p1(2,4)+result_p1(3,4))/3;%011011
us3_4=(result_p1(1,1)*0+result_p0(1,1)*0+result_p0(3,1)*0)/3;%011100
us3_5=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%011101
us3_6=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%011110
us3_7=(result_p1(1,4)*1+result_p0(1,1)*0+result_p0(1,1)*0)/3;%011111
ust3=[us3_0;us3_1;us3_2;us3_3;us3_4;us3_5;us3_6;us3_7];

%s4变换过程
us4_0=(result_p0(1,5)*1+result_p0(2,1)*0+result_p0(3,1)*0)/3;%100000
us4_1=(result_p0(1,1)*0+result_p0(2,1)*0+result_p1(3,2)*0)/3;%100001
us4_2=(result_p0(1,1)*0+result_p1(2,3)*0+result_p0(3,1)*0)/3;%100010
us4_3=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%100011
us4_4=(result_p1(1,5)+result_p0(2,5)+result_p0(3,5))/3;%100100
us4_5=(result_p0(1,1)*0+result_p0(1,1)*0+result_p1(3,5)*1)/3;%100101
us4_6=(result_p0(1,1)*0+result_p1(2,5)*1+result_p0(1,1)*0)/3;%100110
us4_7=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%100111
ust4=[us4_0;us4_1;us4_2;us4_3;us4_4;us4_5;us4_6;us4_7];




%s5变换过程
us5_0=(result_p0(1,1)*0+result_p0(2,1)*0+result_p0(3,1)*0)/3;%101000
us5_1=(result_p0(1,6)*1+result_p0(2,1)*0+result_p1(3,2)*0)/3;%101001
us5_2=(result_p0(1,1)*0+result_p1(2,3)*0+result_p0(3,1)*0)/3;%101010
us5_3=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%101011
us5_4=(result_p1(1,1)*0+result_p0(1,1)*0+result_p0(3,6)*1)/3;%101100
us5_5=(result_p1(1,6)+result_p0(2,6)+result_p1(3,6))/3;%101101
us5_6=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%101110
us5_7=(result_p0(1,1)*0+result_p1(2,6)*1+result_p0(1,1)*0)/3;%101111
ust5=[us5_0;us5_1;us5_2;us5_3;us5_4;us5_5;us5_6;us5_7];



%s6变换过程
us6_0=(result_p0(1,1)*0+result_p0(2,1)*0+result_p0(3,1)*0)/3;%110000
us6_1=(result_p0(1,1)*0+result_p0(2,1)*0+result_p1(3,2)*0)/3;%110001
us6_2=(result_p0(1,7)*1+result_p1(2,3)*0+result_p0(3,1)*0)/3;%110010
us6_3=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%110011
us6_4=(result_p1(1,1)*0+result_p0(2,7)*1+result_p0(3,1)*0)/3;%110100
us6_5=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(1,1)*0)/3;%110101
us6_6=(result_p1(1,7)+result_p1(2,7)+result_p0(3,7))/3;%110110
us6_7=(result_p0(1,1)*0+result_p0(1,1)*0+result_p1(3,7)*1)/3;%110111
ust6=[us6_0;us6_1;us6_2;us6_3;us6_4;us6_5;us6_6;us6_7];



%s7变换过程
us7_0=(result_p0(1,1)*0+result_p0(2,1)*0+result_p0(3,1)*0)/3;%111000
us7_1=(result_p0(1,1)*0+result_p0(2,1)*0+result_p1(3,2)*0)/3;%111001
us7_2=(result_p0(1,1)*0+result_p1(2,3)*0+result_p0(3,1)*0)/3;%111010
us7_3=(result_p0(1,8)*1+result_p0(1,1)*0+result_p0(1,1)*0)/3;%111011
us7_4=(result_p1(1,1)*0+result_p0(1,1)*0+result_p0(3,1)*0)/3;%111100
us7_5=(result_p0(1,1)*0+result_p0(2,8)*1+result_p0(1,1)*0)/3;%111101
us7_6=(result_p0(1,1)*0+result_p0(1,1)*0+result_p0(3,8)*1)/3;%111110
us7_7=(result_p1(1,8)+result_p1(2,8)+result_p1(3,8))/3;%111111
ust7=[us7_0;us7_1;us7_2;us7_3;us7_4;us7_5;us7_6;us7_7];

%汇总
result_sum_P=[ust0,ust1,ust2,ust3,ust4,ust5,ust6,ust7]';
function Pt_1=find_q3(P,result_sum)%算在同一个温度下的稳态的概率PT1
 qt0_0=P*result_sum(:,1);%状态s0的概率
 qt0_1=P*result_sum(:,2);%状态s1的概率
 qt0_2=P*result_sum(:,3);%状态s2的概率
 qt0_3=P*result_sum(:,4);%状态s3的概率
 qt0_4=P*result_sum(:,5);%状态s4的概率
 qt0_5=P*result_sum(:,6);%状态s5的概率
 qt0_6=P*result_sum(:,7);%状态s6的概率
 qt0_7=P*result_sum(:,8);%状态s7的概率
 Pt_1=[qt0_0,qt0_1,qt0_2,qt0_3,qt0_4,qt0_5,qt0_6,qt0_7];
%做这个函数的目的是在一个温度下,他最终的稳定温度
function [Pt_Ti]=find_q4(result_sum,P)
% for n=500:-1:1
%      Pt_Ti=find_q3(P,result_sum);
%      P=Pt_Ti;
% end

   Pt_Ti=find_q3(P,result_sum);%t=13时刻的时候不再发生变化
   while(isequal(P,Pt_Ti)==0)%isequal(a,b)相等返回1,不相等返回0 这个地方是用来判断循环截止的
       P=Pt_Ti;
       Pt_Ti=find_q3(P,result_sum);
   end
%看来是应为用到这While循环语句的问题
%温度为T下的最终结果
clear all
clc
T=1;%初始温度
%P=[0.125,0.125,0.125,0.125,0.125,0.125,0.125,0.125];
P=[0.2,0.1,0.01,0.2,0.3,0.1,0.05,0.04];
for T=1:-0.01:0.02 %求出各个时刻的稳态
[result_p1,result_p0]=find_q(0.2,-0.6,0.4,0.9,0.2,-0.7,T);    
%[result_p1,result_p0]=find_q(0.1,-0.7,0.4,-0.9,-0.2,0.3,T);
result_sum_P=find_q2(result_p1,result_p0);
Pt_1=find_q4(result_sum_P,P);%这个时候也就说明已经是稳定了
%这个将上一个状态的稳定的值,赋值为下一个状态的初始值
P=Pt_1;   
T
P
end

个人建议:还是老老老实自己吧原理什么的,都搞明白再来看,不然程序出现问题,你也不知道怎么改。

你可能感兴趣的:(【MATLAB编程】,【生活二三事】)