-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from fszewczyk/sgd
SGD with Momentum Optimizer
- Loading branch information
Showing
4 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Copyright © 2023 Franciszek Szewczyk. None of the rights reserved. | ||
* This code is released under the Beerware License. If you find this code useful or you appreciate the work, you are | ||
* encouraged to buy the author a beer in return. | ||
* Contact the author at [email protected] for inquiries and support. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <unordered_map> | ||
#include <vector> | ||
|
||
#include "../../core/Type.hpp" | ||
#include "../../core/Value.hpp" | ||
#include "../Module.hpp" | ||
#include "Optimizer.hpp" | ||
|
||
namespace shkyera { | ||
|
||
template <typename T> class SGD; | ||
using SGD32 = SGD<Type::float32>; | ||
using SGD64 = SGD<Type::float32>; | ||
|
||
template <typename T> class SGD : public Optimizer<T> { | ||
private: | ||
T _momentum; | ||
std::unordered_map<Value<T> *, T> _moment; | ||
|
||
T getMoment(const ValuePtr<T> &v); | ||
|
||
public: | ||
SGD(std::vector<ValuePtr<T>> params, T learningRate, T momentum = 0.9); | ||
|
||
void step() override; | ||
}; | ||
|
||
template <typename T> | ||
SGD<T>::SGD(std::vector<ValuePtr<T>> params, T learningRate, T momentum) : Optimizer<T>(params, learningRate) { | ||
_momentum = momentum; | ||
} | ||
|
||
template <typename T> void SGD<T>::step() { | ||
static bool initialized = false; | ||
|
||
for (const ValuePtr<T> ¶m : this->_parameters) { | ||
T gradient = param->getGradient(); | ||
T moment = initialized ? _momentum * getMoment(param) + (1 - _momentum) * gradient : gradient; | ||
_moment.insert({param.get(), moment}); | ||
|
||
param->_data -= this->_learningRate * moment; | ||
} | ||
} | ||
|
||
template <typename T> T SGD<T>::getMoment(const ValuePtr<T> &v) { | ||
auto moment = _moment.find(v.get()); | ||
if (moment == _moment.end()) { | ||
_moment.insert({v.get(), 0}); | ||
return 0; | ||
} | ||
return moment->second; | ||
} | ||
|
||
} // namespace shkyera |