-
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 #7 from fszewczyk/dropout
Dropout Layer and Moduliarizing Activations
- Loading branch information
Showing
19 changed files
with
289 additions
and
65 deletions.
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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,26 @@ | ||
/** | ||
* 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 szewczyk.franciszek02@gmail.com for inquiries and support. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "../../core/Type.hpp" | ||
#include "../Module.hpp" | ||
|
||
namespace shkyera { | ||
|
||
template <typename T> class Activation; | ||
|
||
template <typename T> class Activation : public Module<T> { | ||
protected: | ||
Activation() = default; | ||
|
||
public: | ||
virtual Vector<T> operator()(const Vector<T> &x) const override { return x; } | ||
virtual std::vector<ValuePtr<T>> parameters() const override { return {}; } | ||
}; | ||
|
||
} // namespace shkyera |
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,38 @@ | ||
/** | ||
* 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 szewczyk.franciszek02@gmail.com for inquiries and support. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "Activation.hpp" | ||
|
||
namespace shkyera { | ||
|
||
template <typename T> class Exp; | ||
using Exp32 = Exp<Type::float32>; | ||
using Exp64 = Exp<Type::float64>; | ||
|
||
template <typename T> class Exp : public Activation<T> { | ||
public: | ||
static std::shared_ptr<Exp<T>> create(); | ||
|
||
virtual Vector<T> operator()(const Vector<T> &x) const override; | ||
}; | ||
|
||
template <typename T> std::shared_ptr<Exp<T>> Exp<T>::create() { return std::shared_ptr<Exp<T>>(new Exp<T>()); } | ||
|
||
template <typename T> Vector<T> Exp<T>::operator()(const Vector<T> &x) const { | ||
std::vector<ValuePtr<T>> out; | ||
out.reserve(x.size()); | ||
|
||
for (size_t i = 0; i < x.size(); ++i) { | ||
out.emplace_back(x[i]->exp()); | ||
} | ||
|
||
return Vector<T>(out); | ||
} | ||
|
||
} // namespace shkyera |
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,38 @@ | ||
/** | ||
* 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 szewczyk.franciszek02@gmail.com for inquiries and support. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "Activation.hpp" | ||
|
||
namespace shkyera { | ||
|
||
template <typename T> class ReLU; | ||
using ReLU32 = ReLU<Type::float32>; | ||
using ReLU64 = ReLU<Type::float64>; | ||
|
||
template <typename T> class ReLU : public Activation<T> { | ||
public: | ||
static std::shared_ptr<ReLU<T>> create(); | ||
|
||
virtual Vector<T> operator()(const Vector<T> &x) const override; | ||
}; | ||
|
||
template <typename T> std::shared_ptr<ReLU<T>> ReLU<T>::create() { return std::shared_ptr<ReLU<T>>(new ReLU<T>()); } | ||
|
||
template <typename T> Vector<T> ReLU<T>::operator()(const Vector<T> &x) const { | ||
std::vector<ValuePtr<T>> out; | ||
out.reserve(x.size()); | ||
|
||
for (size_t i = 0; i < x.size(); ++i) { | ||
out.emplace_back(x[i]->relu()); | ||
} | ||
|
||
return Vector<T>(out); | ||
} | ||
|
||
} // namespace shkyera |
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,40 @@ | ||
/** | ||
* 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 szewczyk.franciszek02@gmail.com for inquiries and support. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "Activation.hpp" | ||
|
||
namespace shkyera { | ||
|
||
template <typename T> class Sigmoid; | ||
using Sigmoid32 = Sigmoid<Type::float32>; | ||
using Sigmoid64 = Sigmoid<Type::float64>; | ||
|
||
template <typename T> class Sigmoid : public Activation<T> { | ||
public: | ||
static std::shared_ptr<Sigmoid<T>> create(); | ||
|
||
virtual Vector<T> operator()(const Vector<T> &x) const override; | ||
}; | ||
|
||
template <typename T> std::shared_ptr<Sigmoid<T>> Sigmoid<T>::create() { | ||
return std::shared_ptr<Sigmoid<T>>(new Sigmoid<T>()); | ||
} | ||
|
||
template <typename T> Vector<T> Sigmoid<T>::operator()(const Vector<T> &x) const { | ||
std::vector<ValuePtr<T>> out; | ||
out.reserve(x.size()); | ||
|
||
for (size_t i = 0; i < x.size(); ++i) { | ||
out.emplace_back(x[i]->sigmoid()); | ||
} | ||
|
||
return Vector<T>(out); | ||
} | ||
|
||
} // namespace shkyera |
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,38 @@ | ||
/** | ||
* 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 szewczyk.franciszek02@gmail.com for inquiries and support. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "Activation.hpp" | ||
|
||
namespace shkyera { | ||
|
||
template <typename T> class Tanh; | ||
using Tanh32 = Tanh<Type::float32>; | ||
using Tanh64 = Tanh<Type::float64>; | ||
|
||
template <typename T> class Tanh : public Activation<T> { | ||
public: | ||
static std::shared_ptr<Tanh<T>> create(); | ||
|
||
virtual Vector<T> operator()(const Vector<T> &x) const override; | ||
}; | ||
|
||
template <typename T> std::shared_ptr<Tanh<T>> Tanh<T>::create() { return std::shared_ptr<Tanh<T>>(new Tanh<T>()); } | ||
|
||
template <typename T> Vector<T> Tanh<T>::operator()(const Vector<T> &x) const { | ||
std::vector<ValuePtr<T>> out; | ||
out.reserve(x.size()); | ||
|
||
for (size_t i = 0; i < x.size(); ++i) { | ||
out.emplace_back(x[i]->tanh()); | ||
} | ||
|
||
return Vector<T>(out); | ||
} | ||
|
||
} // namespace shkyera |
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,59 @@ | ||
/** | ||
* 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 szewczyk.franciszek02@gmail.com for inquiries and support. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "../../core/Utils.hpp" | ||
#include "Linear.hpp" | ||
|
||
namespace shkyera { | ||
|
||
template <typename T> class Dropout; | ||
template <typename T> using DropoutPtr = std::shared_ptr<Dropout<T>>; | ||
|
||
using Dropout32 = Dropout<Type::float32>; | ||
using Dropout64 = Dropout<Type::float64>; | ||
|
||
template <typename T> class Dropout : public Linear<T> { | ||
private: | ||
double _dropout; | ||
|
||
Dropout(size_t input, size_t size, double dropout); | ||
|
||
public: | ||
static DropoutPtr<T> create(size_t input, size_t size, double dropout); | ||
|
||
virtual Vector<T> operator()(const Vector<T> &x) const override; | ||
}; | ||
|
||
template <typename T> Dropout<T>::Dropout(size_t input, size_t size, double dropout) : Linear<T>(input, size) { | ||
if (_dropout < 0 || _dropout >= 1) { | ||
throw std::invalid_argument("Droput rate must be in the range [0,1). You set it to " + std::to_string(dropout) + | ||
"."); | ||
} | ||
_dropout = dropout; | ||
} | ||
|
||
template <typename T> DropoutPtr<T> Dropout<T>::create(size_t input, size_t size, double dropout) { | ||
return std::shared_ptr<Dropout<T>>(new Dropout(input, size, dropout)); | ||
} | ||
|
||
template <typename T> Vector<T> Dropout<T>::operator()(const Vector<T> &x) const { | ||
std::vector<ValuePtr<T>> alteredInput; | ||
alteredInput.reserve(x.size()); | ||
auto scaling = Value<T>::create(1.0 / (1 - _dropout)); | ||
for (size_t i = 0; i < x.size(); ++i) | ||
alteredInput.push_back(x[i] * scaling); | ||
|
||
std::vector<size_t> indicesToRemove = utils::sample<size_t>(0, x.size() - 1, _dropout * x.size(), false); | ||
for (size_t idxToRemove : indicesToRemove) | ||
alteredInput[idxToRemove] = Value<T>::create(0); | ||
|
||
return Linear<T>::operator()(Vector<T>(alteredInput)); | ||
} | ||
|
||
} // namespace shkyera |
Oops, something went wrong.