-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_layer.hh
66 lines (51 loc) · 1.73 KB
/
linear_layer.hh
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
#pragma once
#include "nn_layer.hh"
class LinearLayer : public NNLayer
{
private:
const float weights_init_thresh = 0.01;
const float dropout_rate=0.0;
int mb_size=1;
Shape W_shape;
host_vec W;
device_vec W_device;
host_vec b;
device_vec b_device;
Shape b_shape;
host_vec Z;
device_vec Z_device;
Shape Z_shape;
host_vec A;
device_vec A_device;
Shape A_shape;
host_vec dA;
device_vec dA_device;
Shape dA_shape;
host_vec dropped;
device_vec dropped_device;
Shape dropped_shape; // (1, n_neurons)
host_vec m_weight_deriv;
device_vec m_weight_deriv_device;
Shape m_weight_deriv_shape;
host_vec m_bias_deriv;
device_vec m_bias_deriv_device;
Shape m_bias_deriv_shape;
void initializeBiasWithZeros();
void initializeWeightsRandomly();
void computeAndStoreBackpropError(host_vec& dZ);
void computeAndStoreLayerOutput(host_vec& A);
void updateWeights(float learning_rate);
void accumulateWeightDeriv(host_vec& dZ, float learning_rate);
void updateBias(float learning_rate);
void accumulateBiasDeriv(host_vec& dZ, float learning_rate);
public:
LinearLayer(std::string name, Shape W_shape, float dropout=0);
~LinearLayer();
host_vec& forward(host_vec& A, Shape& A_shape);
host_vec& backprop(host_vec& dZ, float learning_rate = 0.01, int mb_size=1);
void update_weights_bias(float learning_rate);
int getXDim() const;
int getYDim() const;
host_vec getWeightsMatrix() const;
host_vec getBiasVector() const;
};