-
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.
- Loading branch information
Showing
15 changed files
with
164 additions
and
99 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
#pragma once | ||
|
||
#include "src/Activation.hpp" | ||
#include "src/Layer.hpp" | ||
#include "src/Loss.hpp" | ||
#include "src/MLP.hpp" | ||
#include "src/Module.hpp" | ||
#include "src/Neuron.hpp" | ||
#include "src/Optimizer.hpp" | ||
#include "src/Type.hpp" | ||
#include "src/Value.hpp" | ||
#include "src/Vector.hpp" | ||
#include "src/core/Type.hpp" | ||
#include "src/core/Value.hpp" | ||
#include "src/core/Vector.hpp" | ||
#include "src/nn/Activation.hpp" | ||
#include "src/nn/Layer.hpp" | ||
#include "src/nn/Loss.hpp" | ||
#include "src/nn/Module.hpp" | ||
#include "src/nn/Neuron.hpp" | ||
#include "src/nn/Optimizer.hpp" | ||
#include "src/nn/Sequential.hpp" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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,20 @@ | ||
#pragma once | ||
|
||
#include "../core/Vector.hpp" | ||
|
||
namespace shkyera { | ||
|
||
template <typename T> class Module; | ||
template <typename T> using ModulePtr = std::shared_ptr<Module<T>>; | ||
|
||
template <typename T> class Module { | ||
protected: | ||
Module() = default; | ||
|
||
public: | ||
Vector<T> forward(const Vector<T> &x) const { return (*this)(x); } | ||
virtual Vector<T> operator()(const Vector<T> &x) const { return x; } | ||
virtual std::vector<ValuePtr<T>> parameters() const { 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
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,76 @@ | ||
#pragma once | ||
|
||
#include "../core/Type.hpp" | ||
#include "Activation.hpp" | ||
#include "Module.hpp" | ||
|
||
namespace shkyera { | ||
|
||
template <typename T> class Sequential; | ||
template <typename T> class SequentialBuilder; | ||
template <typename T> using SequentialPtr = std::shared_ptr<Sequential<T>>; | ||
|
||
using Sequential32 = Sequential<Type::float32>; | ||
using Sequential64 = Sequential<Type::float64>; | ||
using SequentialBuilder32 = SequentialBuilder<Type::float32>; | ||
using SequentialBuilder64 = SequentialBuilder<Type::float64>; | ||
|
||
template <typename T> class Sequential : public Module<T> { | ||
private: | ||
std::vector<ModulePtr<T>> _layers; | ||
|
||
Sequential(const std::vector<ModulePtr<T>> &layers); | ||
|
||
public: | ||
static SequentialPtr<T> create(const std::vector<ModulePtr<T>> &layers); | ||
|
||
virtual Vector<T> operator()(const Vector<T> &x) const override; | ||
virtual std::vector<ValuePtr<T>> parameters() const override; | ||
}; | ||
|
||
template <typename T> class SequentialBuilder { | ||
private: | ||
std::vector<ModulePtr<T>> _layers; | ||
|
||
SequentialBuilder() = default; | ||
|
||
public: | ||
static SequentialBuilder<T> begin(); | ||
|
||
SequentialBuilder<T> add(ModulePtr<T> layer); | ||
SequentialPtr<T> build(); | ||
}; | ||
|
||
template <typename T> Sequential<T>::Sequential(const std::vector<ModulePtr<T>> &layers) : _layers(layers) {} | ||
|
||
template <typename T> SequentialPtr<T> Sequential<T>::create(const std::vector<ModulePtr<T>> &layers) { | ||
return std::shared_ptr<Sequential<T>>(new Sequential<T>(layers)); | ||
} | ||
|
||
template <typename T> Vector<T> Sequential<T>::operator()(const Vector<T> &x) const { | ||
Vector<T> out = (*_layers[0])(x); | ||
|
||
std::for_each(_layers.begin() + 1, _layers.end(), [&out](ModulePtr<T> layer) { out = layer->forward(out); }); | ||
|
||
return out; | ||
} | ||
|
||
template <typename T> std::vector<ValuePtr<T>> Sequential<T>::parameters() const { | ||
std::vector<ValuePtr<T>> params; | ||
|
||
for (const ModulePtr<T> &l : _layers) { | ||
std::vector<ValuePtr<T>> layerParams = l->parameters(); | ||
params.insert(params.end(), layerParams.begin(), layerParams.end()); | ||
} | ||
|
||
return params; | ||
} | ||
|
||
template <typename T> SequentialBuilder<T> SequentialBuilder<T>::begin() { return SequentialBuilder<T>(); } | ||
template <typename T> SequentialBuilder<T> SequentialBuilder<T>::add(ModulePtr<T> layer) { | ||
_layers.push_back(layer); | ||
return *this; | ||
} | ||
template <typename T> SequentialPtr<T> SequentialBuilder<T>::build() { return Sequential<T>::create(_layers); } | ||
|
||
} // 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