-
Notifications
You must be signed in to change notification settings - Fork 0
/
fc_layer_sm.cpp
executable file
·107 lines (104 loc) · 3.08 KB
/
fc_layer_sm.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
#include"layer.h"
#include"fc_layer_sm.h"
#include"vectors_utils.h"
fc_layer_sm::fc_layer_sm(const std::vector<std::vector<double>> &weights_, const std::vector<double> &biases_): layer(weights_,biases_)
{
type = "FC_LAYER_SM";
input_dim = weights.cols();
output_dim = weights.rows();
}
//------------------------------------------------------------------------------
fc_layer_sm::fc_layer_sm(const Eigen::MatrixXd &weights_, const Eigen::VectorXd &biases_): layer(weights_,biases_)
{
type = "FC_LAYER_SM";
input_dim = weights.cols();
output_dim = weights.rows();
}
//------------------------------------------------------------------------------
fc_layer_sm::fc_layer_sm(const Eigen::MatrixXd &weights_,const std::vector<double> &biases_): layer(weights_,biases_)
{
type = "FC_LAYER_SM";
input_dim = weights.cols();
output_dim = weights.rows();
}
//------------------------------------------------------------------------------
Eigen::VectorXd fc_layer_sm::predict(const Eigen::VectorXd & input)
{
Eigen::VectorXd temp;
temp = std::move(weights*input);
temp += biases;
double max = find_max(temp);
int len = weights.rows();
Eigen::VectorXd rescaled_inp(len);
for (int i = 0; i < len; i++)
{
rescaled_inp(i) = temp(i)-max;
}
temp = softmax(rescaled_inp);
return temp;
}
//------------------------------------------------------------------------------
Eigen::MatrixXd fc_layer_sm::compute_partial_derivatives_wrt_inputs(Eigen::VectorXd &input)
{
int nrows = weights.rows();
int ncols = weights.cols();
Eigen::MatrixXd res(nrows,ncols);
Eigen::MatrixXd jac_act(nrows,ncols);
Eigen::VectorXd temp(nrows);
temp = std::move(weights*input);
temp += biases;
double max = find_max(temp);
int len = weights.rows();
Eigen::VectorXd rescaled_inp(len);
for (int i = 0; i < len; i++)
{
rescaled_inp(i) = temp(i)-max;
}
jac_act = std::move(softmax_jacobian(rescaled_inp));
//#pragma omp parallel for
for (int i = 0; i < nrows; i++)
{
#pragma omp simd
for (int j = 0; j < nrows; j++)
{
res(i,j) = jac_act(i,j)*weights(i,j);
}
}
return res;
}
//------------------------------------------------------------------------------
Eigen::MatrixXd fc_layer_sm::compute_partial_derivatives_wrt_weights_biases(Eigen::VectorXd &input)
{
const int nrows = weights.rows();
const int ncols = weights.cols()+1;
Eigen::VectorXd temp;
temp = std::move(weights*input);
temp += std::move(temp+biases);
Eigen::MatrixXd jac_act(nrows,ncols);
double max = find_max(temp);
int len = weights.rows();
Eigen::VectorXd rescaled_inp(len);
for (int i = 0; i < len; i++)
{
rescaled_inp(i) = temp(i)-max;
}
jac_act = std::move(softmax_jacobian(rescaled_inp));
const int w_b_per_row = weights.cols()+1;
Eigen::MatrixXd res(nrows,ncols);
//#pragma omp parallel for
for (int i = 0; i < nrows; i++)
{
#pragma omp simd
for(int j = 0; j < w_b_per_row-1; j++)
{
res(i,j) = input(j)*jac_act(i,j);
}
res(i,w_b_per_row-1) = 0.;
for (int j = 0; j < w_b_per_row-1; j++)
{
res(i,w_b_per_row-1) += jac_act(i,j);
}
}
return res;
}
//------------------------------------------------------------------------------