File: category_encoding_layer.hpp

package info (click to toggle)
frugally-deep 0.18.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,036 kB
  • sloc: cpp: 6,680; python: 1,262; makefile: 4; sh: 1
file content (63 lines) | stat: -rw-r--r-- 2,501 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2016, Tobias Hermann.
// https://github.com/Dobiasd/frugally-deep
// Distributed under the MIT License.
// (See accompanying LICENSE file or at
//  https://opensource.org/licenses/MIT)

#pragma once

#include "fdeep/layers/layer.hpp"

#include <string>
#include <vector>

namespace fdeep {
namespace internal {

    class category_encoding_layer : public layer {
    public:
        explicit category_encoding_layer(const std::string& name,
            const std::size_t& num_tokens,
            const std::string& output_mode)
            : layer(name)
            , num_tokens_(num_tokens)
            , output_mode_(output_mode)
        {
            assertion(output_mode_ == "one_hot" || output_mode_ == "multi_hot" || output_mode_ == "count",
                "Unsupported output mode (" + output_mode_ + ").");
        }

    protected:
        tensors apply_impl(const tensors& inputs) const override
        {
            assertion(inputs.size() == 1, "need exactly one input");
            const auto input = inputs[0];
            assertion(input.shape().rank() == 1, "Tensor of rank 1 required, but shape is '" + show_tensor_shape(input.shape()) + "'");

            if (output_mode_ == "one_hot") {
                assertion(input.shape().depth_ == 1, "Tensor of depth 1 required, but is: " + fplus::show(input.shape().depth_));
                tensor out(tensor_shape(num_tokens_), float_type(0));
                const std::size_t idx = fplus::floor<float_type, std::size_t>(input.get_ignore_rank(tensor_pos(0)));
                assertion(idx <= num_tokens_, "Invalid input value (> num_tokens).");
                out.set_ignore_rank(tensor_pos(idx), 1);
                return { out };
            } else {
                tensor out(tensor_shape(num_tokens_), float_type(0));
                for (const auto& x : *(input.as_vector())) {
                    const std::size_t idx = fplus::floor<float_type, std::size_t>(x);
                    assertion(idx <= num_tokens_, "Invalid input value (> num_tokens).");
                    if (output_mode_ == "multi_hot") {
                        out.set_ignore_rank(tensor_pos(idx), 1);
                    } else if (output_mode_ == "count") {
                        out.set_ignore_rank(tensor_pos(idx), out.get_ignore_rank(tensor_pos(idx)) + 1);
                    }
                }
                return { out };
            }
        }
        std::size_t num_tokens_;
        std::string output_mode_;
    };

}
}