학교 과제에서 perceptron 구현하는게 있었는데 그냥 올려본당
Class0 = {0.05, 0.1, 0.15, 0.2, 0.05, 0.3, 0.35, 0.4, 0.45, 0.49, 0.52, 0.56, 0.57, 0.82}
Class1 = {0.12, 0.47, 0.48, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #include<iostream> #include <vector> using namespace std; float w0 = -0.1; float w1 = -0.36; float learning_rate = 0.1; void perceptron(vector<float> class0, vector<float> class1){ int misclassified_num = 0; float learning_num = 0; cout << "(w0 : " << w0 << ", w1 : " << w1 << ")" << endl; for (int i = 0; i < class0.size(); i++){ if (w0 * class0[i] > 0){ misclassified_num++; learning_num = class0[i]; } } w0 = w0 + (learning_rate*learning_num); for (int i = 0; i < class1.size(); i++){ if (w1 * class1[i] < 0){ misclassified_num++; learning_num = class1[i]; } } w1 = w1 + (learning_rate*learning_num); cout << "misclassified_num : "<< misclassified_num << endl << endl; if (misclassified_num != 0){ perceptron(class0, class1); } else{ cout << w0 << "," << w1 << endl; } } int main(){ vector<float> input_class0; vector<float> input_class1; int data_num = 0; float input_data = 0; cout << "input the number of class0 data : "; cin >> data_num; for (int i = 0; i < data_num; i++){ cout << "input the data ("<<i<<"): "; cin >> input_data; input_class0.push_back(input_data); } cout << "input the number of class1 data : "; cin >> data_num; for (int i = 0; i < data_num; i++){ cout << "input the data (" << i << "): "; cin >> input_data; input_class1.push_back(input_data); } perceptron(input_class0, input_class1); } | cs |
input the number of class0 data : 14
input the data (0): 0.05
input the data (1): 0.1
input the data (2): 0.15
input the data (3): 0.2
input the data (4): 0.25
input the data (5): 0.3
input the data (6): 0.35
input the data (7): 0.4
input the data (8): 0.45
input the data (9): 0.49
input the data (10): 0.52
input the data (11): 0.56
input the data (12): 0.57
input the data (13): 0.82
input the number of class1 data : 13
input the data (0): 0.12
input the data (1): 0.47
input the data (2): 0.48
input the data (3): 0.5
input the data (4): 0.55
input the data (5): 0.6
input the data (6): 0.65
input the data (7): 0.7
input the data (8): 0.75
input the data (9): 0.8
input the data (10): 0.85
input the data (11): 0.9
input the data (12): 0.95
(w0 : -0.1, w1 : -0.36)
misclassified_num : 13
(w0 : -0.1, w1 : -0.265)
misclassified_num : 13
(w0 : -0.1, w1 : -0.17)
misclassified_num : 13
(w0 : -0.1, w1 : -0.075)
misclassified_num : 13
(w0 : -0.1, w1 : 0.02)
misclassified_num : 0
-0.1,0.02
'Machine Learning > Algorithm' 카테고리의 다른 글
DQN Catch game 예제 코드 (1) | 2019.09.12 |
---|---|
Q-learning grid world 예제 코드 (0) | 2019.09.08 |
기계는 사람의 말을 어떻게 이해할까? 워드 임베딩(Word embedding) (1) | 2019.08.09 |
C++로 만드는 multilayer perceptron (MLP) (0) | 2018.11.02 |