feat(math): implement slice for tensor_view
This commit is contained in:
+61
-8
@@ -2,13 +2,16 @@
|
||||
#define CENET_LAYER_HPP
|
||||
|
||||
#include "mat.hpp"
|
||||
#include "math/tensor.hpp"
|
||||
#include "tensor.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <istream>
|
||||
#include <ostream>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
namespace cenet {
|
||||
|
||||
@@ -32,13 +35,25 @@ struct is_tensor<tensor<T, Dims...>> : std::true_type {};
|
||||
template <typename T>
|
||||
static constexpr auto is_tensor_v = is_tensor<T>::value;
|
||||
|
||||
template <typename T>
|
||||
struct is_tokens : std::false_type {};
|
||||
|
||||
template <std::size_t I>
|
||||
struct is_tokens<std::array<std::uint32_t, I>> : std::true_type {};
|
||||
|
||||
template <>
|
||||
struct is_tokens<std::vector<std::uint32_t>> : std::true_type {};
|
||||
|
||||
template <typename T>
|
||||
static constexpr auto is_tokens_v = is_tokens<T>::value;
|
||||
|
||||
} // namespace detail
|
||||
|
||||
struct layer_info {
|
||||
protected:
|
||||
template <class NN, std::size_t I>
|
||||
struct input_type_s {
|
||||
using type = std::tuple_element_t<I - 1, typename NN::layer_types>::output_type;
|
||||
using type = typename std::tuple_element_t<I - 1, typename NN::layer_types>::output_type;
|
||||
};
|
||||
|
||||
template <class NN>
|
||||
@@ -57,7 +72,7 @@ class dense_layer final : public layer_info {
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
struct inner final {
|
||||
using input_type = input_type_s<NN, I>::type;
|
||||
using input_type = typename input_type_s<NN, I>::type;
|
||||
static_assert(detail::is_dense_v<input_type>);
|
||||
|
||||
using output_type = mat<1, Features, T>;
|
||||
@@ -161,7 +176,7 @@ class sigmoid_layer final : public layer_info {
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
struct inner final {
|
||||
using input_type = input_type_s<NN, I>::type;
|
||||
using input_type = typename input_type_s<NN, I>::type;
|
||||
static_assert(detail::is_dense_v<input_type>);
|
||||
using output_type = input_type;
|
||||
|
||||
@@ -188,7 +203,7 @@ private:
|
||||
|
||||
template <typename Error>
|
||||
auto backward(const input_type& input, gradient& grad, Error prevErr) const {
|
||||
return prevErr.hadamard(output.hadamard(static_cast<output_type::inner_type>(1) - output));
|
||||
return prevErr.hadamard(output.hadamard(static_cast<typename output_type::inner_type>(1) - output));
|
||||
}
|
||||
|
||||
void apply_grad(gradient&, double, double) {}
|
||||
@@ -201,7 +216,7 @@ class relu_layer final : public layer_info {
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
struct inner final {
|
||||
using input_type = input_type_s<NN, I>::type;
|
||||
using input_type = typename input_type_s<NN, I>::type;
|
||||
using output_type = input_type;
|
||||
|
||||
input_type savedInput;
|
||||
@@ -244,7 +259,7 @@ class conv2d_layer final : public layer_info {
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
struct inner final {
|
||||
using input_type = input_type_s<NN, I>::type;
|
||||
using input_type = typename input_type_s<NN, I>::type;
|
||||
static_assert(detail::is_tensor_v<input_type>);
|
||||
static_assert(input_type::RANK == 3);
|
||||
|
||||
@@ -425,7 +440,7 @@ class maxpool_layer : public layer_info {
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
struct inner final {
|
||||
using input_type = input_type_s<NN, I>::type;
|
||||
using input_type = typename input_type_s<NN, I>::type;
|
||||
static_assert(detail::is_tensor_v<input_type>);
|
||||
static_assert(std::is_same_v<typename input_type::inner_type, T>);
|
||||
static_assert(input_type::RANK == 3);
|
||||
@@ -515,7 +530,7 @@ class flatten_layer : public layer_info {
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
struct inner final {
|
||||
using input_type = input_type_s<NN, I>::type;
|
||||
using input_type = typename input_type_s<NN, I>::type;
|
||||
static_assert(detail::is_tensor_v<input_type>);
|
||||
|
||||
static constexpr std::size_t FEATURES = input_type::SIZE;
|
||||
@@ -552,6 +567,44 @@ private:
|
||||
};
|
||||
};
|
||||
|
||||
template <std::size_t VocabSize, std::size_t EmbedDims>
|
||||
class tok2vec_layer : public layer_info {
|
||||
template <typename Input, typename... Layers>
|
||||
friend class network;
|
||||
private:
|
||||
template <class NN, std::size_t I>
|
||||
struct inner final {
|
||||
using input_type = typename input_type_s<NN, I>::type;
|
||||
static_assert(detail::is_tokens_v<input_type>);
|
||||
|
||||
using output_type = math::matrix<float, math::dynamic_extent, EmbedDims>;
|
||||
output_type output;
|
||||
|
||||
struct gradient final {
|
||||
void reset() {}
|
||||
};
|
||||
|
||||
void dump(std::ostream& os) const {}
|
||||
|
||||
void load(std::istream& is) {}
|
||||
|
||||
template <typename Dist>
|
||||
void fill_random(const Dist& dist) {}
|
||||
|
||||
auto& forward(const input_type& input) {
|
||||
output.set_dimensions(input.size(), EmbedDims);
|
||||
return output;
|
||||
}
|
||||
|
||||
template <typename Error>
|
||||
auto backward(const input_type& input, gradient& grad, Error prevErr) {
|
||||
return std::move(prevErr);
|
||||
}
|
||||
|
||||
void apply_grad(gradient& grad, double divisor, double rate) {}
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace cenet
|
||||
|
||||
#endif // CENET_LAYER_HPP
|
||||
|
||||
Reference in New Issue
Block a user