-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathoptimize.cc
More file actions
53 lines (43 loc) · 1.77 KB
/
optimize.cc
File metadata and controls
53 lines (43 loc) · 1.77 KB
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
#include "../include/ceras.hpp"
int main()
{
using namespace ceras;
random_generator.seed( 42 ); // fix random seed to reproduce the same result
// define model, a single layer NN, using softmax activation
auto x = place_holder<tensor<double>>{};
auto W = variable{ tensor<double>{ {2, 2}, {1.0, -1.0, 1.0, -1.0} } };
auto b = variable{ tensor<double>{{1,2}, {0.0, 0.0} } };
auto p = softmax( x * W + b ); // p is our model
// preparing input for the model
unsigned long const N = 512;
auto blues = randn<double>( {N, 2} ) - 2.0 * ones<double>( {N, 2} );
auto reds = randn<double>( {N, 2} ) + 2.0 * ones<double>( {N, 2} );
auto _x = concatenate( blues, reds, 0 );
// binding input to layer x
session<tensor<double>> s;
s.bind( x, _x );
// define loss here
auto c = place_holder<tensor<double>>{};
auto J = cross_entropy( c, p );
// generating output/ground_truth for the model
auto c_blue = tensor<double>{{1, 2}, {1.0, 0.0} };
auto c_blues = repmat( c_blue, N, 1 );
auto c_red = tensor<double>{{1, 2}, {0.0, 1.0} };
auto c_reds = repmat( c_red, N, 1 );
auto _c = concatenate( c_blues, c_reds, 0 );
// binding output to the model
s.bind( c, _c );
// define optimizer here
double const learning_rate = 1.0e-3;
auto optimizer = gradient_descent{ J, 1, learning_rate }; // J is the loss, 1 is the batch size, learning_rate is the hyper-parameter
auto const iterations = 32UL;
for ( auto idx = 0UL; idx != iterations; ++idx )
{
// first do forward propagation
auto J_result = s.run( J );
std::cout << "J at iteration " << idx+1 << ": " << J_result[0] << std::endl;
// then do backward propagation
s.run( optimizer );
}
return 0;
}