나도 이걸 왜 C++로 만들었는지 모르겄다 ^^;;
node.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class node{ public: float w; float input; float output; node* tail; void link_node(node *pre, node *post){ pre->tail = post; } void cal_output(node *pre){ pre->output = (pre->input * pre->w); } }; | cs |
main.cpp
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | #include <iostream> #include <math.h> #include <list> #include "node.h" using namespace std; float learning_rate = 0.1; int class0_num = 4; int class1_num = 5; int class0_x[4] = { -1, -1, 1, 1 }; int class0_y[4] = { 1, -1, 1, -1 }; int class1_x[5] = { -1, 0, 0, 1, 0 }; int class1_y[5] = { 0, -1, 1, 0, 0 }; float error_rate = 0; float sigmoid(float x){ float y = 0; y = 1 / (1 + exp(-x)); return y; } float sigmoid_function_output(float x){ float y = 0; y = sigmoid(x)*(1 - sigmoid(x)); return y; } float get_error_rate(float output, float desired_value){ return abs(desired_value - output); } void update_hidden_weight(node *hidden_node, node *output_node, float error_rate, float learning_rate){ hidden_node->w = learning_rate * error_rate * sigmoid_function_output(output_node->input)*hidden_node->output; } void update_input_weight(node *input_node, node *hidden_node, node *output_node, float error_rate, float learning_rate){ input_node->w = learning_rate * (error_rate * sigmoid_function_output(output_node->input) * hidden_node->w) * sigmoid_function_output(hidden_node->input) * input_node->input; } void MLP_one(int class0_x[4], int class0_y[4], int class1_x[5], int class1_y[5], node *input_bias, node *input_x, node *input_y, node *hidden_node1, node *output_node){ //learning for (int i = 0; i < class0_num; i++){ input_x->input = class0_x[i]; input_y->input = class0_y[i]; input_bias->input = 1; input_x->cal_output(input_x); input_y->cal_output(input_y); input_bias->cal_output(input_bias); hidden_node1->input += (input_x->output + input_y->output + input_bias->output); } for (int i = 0; i < class1_num; i++){ input_x->input = class1_x[i]; input_y->input = class1_y[i]; input_bias->input = 1; input_x->cal_output(input_x); input_y->cal_output(input_y); input_bias->cal_output(input_bias); hidden_node1->input += (input_x->output + input_y->output + input_bias->output); } hidden_node1->cal_output(hidden_node1); output_node->input = hidden_node1->output; output_node->output = output_node->input; error_rate = get_error_rate(0.1, output_node->output); cout << error_rate << endl; cout << "x_w : " << input_x->w << " / y_w : " << input_y->w << " / bias_w : " << input_bias->w << endl; if (error_rate > 1.7){ update_hidden_weight(hidden_node1, output_node, error_rate, learning_rate); update_input_weight(input_bias, hidden_node1, output_node, error_rate, learning_rate); update_input_weight(input_x, hidden_node1, output_node, error_rate, learning_rate); update_input_weight(input_y, hidden_node1, output_node, error_rate, learning_rate); error_rate = get_error_rate(output_node->output, 1.7); MLP_one(class0_x, class0_y, class1_x, class1_y, input_bias, input_x, input_y, hidden_node1, output_node); } } void MLP_four(int class0_x[4], int class0_y[4], int class1_x[5], int class1_y[5], node *input_bias, node *input_x, node *input_y, node *hidden_node1, node *hidden_node2, node *hidden_node3, node *hidden_node4, node *output_node){ //learning for (int i = 0; i < class0_num; i++){ input_x->input = class0_x[i]; input_y->input = class0_y[i]; input_bias->input = 1; input_x->cal_output(input_x); input_y->cal_output(input_y); input_bias->cal_output(input_bias); hidden_node1->input += (input_x->output + input_y->output + input_bias->output); hidden_node2->input += (input_x->output + input_y->output + input_bias->output); hidden_node3->input += (input_x->output + input_y->output + input_bias->output); hidden_node4->input += (input_x->output + input_y->output + input_bias->output); } for (int i = 0; i < class1_num; i++){ input_x->input = class1_x[i]; input_y->input = class1_y[i]; input_bias->input = 1; input_x->cal_output(input_x); input_y->cal_output(input_y); input_bias->cal_output(input_bias); hidden_node1->input += (input_x->output + input_y->output + input_bias->output); hidden_node2->input += (input_x->output + input_y->output + input_bias->output); hidden_node3->input += (input_x->output + input_y->output + input_bias->output); hidden_node4->input += (input_x->output + input_y->output + input_bias->output); } hidden_node1->cal_output(hidden_node1); hidden_node2->cal_output(hidden_node2); hidden_node3->cal_output(hidden_node3); hidden_node4->cal_output(hidden_node4); output_node->input += hidden_node1->output + hidden_node2->output + hidden_node3->output + hidden_node4->output; output_node->output = output_node->input; error_rate = get_error_rate(0.1, output_node->output); cout << error_rate << endl; cout << "x_w : " << input_x->w << " / y_w : " << input_y->w << " / bias_w : " << input_bias->w << endl; if (error_rate > 9){ update_hidden_weight(hidden_node1, output_node, error_rate, learning_rate); update_hidden_weight(hidden_node2, output_node, error_rate, learning_rate); update_hidden_weight(hidden_node3, output_node, error_rate, learning_rate); update_hidden_weight(hidden_node4, output_node, error_rate, learning_rate); update_input_weight(input_bias, hidden_node1, output_node, error_rate, learning_rate); update_input_weight(input_x, hidden_node1, output_node, error_rate, learning_rate); update_input_weight(input_y, hidden_node1, output_node, error_rate, learning_rate); error_rate = get_error_rate(output_node->output, 1.7); MLP_four(class0_x, class0_y, class1_x, class1_y, input_bias, input_x, input_y, hidden_node1, hidden_node2, hidden_node3, hidden_node4, output_node); } } void MLP_five(int class0_x[4], int class0_y[4], int class1_x[5], int class1_y[5], node *input_bias, node *input_x, node *input_y, node *hidden_node1, node *hidden_node2, node *hidden_node3, node *hidden_node4, node *hidden_node5, node *output_node){ for (int i = 0; i < class0_num; i++){ input_x->input = class0_x[i]; input_y->input = class0_y[i]; input_bias->input = 1; input_x->cal_output(input_x); input_y->cal_output(input_y); input_bias->cal_output(input_bias); hidden_node1->input += (input_x->output + input_y->output + input_bias->output); hidden_node2->input += (input_x->output + input_y->output + input_bias->output); hidden_node3->input += (input_x->output + input_y->output + input_bias->output); hidden_node4->input += (input_x->output + input_y->output + input_bias->output); hidden_node5->input += (input_x->output + input_y->output + input_bias->output); } for (int i = 0; i < class1_num; i++){ input_x->input = class1_x[i]; input_y->input = class1_y[i]; input_bias->input = 1; input_x->cal_output(input_x); input_y->cal_output(input_y); input_bias->cal_output(input_bias); hidden_node1->input += (input_x->output + input_y->output + input_bias->output); hidden_node2->input += (input_x->output + input_y->output + input_bias->output); hidden_node3->input += (input_x->output + input_y->output + input_bias->output); hidden_node4->input += (input_x->output + input_y->output + input_bias->output); hidden_node5->input += (input_x->output + input_y->output + input_bias->output); } hidden_node1->cal_output(hidden_node1); hidden_node2->cal_output(hidden_node2); hidden_node3->cal_output(hidden_node3); hidden_node4->cal_output(hidden_node4); hidden_node5->cal_output(hidden_node5); output_node->input += hidden_node1->output + hidden_node2->output + hidden_node3->output + hidden_node4->output + hidden_node5->output; output_node->output = output_node->input; error_rate = get_error_rate(0.1, output_node->output); cout << error_rate << endl; cout << "x_w : " << input_x->w << " / y_w : " << input_y->w << " / bias_w : " << input_bias->w << endl; if (error_rate > 12){ update_hidden_weight(hidden_node1, output_node, error_rate, learning_rate); update_hidden_weight(hidden_node2, output_node, error_rate, learning_rate); update_hidden_weight(hidden_node3, output_node, error_rate, learning_rate); update_hidden_weight(hidden_node4, output_node, error_rate, learning_rate); update_input_weight(input_bias, hidden_node1, output_node, error_rate, learning_rate); update_input_weight(input_x, hidden_node1, output_node, error_rate, learning_rate); update_input_weight(input_y, hidden_node1, output_node, error_rate, learning_rate); error_rate = get_error_rate(output_node->output, 1.7); MLP_five(class0_x, class0_y, class1_x, class1_y, input_bias, input_x, input_y, hidden_node1, hidden_node2, hidden_node3, hidden_node4, hidden_node5, output_node); } } int main(){ node *input_bias = new node(); node *input_x = new node(); node *input_y = new node(); node *hidden_node1 = new node(); node *output_node = new node(); float error_rate = 0; //initial input_bias->w = -1; input_x->w = 0.3; input_y->w = -1; hidden_node1->w = 0.5; // MLP_one(class0_x, class0_y, class1_x, class1_y, input_bias, input_x, input_y, hidden_node1, output_node); //initial input_bias->w = -1; input_x->w = 0.7; input_y->w = 0.5; hidden_node1->w = 0.5; node *hidden_node2 = new node(); node *hidden_node3 = new node(); node *hidden_node4 = new node(); hidden_node2->w = 0.3; hidden_node3->w = 0.1; hidden_node4->w = 0.05; // MLP_four(class0_x, class0_y, class1_x, class1_y, input_bias, input_x, input_y, hidden_node1, hidden_node2, hidden_node3, hidden_node4, output_node); node *hidden_node5 = new node(); hidden_node5->w = 0.3; MLP_five(class0_x, class0_y, class1_x, class1_y, input_bias, input_x, input_y, hidden_node1, hidden_node2, hidden_node3, hidden_node4, hidden_node5, output_node); return 0; } | cs |
Result
1) one sigmoid nodes
4.6
x_w : 0.3 / y_w : -1 / bias_w : -1
2) four sigmoid nodes
0.102438
x_w : -0 / y_w : -0 / bias_w : -1.38717e-008
계속하려면 아무 키나 누르십시오 . . .
3) five sigmoid nodes
8.65
x_w : 0.7 / y_w : 0.5 / bias_w : -1
계속하려면 아무 키나 누르십시오 . . .
11.35
x_w : 0.7 / y_w : 0.5 / bias_w : -1
계속하려면 아무 키나 누르십시오 . . .
'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++로 만드는 perceptron (0) | 2018.11.02 |