You are on page 1of 10

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

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

#define jumlahNodeInput 2

#define jumlahData 4

3
4
5
6

class Perceptron{
public:
Perceptron(){
init();

7
};

8
void setInput(int dataInput[jumlahData][jumlahNodeInput]){

9
int i;

10
int j;

11
12

for (i=0;i<jumlahData;i++){
for(j=0;j<jumlahNodeInput;j++){

13

this->dataInput[i][j] = dataInput[i][j];

14

15

16

17
18
19
20
21

void setTarget(int dataTarget[jumlahData]){


int i;
for (i=0;i<jumlahData;i++){
this->dataTarget[i]=dataTarget[i];
}

22
23
}

24
25
void viewData(){

26
int i;

27
int j;

28
29

for (i=0;i<jumlahData;i++){
for(j=0;j<jumlahNodeInput;j++){

30

std::cout<< "dataInput["<<i<<"]["<< j<< "] = " <<dataInput[i][j] <<std::e

31

32

std::cout<< "dataTarget["<<i<<"] = " <<dataTarget[i] <<std::endl;

33

}
}

34

void showBobot(){

35

int i;

36
37
38

for(i=0;i<jumlahNodeInput;i++){
std::cout<< "bobot input ["<<i<<"] ="<<bobotInput[i]<<std::endl;
}
std::cout<<"bobot bias : "<<bobotBias<<std::endl;

39
}

40
41
void train(){

42
int i;

43
int j;

44
45

int net;
int epochCounter=1;

46

int perubahanBobotPerEpoch = 0;

47

//feed forward

48
49

do{

50

perubahanBobotPerEpoch = 0;

51

std::cout<< "epoch :"<< epochCounter << std::endl;


epochCounter++;

52

for(i=0;i<jumlahData;i++){

53

net = 0;

54
55
56

for(j=0;j<jumlahNodeInput;j++){
std::cout<< "| input["<< j << "] = " << bobotInput[j];
net += dataInput[i][j]*bobotInput[j];
}

57
net += bobotBias;

58
std::cout<< "| net :"<< net;

59
//learning

60
std::cout<< "| output jaringan :"<< fungsiAktifasi(net);

61
62

std::cout<< "| target :"<< dataTarget[i]<<std::endl;


if (fungsiAktifasi(net)!=dataTarget[i]){

63

//ubah bobot

64

perubahanBobotPerEpoch++;

65

int k;

66

for(k=0;k<jumlahNodeInput;k++){

67

bobotInput[k] += learningRate*dataTarget[i]*dataInput[i][k];

68
69

}
bobotBias += learningRate*dataTarget[i];
}

70

71

}while(perubahanBobotPerEpoch != 0);

72

73
74

private:

75
76

void init(){
learningRate = 1;

77
threshold = 0;

78
int i;

79
for(i=0;i<jumlahNodeInput;i++){

80
bobotInput[i] = 0;

81
82
83

}
bobotBias = 0;
}

84
85

int fungsiAktifasi(float masukanFungsi){

86

if(masukanFungsi > threshold){

87

return 1;
}

88

else if(masukanFungsi < (-threshold)){

89

return -1;

90
91
92

}
else{
return 0;
}

93
}

94
95
int dataInput[jumlahData][jumlahNodeInput];

96
int dataTarget[jumlahData];

97
float threshold;

98
99
10
0
10
1
10
2
10
3
10

float learningRate;
int bobotInput[jumlahNodeInput];
int bobotBias;
};

4
10
5
10
6
10
7
10
8
10
9
110
111

File Utama(Main.cpp)
1

#include <iostream>

#include "Perceptron.cpp"

3
4
5
6

int main(){
Perceptron perceptron;
int arrayInput[4][2];
int arrayTarget[4];

8
9

//data pertama

1
0

arrayInput[0][0]= 1;
arrayInput[0][1]= 1;

11
arrayTarget[0] = 1;

1
2
1
3

//data kedua
arrayInput[1][0]= 1;
arrayInput[1][1]= -1;

1
4
1
5

arrayTarget[1] = -1;
//data ketiga
arrayInput[2][0]= -1;

1
6
1
7

arrayInput[2][1]= 1;
arrayTarget[2] = -1;
//data keempat

1
8
1
9

arrayInput[3][0]= -1;
arrayInput[3][1]= -1;
arrayTarget[3] = -1;

2
0
2
1

perceptron.setInput(arrayInput);
perceptron.setTarget(arrayTarget);

2
2
3
2
4
2
5
2
6

//perceptron.viewData();
perceptron.train();

2
7
2
8

perceptron.showBobot();
return 0;
}

2
9
3
0
3
1
3
2

You might also like