-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.cpp
100 lines (98 loc) · 3.94 KB
/
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
/*
███████╗██████╗ ██████╗ ███████╗ ███████╗███╗ ██╗ ██████╗ ██╗███╗ ██╗███████╗
██╔════╝██╔══██╗██╔════╝ ██╔════╝ ██╔════╝████╗ ██║██╔════╝ ██║████╗ ██║██╔════╝
█████╗ ██║ ██║██║ ███╗█████╗ █████╗ ██╔██╗ ██║██║ ███╗██║██╔██╗ ██║█████╗
██╔══╝ ██║ ██║██║ ██║██╔══╝ ██╔══╝ ██║╚██╗██║██║ ██║██║██║╚██╗██║██╔══╝
███████╗██████╔╝╚██████╔╝███████╗ ███████╗██║ ╚████║╚██████╔╝██║██║ ╚████║███████╗
╚══════╝╚═════╝ ╚═════╝ ╚══════╝ ╚══════╝╚═╝ ╚═══╝ ╚═════╝ ╚═╝╚═╝ ╚═══╝╚══════╝
Author:Edge
Web:likedge.top
Date:20200925
*/
#include<iostream>
#ifdef __APPLE__
#include <sys/malloc.h>
#else
#include <malloc.h>
#endif
#include<stdio.h>
#include<stdlib.h>
#include<ctime>
#include<string>
#include <time.h>
#include <math.h>
#include <fstream>
#include "./autodiff/node.h"
#include"./matrix/matrix_def.h"
#include"./matrix/matrix_pro.h"
#include"./welcome/score_wel.cpp"
#include"./logistic/logistic_def.h"
#include"./grad_edge/matrix_grad.h"
using namespace std;
clock_t start, stop;
double duration;
Node z=1;
Node t1 = 1,a13 = 1;
/*自定义的损失函数*/
Node loss_act(Node t1,Node a13)
{
Node loss = 0.5*(pow((t1-a13),2));
return loss;
}
Node sigmoid_act(Node z)
{
Node sigmoid_act = 1/(1+(1/exp(z)));
return sigmoid_act;
}
//loss
Node (*loss)(Node,Node) = loss_act;
Node (*act)(Node) = sigmoid_act;
int main()
{
Matrix data_1 = ones(1,3);
Matrix data_2 = ones(3,1);
cout_mat(mul(data_1,data_2));
welcome();
//begin
cout<<"auto build on gcc"<<endl;
cout<<"---------autodiff for neraul network-----------"<<endl;
Matrix data_mine = ones(3,3);
cout<<"data mine"<<endl;
cout_mat(data_mine);
cout<<"data mine"<<endl;
Matrix label = ones(3,1);
Matrix weight1 = ones(3,3);
Matrix bais1 = zeros(3,1);
Matrix weight2 = ones(3,3);
Matrix bais2 = zeros(3,1);
for(int epoch = 0;epoch<1;epoch++)
{
cout<<"---------epoch: "<<epoch<<"------------"<<endl;
cout_mat(weight1);
int input_dim = 3;
int output_dim = 3;
edge_network sequaltial(input_dim,output_dim);
Matrix output1 = sequaltial.forward(data_mine,weight1,bais1);
Matrix output1_without_act = sequaltial.forward_without_act(data_mine,weight1,bais1);
cout_mat(output1_without_act);
cout<<"output2:"<<endl;
Matrix output2 = sequaltial.forward(output1,weight2,bais2);
cout_mat(output2);
Matrix output2_without_act = sequaltial.forward_without_act(output1,weight2,bais2);
cout_mat(output2_without_act);
Matrix output_end = sequaltial.end_layer_backward(label,output2_without_act,*loss,*act);
Matrix backward3 = sequaltial.backward(output_end,output1_without_act,weight2,*act);
Matrix weight_2_grad = mul(output_end,get_T(output1));
cout<<"weight_2_grad:"<<endl;
cout_mat(weight_2_grad);
Matrix weight_1_grad = mul(backward3,get_T(data_mine));
cout<<"weight_1_grad:"<<endl;
cout_mat(weight_1_grad);
weight1 = subtract(weight1,times_mat(0.001,weight_1_grad));
bais1 = subtract(bais1,times_mat(0.001,backward3));
weight2 = subtract(weight2,times_mat(0.001,weight_2_grad));
bais2 = subtract(bais2,times_mat(0.001,output_end));
cout<<"neraul end;"<<endl;
}
return 0;
}