44template <
typename Architecture_t,
typename Layer_t = VGeneralLayer<Architecture_t>,
45 typename DeepNet_t = TDeepNet<Architecture_t, Layer_t>>
48 using Matrix_t =
typename Architecture_t::Matrix_t;
49 using Scalar_t =
typename Architecture_t::Scalar_t;
53 std::vector<std::vector<Matrix_t>>
55 std::vector<std::vector<Matrix_t>>
59 void UpdateWeights(
size_t layerIndex, std::vector<Matrix_t> &weights,
const std::vector<Matrix_t> &weightGradients);
62 void UpdateBiases(
size_t layerIndex, std::vector<Matrix_t> &biases,
const std::vector<Matrix_t> &biasGradients);
85template <
typename Architecture_t,
typename Layer_t,
typename DeepNet_t>
87 :
VOptimizer<Architecture_t, Layer_t, DeepNet_t>(learningRate, deepNet), fMomentum(momentum)
89 std::vector<Layer_t *> &layers = deepNet.
GetLayers();
90 size_t layersNSlices = layers.size();
94 for (
size_t i = 0; i < layersNSlices; i++) {
98 for (
size_t j = 0; j < weightsNSlices; j++) {
104 for (
size_t j = 0; j < biasesNSlices; j++) {
113template <
typename Architecture_t,
typename Layer_t,
typename DeepNet_t>
115 const std::vector<Matrix_t> &weightGradients) ->
void
120 std::vector<Matrix_t> ¤tLayerPastWeightGradients = this->GetPastWeightGradientsAt(layerIndex);
122 for (
size_t k = 0; k < currentLayerPastWeightGradients.size(); k++) {
123 Architecture_t::ConstMult(currentLayerPastWeightGradients[k], this->GetMomentum());
124 Architecture_t::ScaleAdd(currentLayerPastWeightGradients[k], weightGradients[k], 1.0);
129 for (
size_t i = 0; i < weights.size(); i++) {
130 Architecture_t::ScaleAdd(weights[i], currentLayerPastWeightGradients[i], -this->GetLearningRate());
135template <
typename Architecture_t,
typename Layer_t,
typename DeepNet_t>
137 const std::vector<Matrix_t> &biasGradients) ->
void
142 std::vector<Matrix_t> ¤tLayerPastBiasGradients = this->GetPastBiasGradientsAt(layerIndex);
144 for (
size_t k = 0; k < currentLayerPastBiasGradients.size(); k++) {
145 Architecture_t::ConstMult(currentLayerPastBiasGradients[k], this->GetMomentum());
146 Architecture_t::ScaleAdd(currentLayerPastBiasGradients[k], biasGradients[k], 1.0);
151 for (
size_t i = 0; i < biases.size(); i++) {
152 Architecture_t::ScaleAdd(biases[i], currentLayerPastBiasGradients[i], -this->GetLearningRate());
Stochastic Batch Gradient Descent Optimizer class.
void UpdateWeights(size_t layerIndex, std::vector< Matrix_t > &weights, const std::vector< Matrix_t > &weightGradients)
Update the weights, given the current weight gradients.
~TSGD()=default
Destructor.
Scalar_t fMomentum
The momentum used for training.
typename Architecture_t::Scalar_t Scalar_t
TSGD(Scalar_t learningRate, DeepNet_t &deepNet, Scalar_t momentum)
Constructor.
std::vector< std::vector< Matrix_t > > & GetPastBiasGradients()
std::vector< std::vector< Matrix_t > > fPastBiasGradients
The sum of the past bias gradients associated with the deep net.
std::vector< std::vector< Matrix_t > > & GetPastWeightGradients()
void UpdateBiases(size_t layerIndex, std::vector< Matrix_t > &biases, const std::vector< Matrix_t > &biasGradients)
Update the biases, given the current bias gradients.
std::vector< Matrix_t > & GetPastWeightGradientsAt(size_t i)
std::vector< Matrix_t > & GetPastBiasGradientsAt(size_t i)
std::vector< std::vector< Matrix_t > > fPastWeightGradients
The sum of the past weight gradients associated with the deep net.
typename Architecture_t::Matrix_t Matrix_t
Scalar_t GetMomentum() const
Getters.
std::vector< Layer_t * > & GetLayers()
create variable transformations