feat(nn): introduce layer traits
This commit is contained in:
@@ -34,7 +34,7 @@ struct layer_info {
|
|||||||
protected:
|
protected:
|
||||||
template <class NN, std::size_t I>
|
template <class NN, std::size_t I>
|
||||||
struct input_type_s {
|
struct input_type_s {
|
||||||
using type = typename std::tuple_element_t<I - 1, typename NN::layer_types>::output_type;
|
using type = typename std::template tuple_element_t<I - 1, typename NN::layer_types>::output_type;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class NN>
|
template <class NN>
|
||||||
|
|||||||
@@ -1,23 +1,28 @@
|
|||||||
#ifndef CENET_LAYERS_CONVOLUTIONAL_HPP
|
#ifndef CENET_LAYERS_CONVOLUTIONAL_HPP
|
||||||
#define CENET_LAYERS_CONVOLUTIONAL_HPP
|
#define CENET_LAYERS_CONVOLUTIONAL_HPP
|
||||||
|
|
||||||
#include "../layer.hpp"
|
|
||||||
#include "../math/tensor_fwd.hpp"
|
#include "../math/tensor_fwd.hpp"
|
||||||
#include "../math/tensor_shape.hpp"
|
#include "../math/tensor_shape.hpp"
|
||||||
#include "../math/tensor_traits.hpp"
|
#include "../math/tensor_traits.hpp"
|
||||||
#include "../network.hpp"
|
#include "../network.hpp"
|
||||||
|
#include "./layer_fwd.hpp"
|
||||||
|
#include "./layer_traits.hpp"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
namespace cenet {
|
namespace cenet {
|
||||||
namespace layers {
|
namespace layers {
|
||||||
|
|
||||||
template <std::size_t OutChannels, std::size_t KernelSize, std::size_t Stride = 1, std::size_t Padding = 0>
|
template <std::size_t OutChannels, std::size_t KernelSize, std::size_t Stride = 1, std::size_t Padding = 0>
|
||||||
class conv2d final : public layer_info {
|
class conv2d final : public layer_tag {
|
||||||
template <typename Input, typename... Layers>
|
template <typename Input, typename... Layers>
|
||||||
friend class ::cenet::network;
|
friend class ::cenet::network;
|
||||||
private:
|
private:
|
||||||
template <class NN, std::size_t I>
|
template <class N, std::size_t I>
|
||||||
struct inner final {
|
struct inner final {
|
||||||
using input_type = typename input_type_s<NN, I>::type;
|
using traits = typename detail::layer_traits<network_layer<conv2d, N, I>>;
|
||||||
|
|
||||||
|
using input_type = typename traits::input_type;
|
||||||
static_assert(math::is_tensor_v<input_type>);
|
static_assert(math::is_tensor_v<input_type>);
|
||||||
static_assert(input_type::shape_type::rank == 3);
|
static_assert(input_type::shape_type::rank == 3);
|
||||||
|
|
||||||
@@ -172,9 +177,9 @@ private:
|
|||||||
double avgGrad = grad.weights[oc][ic][ky][kx] / divisor;
|
double avgGrad = grad.weights[oc][ic][ky][kx] / divisor;
|
||||||
|
|
||||||
grad.momentumWeights[oc][ic][ky][kx] =
|
grad.momentumWeights[oc][ic][ky][kx] =
|
||||||
beta1 * grad.momentumWeights[oc][ic][ky][kx] + (1.0 - beta1) * avgGrad;
|
(beta1 * grad.momentumWeights[oc][ic][ky][kx]) + ((1.0 - beta1) * avgGrad);
|
||||||
grad.varianceWeights[oc][ic][ky][kx] =
|
grad.varianceWeights[oc][ic][ky][kx] =
|
||||||
beta2 * grad.varianceWeights[oc][ic][ky][kx] + (1.0 - beta2) * avgGrad * avgGrad;
|
(beta2 * grad.varianceWeights[oc][ic][ky][kx]) + ((1.0 - beta2) * avgGrad * avgGrad);
|
||||||
|
|
||||||
auto momentumCorrected =
|
auto momentumCorrected =
|
||||||
grad.momentumWeights[oc][ic][ky][kx] / (1.0 - std::pow(beta1, grad.time));
|
grad.momentumWeights[oc][ic][ky][kx] / (1.0 - std::pow(beta1, grad.time));
|
||||||
@@ -189,8 +194,8 @@ private:
|
|||||||
|
|
||||||
auto avgGrad = grad.biases[oc] / divisor;
|
auto avgGrad = grad.biases[oc] / divisor;
|
||||||
|
|
||||||
grad.momentumBiases[oc] = beta1 * grad.momentumBiases[oc] + (1.0 - beta1) * avgGrad;
|
grad.momentumBiases[oc] = (beta1 * grad.momentumBiases[oc]) + ((1.0 - beta1) * avgGrad);
|
||||||
grad.varianceBiases[oc] = beta2 * grad.varianceBiases[oc] + (1.0 - beta2) * avgGrad * avgGrad;
|
grad.varianceBiases[oc] = (beta2 * grad.varianceBiases[oc]) + ((1.0 - beta2) * avgGrad * avgGrad);
|
||||||
|
|
||||||
auto momentumCorrected = grad.momentumBiases[oc] / (1.0 - std::pow(beta1, grad.time));
|
auto momentumCorrected = grad.momentumBiases[oc] / (1.0 - std::pow(beta1, grad.time));
|
||||||
auto varianceCorrected = grad.varianceBiases[oc] / (1.0 - std::pow(beta2, grad.time));
|
auto varianceCorrected = grad.varianceBiases[oc] / (1.0 - std::pow(beta2, grad.time));
|
||||||
@@ -202,13 +207,15 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <std::size_t PoolSize = 2, std::size_t Stride = 2>
|
template <std::size_t PoolSize = 2, std::size_t Stride = 2>
|
||||||
class maxpool : public layer_info {
|
class maxpool final : public layer_tag {
|
||||||
template <typename Input, typename... Layers>
|
template <typename Input, typename... Layers>
|
||||||
friend class ::cenet::network;
|
friend class ::cenet::network;
|
||||||
private:
|
private:
|
||||||
template <class NN, std::size_t I>
|
template <class N, std::size_t I>
|
||||||
struct inner final {
|
struct inner final {
|
||||||
using input_type = typename input_type_s<NN, I>::type;
|
using traits = typename detail::layer_traits<network_layer<maxpool, N, I>>;
|
||||||
|
|
||||||
|
using input_type = typename traits::input_type;
|
||||||
static_assert(math::is_tensor_v<input_type>);
|
static_assert(math::is_tensor_v<input_type>);
|
||||||
static_assert(input_type::shape_type::rank == 3);
|
static_assert(input_type::shape_type::rank == 3);
|
||||||
static_assert(!input_type::traits::dynamic, "dynamic input to maxpool is currently unsupported");
|
static_assert(!input_type::traits::dynamic, "dynamic input to maxpool is currently unsupported");
|
||||||
@@ -299,13 +306,15 @@ private:
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
class flatten final : public layer_info {
|
class flatten final : public layer_tag {
|
||||||
template <typename Input, typename... Layers>
|
template <typename Input, typename... Layers>
|
||||||
friend class ::cenet::network;
|
friend class ::cenet::network;
|
||||||
private:
|
private:
|
||||||
template <class NN, std::size_t I>
|
template <class N, std::size_t I>
|
||||||
struct inner final {
|
struct inner final {
|
||||||
using input_type = typename input_type_s<NN, I>::type;
|
using traits = typename detail::layer_traits<network_layer<flatten, N, I>>;
|
||||||
|
|
||||||
|
using input_type = typename traits::input_type;
|
||||||
static_assert(math::is_tensor_v<input_type>);
|
static_assert(math::is_tensor_v<input_type>);
|
||||||
static_assert(!input_type::traits::dynamic, "dynamic input to flatten is currently unsupported");
|
static_assert(!input_type::traits::dynamic, "dynamic input to flatten is currently unsupported");
|
||||||
|
|
||||||
|
|||||||
@@ -5,22 +5,27 @@
|
|||||||
#ifndef CENET_LAYERS_DENSE_HPP
|
#ifndef CENET_LAYERS_DENSE_HPP
|
||||||
#define CENET_LAYERS_DENSE_HPP
|
#define CENET_LAYERS_DENSE_HPP
|
||||||
|
|
||||||
#include "../layer.hpp"
|
|
||||||
#include "../math/tensor_fwd.hpp"
|
#include "../math/tensor_fwd.hpp"
|
||||||
#include "../network.hpp"
|
#include "../network.hpp"
|
||||||
|
#include "./layer_fwd.hpp"
|
||||||
|
#include "./layer_traits.hpp"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
namespace cenet {
|
namespace cenet {
|
||||||
namespace layers {
|
namespace layers {
|
||||||
|
|
||||||
// TODO: redesign the layer interface after porting the old layers
|
// TODO: redesign the layer interface after porting the old layers
|
||||||
template <std::size_t Features>
|
template <std::size_t Features>
|
||||||
class dense final : public layer_info {
|
class dense final : public layer_tag {
|
||||||
template <typename Input, typename... Layers>
|
template <typename Input, typename... Layers>
|
||||||
friend class ::cenet::network;
|
friend class ::cenet::network;
|
||||||
private:
|
private:
|
||||||
template <class NN, std::size_t I>
|
template <class N, std::size_t I>
|
||||||
struct inner final {
|
struct inner final {
|
||||||
using input_type = typename input_type_s<NN, I>::type;
|
using traits = typename detail::layer_traits<network_layer<dense, N, I>>;
|
||||||
|
|
||||||
|
using input_type = typename traits::input_type;
|
||||||
using value_type = typename input_type::value_type;
|
using value_type = typename input_type::value_type;
|
||||||
using output_type = math::matrix<value_type, 1, Features>;
|
using output_type = math::matrix<value_type, 1, Features>;
|
||||||
|
|
||||||
@@ -97,9 +102,10 @@ private:
|
|||||||
for (std::size_t col = 0; col < Features; ++col) {
|
for (std::size_t col = 0; col < Features; ++col) {
|
||||||
auto avgGrad = grad.weights[row][col] / divisor;
|
auto avgGrad = grad.weights[row][col] / divisor;
|
||||||
|
|
||||||
grad.momentumWeights[row][col] = beta1 * grad.momentumWeights[row][col] + (1.0 - beta1) * avgGrad;
|
grad.momentumWeights[row][col] =
|
||||||
|
(beta1 * grad.momentumWeights[row][col]) + ((1.0 - beta1) * avgGrad);
|
||||||
grad.varianceWeights[row][col] =
|
grad.varianceWeights[row][col] =
|
||||||
beta2 * grad.varianceWeights[row][col] + (1.0 - beta2) * (avgGrad * avgGrad);
|
(beta2 * grad.varianceWeights[row][col]) + ((1.0 - beta2) * (avgGrad * avgGrad));
|
||||||
|
|
||||||
auto momentumCorrected = grad.momentumWeights[row][col] / (1.0 - std::pow(beta1, grad.time));
|
auto momentumCorrected = grad.momentumWeights[row][col] / (1.0 - std::pow(beta1, grad.time));
|
||||||
auto varianceCorrected = grad.varianceWeights[row][col] / (1.0 - std::pow(beta2, grad.time));
|
auto varianceCorrected = grad.varianceWeights[row][col] / (1.0 - std::pow(beta2, grad.time));
|
||||||
@@ -111,8 +117,9 @@ private:
|
|||||||
for (std::size_t col = 0; col < Features; ++col) {
|
for (std::size_t col = 0; col < Features; ++col) {
|
||||||
double avgGrad = grad.biases[0][col] / divisor;
|
double avgGrad = grad.biases[0][col] / divisor;
|
||||||
|
|
||||||
grad.momentumBiases[0][col] = beta1 * grad.momentumBiases[0][col] + (1.0 - beta1) * avgGrad;
|
grad.momentumBiases[0][col] = (beta1 * grad.momentumBiases[0][col]) + ((1.0 - beta1) * avgGrad);
|
||||||
grad.varianceBiases[0][col] = beta2 * grad.varianceBiases[0][col] + (1.0 - beta2) * (avgGrad * avgGrad);
|
grad.varianceBiases[0][col] =
|
||||||
|
(beta2 * grad.varianceBiases[0][col]) + ((1.0 - beta2) * (avgGrad * avgGrad));
|
||||||
|
|
||||||
auto momentumCorrected = grad.momentumBiases[0][col] / (1.0 - std::pow(beta1, grad.time));
|
auto momentumCorrected = grad.momentumBiases[0][col] / (1.0 - std::pow(beta1, grad.time));
|
||||||
auto varianceCorrected = grad.varianceBiases[0][col] / (1.0 - std::pow(beta2, grad.time));
|
auto varianceCorrected = grad.varianceBiases[0][col] / (1.0 - std::pow(beta2, grad.time));
|
||||||
@@ -123,13 +130,15 @@ private:
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
class sigmoid final : public layer_info {
|
class sigmoid final : public layer_tag {
|
||||||
template <typename Input, typename... Layers>
|
template <typename Input, typename... Layers>
|
||||||
friend class ::cenet::network;
|
friend class ::cenet::network;
|
||||||
private:
|
private:
|
||||||
template <class NN, std::size_t I>
|
template <class N, std::size_t I>
|
||||||
struct inner final {
|
struct inner final {
|
||||||
using input_type = typename input_type_s<NN, I>::type;
|
using traits = typename detail::layer_traits<network_layer<sigmoid, N, I>>;
|
||||||
|
|
||||||
|
using input_type = typename traits::input_type;
|
||||||
using output_type = input_type;
|
using output_type = input_type;
|
||||||
|
|
||||||
typename output_type::template with_storage<math::inline_storage_tag> retained;
|
typename output_type::template with_storage<math::inline_storage_tag> retained;
|
||||||
@@ -160,13 +169,15 @@ private:
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
class relu final : public layer_info {
|
class relu final : public layer_tag {
|
||||||
template <typename Input, typename... Layers>
|
template <typename Input, typename... Layers>
|
||||||
friend class ::cenet::network;
|
friend class ::cenet::network;
|
||||||
private:
|
private:
|
||||||
template <class NN, std::size_t I>
|
template <class N, std::size_t I>
|
||||||
struct inner final {
|
struct inner final {
|
||||||
using input_type = typename input_type_s<NN, I>::type;
|
using traits = typename detail::layer_traits<network_layer<relu, N, I>>;
|
||||||
|
|
||||||
|
using input_type = typename traits::input_type;
|
||||||
using output_type = input_type;
|
using output_type = input_type;
|
||||||
|
|
||||||
typename input_type::template with_storage<math::inline_storage_tag> retained;
|
typename input_type::template with_storage<math::inline_storage_tag> retained;
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef CENET_LAYERS_LAYER_FWD_HPP
|
||||||
|
#define CENET_LAYERS_LAYER_FWD_HPP
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
namespace cenet {
|
||||||
|
namespace layers {
|
||||||
|
|
||||||
|
struct layer_tag {};
|
||||||
|
|
||||||
|
template <typename L, typename N, std::size_t I>
|
||||||
|
struct network_layer {
|
||||||
|
using layer_type = L;
|
||||||
|
using network_type = N;
|
||||||
|
|
||||||
|
static constexpr auto position = I;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace layers
|
||||||
|
} // namespace cenet
|
||||||
|
|
||||||
|
#endif // CENET_LAYERS_LAYER_FWD_HPP
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
#ifndef CENET_LAYERS_LAYER_TRAITS_HPP
|
||||||
|
#define CENET_LAYERS_LAYER_TRAITS_HPP
|
||||||
|
|
||||||
|
#include "layer_fwd.hpp"
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
namespace cenet {
|
||||||
|
namespace layers {
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct is_layer : std::is_base_of<layer_tag, T> {};
|
||||||
|
|
||||||
|
template <typename L, typename... Args>
|
||||||
|
struct is_layer<network_layer<L, Args...>> : is_layer<L> {};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static constexpr auto is_layer_v = is_layer<T>::value;
|
||||||
|
|
||||||
|
template <typename N, std::size_t I>
|
||||||
|
struct layer_input {
|
||||||
|
using type = typename N::template layer<I - 1>::output_type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename N>
|
||||||
|
struct layer_input<N, 0> {
|
||||||
|
using type = typename N::input_type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct layer_traits {};
|
||||||
|
|
||||||
|
template <typename L, typename N, std::size_t I>
|
||||||
|
struct layer_traits<network_layer<L, N, I>> : network_layer<L, N, I> {
|
||||||
|
using input_type = typename layer_input<N, I>::type;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
} // namespace layers
|
||||||
|
} // namespace cenet
|
||||||
|
|
||||||
|
#endif // CENET_LAYERS_LAYER_TRAITS_HPP
|
||||||
@@ -36,6 +36,9 @@ private:
|
|||||||
const auto& get_layer() const {
|
const auto& get_layer() const {
|
||||||
return std::get<I>(m_layers);
|
return std::get<I>(m_layers);
|
||||||
}
|
}
|
||||||
|
public:
|
||||||
|
template <std::size_t I>
|
||||||
|
using layer = std::tuple_element_t<I, layer_types>;
|
||||||
public:
|
public:
|
||||||
using input_type = Input;
|
using input_type = Input;
|
||||||
using output_type = typename output_layer_type::output_type;
|
using output_type = typename output_layer_type::output_type;
|
||||||
|
|||||||
Reference in New Issue
Block a user