Anda di halaman 1dari 5

Jaringan Komputer

Tugas

DEWY YULIANA 09121002056

JURUSAN TEKNIK INFORMATIKA


FAKULTAS ILMU KOMPUTER
UNIVERSITAS SRIWIJAYA
Oktober 2014

#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<fstream>
using namespace std;
class BackPropagation{
private:
double V[55][55], W[55][55], ErrH[55], ErrO[55],Zinj[55], Yink[55];
int i,j,k,o;
public:
int X[55], T[55], nN_i, nN_h, nN_o, EpochMaks=1000, Epoch=0, BobotEpoch;
double LR, ErrToleransi,Y[55], Z[55],temp=0,temp2=0, temp3=0,temp4=0, average,
SSE;
BackPropagation(){}
double sigmoid(double x){
return (1/(1+exp(-x)));
}
double Turunan_sigmoid(double x){
return x*(1-x);
}
void inisialisasi(int nNode_I, int nNode_H, int nNode_O, double LearningRate,
double ErrMax){
nN_i=nNode_I;
nN_h= nNode_H;
nN_o=nNode_O;
LR=LearningRate;
ErrToleransi=ErrMax;
}
void file_double2(string filename,double init[55][55] ,int row, int col){
ifstream fileIN;
fileIN.open(filename.c_str());
if(fileIN.fail()){ cerr << "File gagal";
exit(1); }
for(int i =0; i<row; i++){
for(int j=0; j<col; j++){
fileIN>>init[i][j];
}
}
}
void file_int(string filename,int init[55] ,int row){
ifstream fileIN;
fileIN.open(filename.c_str());
if(fileIN.fail()){ cerr << "File gagal";
exit(1); }
for(i =0; i<row; i++){ fileIN>>init[i]; }

}
void forward(){
for(k=0;k<nN_o;k++){
for(j=0;j<nN_h;j++){
for(i=0;i<nN_i;i++){
Zinj[j] = Zinj[j] + X[i]*V[i][j];
}
Z[j] = sigmoid(Zinj[j]);
Yink[k] = Yink[k] + Z[j]*W[j][k];
}
Y[k] = sigmoid(Yink[k]);
}
cout<<" Z "<<endl;
displaycalculate(Z, nN_h);
cout<<" Y "<<endl;
displaycalculate(Y, nN_o);
}
void back(){
temp=0;
temp2=0;
for(k=0;k<nN_o;k++){
temp += Y[k];
temp2 += ((T[k]-Y[k])*(T[k]-Y[k]));
ErrO[k] = (T[k]-Y[k])*Turunan_sigmoid(Y[k]);
}
average = temp/nN_o;
SSE = temp2/2;
for(j=0;j<nN_h;j++){
for(k=0;k<nN_o;k++){
W[j][k] += LR*ErrO[k]*Z[j];
}
}
temp3=0;
for(i=0;i<nN_i;i++){
for(j=0;j<nN_h;j++){
for(k=0;k<nN_o;k++){
temp3+= (ErrO[k]*W[j][k]);
}
ErrH[j] = (Turunan_sigmoid(Z[j])*temp3);
temp3=0;
V[i][j] += LR*ErrH[j]*X[i];
}
}
cout<<" Error Output "<<endl;
displaycalculate(ErrO, nN_o);
cout<<"rata-rata Y : "<<average<<endl;

cout<<"SSE = "<<SSE<<endl;
}
void displaybobot(double bobot[55][55], int row, int col){
for(i=0;i<row;i++){
for(j=0;j<col;j++){
cout<<bobot[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
void displaycalculate(double calculate[55], int row){
for(i=0;i<row;i++){
cout<<calculate[i]<<" "<<endl;
}
cout<<endl;
}
void process(){
file_double2("W.dat", W, nN_h, nN_o);
file_double2("V.dat", V, nN_i, nN_h);
file_int("T.dat", T, nN_o);
file_int("X.dat", X, nN_i);
do{
Epoch++;
cout<<endl<<endl<<"Epoch ke "<<Epoch<<endl<<endl;
cout<<" BOBOT V "<<endl;
displaybobot(V, nN_i, nN_h);
cout<<" BOBOT W "<<endl;
displaybobot(W,nN_h, nN_o);
forward();
back();
cout<<endl<<"PERUBAHAN BOBOT"<<endl;
}while(SSE < ErrToleransi || Epoch <= EpochMaks);
}
void testing(){
cout<< "Fase Testing"<<endl;
file_int("T.dat", T, nN_o);
file_int("X.dat", X, nN_i);
cout<<" BOBOT V "<<endl;
displaybobot(V, nN_i, nN_h);
cout<<" BOBOT W "<<endl;

displaybobot(W,nN_h, nN_o);
forward();
back();
}
};

int main(){
double LearningRate, ErrToleransi,ErrH[55], ErrO[55],Y[55],Z[55];
int nNodeOutput, nNodeInput, nNodeHidden;
cout<< "Jumlah Node Input = "; cin>> nNodeInput;
cout<< "Jumlah Node Hidden = ";
cin>> nNodeHidden;
cout<< "Jumlah Node Output = "; cin>> nNodeOutput;
cout<< "Nilai Error Toleransi = ";
cin>> ErrToleransi;
cout<< "Nilai Learning Rate = "; cin>> LearningRate;
BackPropagation BP1;
BP1.inisialisasi(nNodeInput, nNodeHidden, nNodeOutput, LearningRate, ErrToleransi);
BP1.process();
BP1.testing();
}

Anda mungkin juga menyukai