Thuật toán luyện khe trong quá trình luyện mạng nơron - 17


deltb{i}=(c.*e{i}).*ones(unl(i+1),1);

b{i}=b{i}+deltb{i};% The modify

biases


end J=J/2;


end

if mod(sum,100)==0 sum1=sum1+1; data(sum1,1)=sum; data(sum1,2)=J;

end

end

% Example a value

x{1} = [22 35 1 6 36]';

for i=1:L-1

s{i}=w{i}*x{i}+b{i};

x{i+1}=g(s{i});

end

x{i+1}

%x{L}

semilogy(data(:,1),data(:,2)),grid

%-------------------------------


PHỤ LỤC 3: MÃ NGUỒN CHƯƠNG TRÌNH LUYỆN MẠNG NƠRON VỚI BƯỚC HỌC VƯỢT KHE ĐỂ NHẬN DẠNG CHỮ VIẾT

dinhnghia.h

/*

*=======================================================================*

*------------------------------------------------------------------------

*

* DE TAI : HUAN LUYEN MANG NO-RON VOI BUOC HOC TINH THEO NGUYEN LY VUOT KHE *

* NGON NGU : C

* TRINH DICH : VISUAL C++

* TEN TEP : dinhnghia.h

*-----------------------------------------------------------------------*

*=======================================================================*

*/

#define SIGMF(x) 1/(1 + exp(-(double)x))//HAM KICH HOAT NO RON

#define DSIGM(y) (float)(y)*(1.0-y))//DAO HAM CUA HAM KICH HOAT NO RON

#define SLNRLV

35

//

SO LUONG NO RON LOP VAO


#define SLNRLA

5

//

SO LUONG NO RON LOP AN

#define SLNRLR

10

//

SO LUONG NO RON LOP RA

#define EPSILON

0.06

//

SAI SO TRUNG BINH BINH PHUONG

DE DUNG QUA

TRINH LUYEN MANG





#define SLMHL

18

//

SO LUONG MAU HUAN LUYEN MANG


#define STEPinit

0.5

//

GIA TRI KHOI TAO BUOC HOC, CO

THE DUNG CHO

NHIEU TRUONG HOP





#define DCBH

0.0001

//

DIEU CHINH BUOC HOC


#define MSDCBH

5

//

MAU SO DIEU CHINH BUOC HOC


#define TSDCBH

5

//

TU SO DIEU CHINH BUOC HOC


#define BLTD

30000

//

BUOC LAP TOI DA


#define FD

1e-1

//



Có thể bạn quan tâm!

Xem toàn bộ 150 trang tài liệu này.

Thuật toán luyện khe trong quá trình luyện mạng nơron - 17

taphuanluyen.h

/*

*====================================================================*

*-----------------------------------------------------------------------*

* DE TAI : HUAN LUYEN MANG NO-RON VOI BUOC HOC TINH THEO NGUYEN LY VUOT KHE *

* NGON NGU : C

* TRINH DICH : VISUAL C++

* TEN TEP : taphuanluyen.h

*-----------------------------------------------------------------------*

*=======================================================================*

*/

#include "dinhnghia.h"

//TAP MAU HUAN LUYEN DAU VAO

int TAPHUANLUYEN[SLMHL][SLNRLV] =

{

{ 0,1,1,1,1,1,0, /* 0 */

1,0,0,0,0,0,1,

1,0,0,0,0,0,1,

1,0,0,0,0,0,1,

0,1,1,1,1,1,0 },


{ 0,0,0,0,0,0,0, /* 1 */

0,1,0,0,0,0,1,

1,1,1,1,1,1,1,

0,0,0,0,0,0,1,


0,0,0,0,0,0,0 },

{ 0,1,0,0,0,0,1, /* 2 */

1,0,0,0,0,1,1,

1,0,0,0,1,0,1,

1,0,0,1,0,0,1,

0,1,1,0,0,0,1 },


{

1,0,0,0,0,1,0,

/*

3

*/


1,0,0,0,0,0,1,





1,0,0,1,0,0,1,





1,1,1,0,1,0,1,





1,0,0,0,1,1,0

},



{

0,0,0,1,1,0,0,

/*

4

*/


0,0,1,0,1,0,0,





0,1,0,0,1,0,0,





1,1,1,1,1,1,1,





0,0,0,0,1,0,0

},



{

1,1,1,0,0,1,0,

/*

5

*/


1,0,1,0,0,0,1,





1,0,1,0,0,0,1,





1,0,1,0,0,0,1,





1,0,0,1,1,1,0

},



{

0,0,1,1,1,1,0,

/*

6

*/


0,1,0,1,0,0,1,





1,0,0,1,0,0,1,





1,0,0,1,0,0,1,





0,0,0,0,1,1,0

},



{

1,0,0,0,0,0,0,

/*

7

*/


1,0,0,0,0,0,0,





1,0,0,1,1,1,1,





1,0,1,0,0,0,0,





1,1,0,0,0,0,0

},



{

0,1,1,0,1,1,0,

/*

8

*/


1,0,0,1,0,0,1,





1,0,0,1,0,0,1,





1,0,0,1,0,0,1,





0,1,1,0,1,1,0

},



{

0,1,1,0,0,0,0,

/*

9

*/


1,0,0,1,0,0,1,





1,0,0,1,0,0,1,





1,0,0,1,0,1,0,





0,1,1,1,1,0,0

},



{

1,1,1,1,0,0,0,

/*

4

*/


0,0,0,1,0,0,0,





0,0,0,1,0,0,0,





0,0,0,1,0,0,0,





1,1,1,1,1,1,1

},



{

1,1,1,1,0,1,0,

/*

5

*/


1,0,0,1,0,0,1,





1,0,0,1,0,0,1,





1,0,0,1,0,0,1,





1,0,0,0,1,1,0

},




{ 1,0,0,0,0,0,0, /* 7 */

1,0,0,0,0,0,0,

1,0,0,1,0,0,0,

1,1,1,1,1,1,1,

0,0,0,1,0,0,0 },


{ 0,1,0,0,0,1,0, /* 3 */

1,0,0,0,0,0,1,

1,0,0,1,0,0,1,

1,0,1,0,1,0,1,

0,1,1,0,1,1,0 },


{ 1,0,0,0,0,1,1, /* 2 */

1,0,0,0,1,0,1,

1,0,0,1,0,0,1,

1,0,1,0,0,0,1,

1,1,0,0,0,0,1 },


{ 1,1,1,1,0,0,0, /* 4 */

0,0,0,1,0,0,0,

0,0,0,1,0,0,0,

1,1,1,1,1,1,1,

0,0,0,1,0,0,0 },


{ 1,1,1,1,1,1,1, /* 0 */

1,0,0,0,0,0,1,

1,0,0,0,0,0,1,

1,0,0,0,0,0,1,

1,1,1,1,1,1,1 },


{ 0,1,1,0,0,0,1, /* 9 */

1,0,0,1,0,0,1,

1,0,0,1,0,0,1,

1,0,0,1,0,0,1,

0,1,1,1,1,1,1 } };


//DAU RA MONG MUON TUONG UNG int DRMM[SLMHL][SLNRLR] =

{

1,0,0,0,0,0,0,0,0,0

},

/*

0

*/

{

0,1,0,0,0,0,0,0,0,0

},

/*

1

*/

{

0,0,1,0,0,0,0,0,0,0

},

/*

2

*/

{

0,0,0,1,0,0,0,0,0,0

},

/*

3

*/

{

0,0,0,0,1,0,0,0,0,0

},

/*

4

*/

{

0,0,0,0,0,1,0,0,0,0

},

/*

5

*/

{

0,0,0,0,0,0,1,0,0,0

},

/*

6

*/

{

0,0,0,0,0,0,0,1,0,0

},

/*

7

*/

{

0,0,0,0,0,0,0,0,1,0

},

/*

8

*/

{

0,0,0,0,0,0,0,0,0,1

},

/*

9

*/

{

0,0,0,0,1,0,0,0,0,0

},

/*

4

*/

{

0,0,0,0,0,1,0,0,0,0

},

/*

5

*/

{

0,0,0,0,0,0,0,1,0,0

},

/*

7

*/

{

0,0,0,1,0,0,0,0,0,0

},

/*

3

*/

{

0,0,1,0,0,0,0,0,0,0

},

/*

2

*/

{

0,0,0,0,1,0,0,0,0,0

},

/*

4

*/

{

1,0,0,0,0,0,0,0,0,0

},

/*

0

*/

{



{ 0,0,0,0,0,0,0,0,0,1 } };/* 9 */

backprop5.c

/*

*=======================================================================*


*------------------------------------------------------------------------

*

* DE TAI : HUAN LUYEN MANG NO-RON VOI BUOC HOC TINH THEO NGUYEN LY VUOT KHE *

* NGON NGU : C

* TRINH DICH : VISUAL C++

* TEN TEP : backprop.c

*------------------------------------------------------------------------

*

*=======================================================================*

*/

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include "taphuanluyen.h"

#include "dinhnghia.h"

/************************** DINH NGHIA CAC BIEN TOAN CUC

****************************/

float MTTSLA[SLNRLV][SLNRLA]; //MA TRAN TRONG SO LOP AN float MTTSLR[SLNRLA][SLNRLR]; //MA TRAN TRONG SO LOP RA

float BTMTTSLA[SLNRLV][SLNRLA];//BIEN THIEN MA TRAN TRONG SO LOP AN float BTMTTSLR[SLNRLA][SLNRLR];//BIEN THIEN MA TRAN TRONG SO LOP RA float x[SLNRLV]; //VEC-TO DAU VAO LOP VAO

float y[SLNRLA]; //VEC TO DAU RA LOP AN float z[SLNRLR]; //VEC TO DAU RA LOP RA float HW1[SLNRLV][SLNRLA]; //KHONG SU DUNG

float HW2[SLNRLV][SLNRLA]; //KHONG SU DUNG float OW1[SLNRLA][SLNRLR]; //KHONG SU DUNG float OW2[SLNRLA][SLNRLR]; //KHONG SU DUNG float SSLA[SLNRLA]; //SAI SO LOP AN float SSLR[SLNRLR]; //SAI SO LOP RA

int PATR[SLMHL];

float ECM[SLMHL];

float TOCDOHOC=2; //TOC DO HOC int SOBUOCLAP=0;

float BVK=0; //BUOC VUOT KHE

int NBS=0; float FX[SLMHL]; float F[SLMHL]; float A,GAMA;

float QUANTINH=0.1; //TOAN HANG QUAN TINH

int MTDVKT[35]; //MA TRAN DAU VAO KIEM TRA

long int itr;

int HTHL; //HOAN THANH HUAN LUYEN

int LCBH; //LUA CHON BUOC HOC

int RESET; //RESET MANG

int RES=1; //RESET MANG

int SDM; //SU DUNG MANG

/*************** KET THUC DINH NGHIA CAC BIEN TOAN CUC ****************/

/*************************** CAC NGUYEN MAU HAM ***********************/ int KHOITAOMANG();

void QUATRINHHUANLUYEN();

void DAPUNGDAURA(int afer[]);

float GIATRIHAMMUCTIEU(int x[],float y[],int SIZE); void DIEUCHINHTRONGSO(int k);

void HAMMUCTIEU(); void BUOCLAP();


/***************************** CAC HAM VA CAC THU TUC ******************/


/*---------------------------------------------------------------------- Ten Ham: KHOITAOMANG

Mo ta: 1. KHOI TAO MA TRAN TRONG SO LOP AN, VOI CACS GIA TRI NGAU NHIEN BI CHAN

2. KHOI TAO MA TRAN TRONG SO LOP RA, VOI CACS GIA

TRI NGAU NHIEN BI CHAN

Cac dau vao: KHONG CO Gia tri tra ve: 1

--------------------------------------------------------------------*/


int KHOITAOMANG()

{

int i,j; int ch; int num;


NBS=0;

HTHL=2;

RESET=0;

RES=1;

MHL=0;

srand(time(0)); for(i=0;i<SLNRLV;i++)

for(j=0;j<SLNRLA;j++)

{

MTTSLA[i][j] = -0.5+ (float) rand()/RAND_MAX; BTMTTSLA[i][j] = 0;

}


for(i=0;i<SLNRLA;i++) for(j=0;j<SLNRLR;j++)

{

MTTSLR[i][j] = -0.5 + (float) rand()/RAND_MAX; BTMTTSLR[i][j] = 0;

}


for(i=0;i<SLMHL;i++)

PATR[i] = 0;


for(i=0;i<35;i++)

MTDVKT[i]=0;

return 1;

}


/*---------------------------------------------------------------------

Ten Thu tuc: TOCDOHOCZERO

Mo ta: 1. CAC DAU RA CUA MANG VAN LA ZERO, VI TOC DO HOC LA ZERO.


MUON.


TOAN BUOC VUOT KHE.

Cac dau vao: KHONG CO Gia tri tra ve: KHONG CO

2. CAC SAI SO HOAN TOAN BANG VOI CAC DAU RA MONG


3. THU TUC NAY DUNG DE PHUC VU CHO VIEC TINH

------------------------------------------------------------------------

*/


void TOCDOHOCZERO(void)

{

int t;

TOCDOHOC=0;


BUOCLAP(); HAMMUCTIEU(); for(t=0;t<SLMHL;t++)

FX[t] = ECM[t];

}


/*--------------------------------------------------------------------- Ten Thu tuc: TINHBUOCHOCVUOTKHE

Ngay sua : 03-11-2007 Phien ban : V2

Mo ta: THU TUC TINH BUOC HOC THEO NGUYEN LY VUOT KHE

Cac dau vao: KHONG CO Gia tri tra ve: KHONG CO

----------------------------------------------------------------------*/


void TINHBUOCHOCVUOTKHE(void)

{

float XL,XU,FL[SLMHL],temp; int i,t,j;

if(NBS==0)

{

A=0.5;

GAMA=0.1;

TOCDOHOC=A;

}

for(t=0;t<SLMHL;t++)

FL[t]=FX[t];

XL=0;


BUOC1:


TOCDOHOC=A; BUOCLAP(); HAMMUCTIEU(); for(t=0;t<SLMHL;t++)

F[t] = ECM[t];

if(F[t]>FL[t])

{

XU=A;

goto BUOC2;

}

//XL=A; for(t=0;t<SLMHL;t++)

FL[t]=F[t];

XL=A;

A=1.5*A;

XU=A;

goto BUOC1;

BUOC2:


if(FD>=(XU-FL))

goto BUOC3; A=XL+GAMA*(XU-XL); temp=TOCDOHOC; TOCDOHOC=A; BUOCLAP(); HAMMUCTIEU(); TOCDOHOC=temp; for(t=0;t<SLMHL;t++)

F[t] = ECM[t]; for(t=0;t<SLMHL;t++)


if(FL[t]>F[t])

{

XU=A; for(j=0;j<SLMHL;j++)

FL[j]=F[j];

goto BUOC2;

}

for(t=0;t<SLMHL;t++)

if(F[t]>FL[t])

{

for(j=0;j<SLMHL;j++) FL[j]=F[j];


BUOC3:

XL=A;

goto BUOC2;

}

for(t=0;t<SLMHL;t++)

FX[t]=F[t];

NBS=NBS+1;

TOCDOHOC=A;

}

/*-----------------------------------------------------------------------

----

Ten Thu tuc: TINHTOANBUOCHOC

Mo ta: TINH TOAN TOC DO HOC SAU MOI BUOC LAP

Cac dau vao: KHONG CO Gia tri tra ve: KHONG CO

-----------------------------------------------------------------------*/ void TINHTOANBUOCHOC()

{

TOCDOHOC=TSDCBH/(SOBUOCLAP*0.001+MSDCBH);

}


/*-----------------------------------------------------------------------

Ten Thu tuc: BUOCLAP

Mo ta: DIEU CHINH CAC TRONG SO CUA MANG

Cac dau vao: KHONG CO Gia tri tra ve: KHONG CO

------------------------------------------------------------------------

*/


void BUOCLAP()

{

int i; i=0;

//LUA CHON MAU HUAN LUYEN NGAU NHIEN: i = (int)(SLMHL*rnd), VOI

0<rnd<1

do

{

i = (int)(SLMHL*(float) rand() / RAND_MAX);

}while(PATR[i]); DAPUNGDAURA(TAPHUANLUYEN[i]); DIEUCHINHTRONGSO(i);

}

/*----------------------------------------------------------------------- Ten Thu tuc: HUANLUYENCODINH

Mo ta: HUAN LUYEN MANG DE TB-BINH PHUONG SAI SO DAU RA NHO HON EPSILON CHO TRUOC

BUOC HOC LA MOT HANG SO.

Cac dau vao: KHONG CO Gia tri tra ve: KHONG CO

..... Xem trang tiếp theo?
⇦ Trang trước - Trang tiếp theo ⇨

Ngày đăng: 04/12/2022