File: tensor_shape_variable.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 (141 lines) | stat: -rw-r--r-- 4,133 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// 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/common.hpp"

#include <algorithm>
#include <cstddef>
#include <cstdlib>
#include <string>
#include <vector>

namespace fdeep {
namespace internal {

    class tensor_shape_variable {
    public:
        explicit tensor_shape_variable(
            fplus::maybe<std::size_t> size_dim_5,
            fplus::maybe<std::size_t> size_dim_4,
            fplus::maybe<std::size_t> height,
            fplus::maybe<std::size_t> width,
            fplus::maybe<std::size_t> depth)
            : size_dim_5_(size_dim_5)
            , size_dim_4_(size_dim_4)
            , height_(height)
            , width_(width)
            , depth_(depth)
            , rank_(5)
        {
        }

        explicit tensor_shape_variable(
            fplus::maybe<std::size_t> size_dim_4,
            fplus::maybe<std::size_t> height,
            fplus::maybe<std::size_t> width,
            fplus::maybe<std::size_t> depth)
            : size_dim_5_(1)
            , size_dim_4_(size_dim_4)
            , height_(height)
            , width_(width)
            , depth_(depth)
            , rank_(4)
        {
        }

        explicit tensor_shape_variable(
            fplus::maybe<std::size_t> height,
            fplus::maybe<std::size_t> width,
            fplus::maybe<std::size_t> depth)
            : size_dim_5_(1)
            , size_dim_4_(1)
            , height_(height)
            , width_(width)
            , depth_(depth)
            , rank_(3)
        {
        }

        explicit tensor_shape_variable(
            fplus::maybe<std::size_t> width,
            fplus::maybe<std::size_t> depth)
            : size_dim_5_(1)
            , size_dim_4_(1)
            , height_(1)
            , width_(width)
            , depth_(depth)
            , rank_(2)
        {
        }

        explicit tensor_shape_variable(
            fplus::maybe<std::size_t> depth)
            : size_dim_5_(1)
            , size_dim_4_(1)
            , height_(1)
            , width_(1)
            , depth_(depth)
            , rank_(1)
        {
        }

        std::size_t minimal_volume() const
        {
            return fplus::just_with_default<std::size_t>(1, size_dim_5_) * fplus::just_with_default<std::size_t>(1, size_dim_4_) * fplus::just_with_default<std::size_t>(1, height_) * fplus::just_with_default<std::size_t>(1, width_) * fplus::just_with_default<std::size_t>(1, depth_);
        }

        std::size_t rank() const
        {
            return rank_;
        }

        fplus::maybe<std::size_t> size_dim_5_;
        fplus::maybe<std::size_t> size_dim_4_;
        fplus::maybe<std::size_t> height_;
        fplus::maybe<std::size_t> width_;
        fplus::maybe<std::size_t> depth_;

    private:
        std::size_t rank_;
    };

    inline bool operator==(const tensor_shape_variable& lhs, const tensor_shape_variable& rhs)
    {
        return lhs.size_dim_5_ == rhs.size_dim_5_ && lhs.size_dim_4_ == rhs.size_dim_4_ && lhs.height_ == rhs.height_ && lhs.width_ == rhs.width_ && lhs.depth_ == rhs.depth_;
    }

    inline bool operator!=(const tensor_shape_variable& lhs, const tensor_shape_variable& rhs)
    {
        return !(lhs == rhs);
    }

}

using tensor_shape_variable = internal::tensor_shape_variable;

inline std::string show_tensor_shape_variable(const tensor_shape_variable& s)
{
    const std::vector<fplus::maybe<std::size_t>> dimensions = {
        s.size_dim_5_,
        s.size_dim_4_,
        s.height_,
        s.width_,
        s.depth_
    };
    const auto dimensions_repr = fplus::transform(
        fplus::show_maybe<std::size_t>, fplus::drop(5 - s.rank(), dimensions));
    return std::to_string(s.rank()) + fplus::show_cont_with_frame(", ", "(", ")", dimensions_repr);
}

inline std::string show_tensor_shapes_variable(
    const std::vector<tensor_shape_variable>& shapes)
{
    return fplus::show_cont(fplus::transform(show_tensor_shape_variable, shapes));
}

}