percept ron
DESCRIPTION
perceptromTRANSCRIPT
Diketahui:
Representasi masukan/keluaran dalam bipolar (-1 untuk
false, 1 untuk true)
Learning rate = 1 (penyederhanaan)
Threshold = 0
Tabel Kebenaran Logika And
Input 1 Input 2 Bias Target
1 1 1 1
1 -1 1 -1
-1 1 1 -1
-1 -1 1 -1
Fungsi Aktifasi:
1 jika net > threshold
0 jika net = threshold
-1 jika net < threshold
Arsitektur Jaringan Perceptron:
Arsitektur Perceptron
Source Code Program dibuat menggunakan C++ dan
dicompile menggunakan MinGW.
File Perceptron.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#define jumlahNodeInput 2
#define jumlahData 4
class Perceptron{
public:
Perceptron(){
init();
};
void setInput(int dataInput[jumlahData][jumlahNodeInput]){
int i;
int j;
for (i=0;i<jumlahData;i++){
for(j=0;j<jumlahNodeInput;j++){
this->dataInput[i][j] = dataInput[i][j];
}
}
}
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void setTarget(int dataTarget[jumlahData]){
int i;
for (i=0;i<jumlahData;i++){
this->dataTarget[i]=dataTarget[i];
}
}
void viewData(){
int i;
int j;
for (i=0;i<jumlahData;i++){
for(j=0;j<jumlahNodeInput;j++){
std::cout<< "dataInput["<<i<<"]["<< j<< "] = " <<dataInput[i][j] <<std::endl;
}
std::cout<< "dataTarget["<<i<<"] = " <<dataTarget[i] <<std::endl;
}
}
void showBobot(){
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
int i;
for(i=0;i<jumlahNodeInput;i++){
std::cout<< "bobot input ["<<i<<"] ="<<bobotInput[i]<<std::endl;
}
std::cout<<"bobot bias : "<<bobotBias<<std::endl;
}
void train(){
int i;
int j;
int net;
int epochCounter=1;
int perubahanBobotPerEpoch = 0;
//feed forward
do{
perubahanBobotPerEpoch = 0;
std::cout<< "epoch :"<< epochCounter << std::endl;
epochCounter++;
for(i=0;i<jumlahData;i++){
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
net = 0;
for(j=0;j<jumlahNodeInput;j++){
std::cout<< "| input["<< j << "] = " << bobotInput[j];
net += dataInput[i][j]*bobotInput[j];
}
net += bobotBias;
std::cout<< "| net :"<< net;
//learning
std::cout<< "| output jaringan :"<< fungsiAktifasi(net);
std::cout<< "| target :"<< dataTarget[i]<<std::endl;
if (fungsiAktifasi(net)!=dataTarget[i]){
//ubah bobot
perubahanBobotPerEpoch++;
int k;
for(k=0;k<jumlahNodeInput;k++){
bobotInput[k] += learningRate*dataTarget[i]*dataInput[i][k];
}
bobotBias += learningRate*dataTarget[i];
}
}
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
}while(perubahanBobotPerEpoch != 0);
}
private:
void init(){
learningRate = 1;
threshold = 0;
int i;
for(i=0;i<jumlahNodeInput;i++){
bobotInput[i] = 0;
}
bobotBias = 0;
}
int fungsiAktifasi(float masukanFungsi){
if(masukanFungsi > threshold){
return 1;
}
else if(masukanFungsi < (-threshold)){
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
10
return -1;
}
else{
return 0;
}
}
int dataInput[jumlahData][jumlahNodeInput];
int dataTarget[jumlahData];
float threshold;
float learningRate;
int bobotInput[jumlahNodeInput];
int bobotBias;
};
5
106
107
108
109
110
111
File Utama(Main.cpp)
1
2
3
4
5
6
7
8
#include <iostream>
#include "Perceptron.cpp"
int main(){
Perceptron perceptron;
int arrayInput[4][2];
int arrayTarget[4];
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//data pertama
arrayInput[0][0]= 1;
arrayInput[0][1]= 1;
arrayTarget[0] = 1;
//data kedua
arrayInput[1][0]= 1;
arrayInput[1][1]= -1;
arrayTarget[1] = -1;
//data ketiga
arrayInput[2][0]= -1;
arrayInput[2][1]= 1;
arrayTarget[2] = -1;
//data keempat
arrayInput[3][0]= -1;
arrayInput[3][1]= -1;
arrayTarget[3] = -1;
perceptron.setInput(arrayInput);
perceptron.setTarget(arrayTarget);
//perceptron.viewData();
27
28
29
30
31
32
perceptron.train();
perceptron.showBobot();
return 0;
}