Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * Copyright (C) 2021 hyeonseok lee <hs89.lee@samsung.com>
4 : *
5 : * @file rnncell.h
6 : * @date 29 Oct 2021
7 : * @brief This is Recurrent Layer Cell Class of Neural Network
8 : * @see https://github.com/nnstreamer/nntrainer
9 : * @author hyeonseok lee <hs89.lee@samsung.com>
10 : * @bug No known bugs except for NYI items
11 : *
12 : */
13 :
14 : #ifndef __RNNCELL_H__
15 : #define __RNNCELL_H__
16 : #ifdef __cplusplus
17 :
18 : #include <acti_func.h>
19 : #include <common_properties.h>
20 : #include <layer_impl.h>
21 :
22 : namespace nntrainer {
23 :
24 : /**
25 : * @class RNNCellLayer
26 : * @brief RNNCellLayer
27 : */
28 : class RNNCellLayer : public LayerImpl {
29 : public:
30 : /**
31 : * @brief Constructor of RNNCellLayer
32 : */
33 : RNNCellLayer();
34 :
35 : /**
36 : * @brief Destructor of RNNCellLayer
37 : */
38 98 : ~RNNCellLayer() = default;
39 :
40 : /**
41 : * @brief Move constructor.
42 : * @param[in] RNNCellLayer &&
43 : */
44 : RNNCellLayer(RNNCellLayer &&rhs) noexcept = default;
45 :
46 : /**
47 : * @brief Move assignment operator.
48 : * @parma[in] rhs RNNCellLayer to be moved.
49 : */
50 : RNNCellLayer &operator=(RNNCellLayer &&rhs) = default;
51 :
52 : /**
53 : * @copydoc Layer::finalize(InitLayerContext &context)
54 : */
55 : void finalize(InitLayerContext &context) override;
56 :
57 : /**
58 : * @copydoc Layer::forwarding(RunLayerContext &context, bool training)
59 : */
60 : void forwarding(RunLayerContext &context, bool training) override;
61 :
62 : /**
63 : * @copydoc Layer::calcDerivative(RunLayerContext &context)
64 : */
65 : void calcDerivative(RunLayerContext &context) override;
66 :
67 : /**
68 : * @copydoc Layer::calcGradient(RunLayerContext &context)
69 : */
70 : void calcGradient(RunLayerContext &context) override;
71 :
72 : /**
73 : * @copydoc Layer::exportTo(Exporter &exporter, ml::train::ExportMethods
74 : * method)
75 : */
76 : void exportTo(Exporter &exporter,
77 : const ml::train::ExportMethods &method) const override;
78 :
79 : /**
80 : * @copydoc Layer::getType()
81 : */
82 535 : const std::string getType() const override { return RNNCellLayer::type; };
83 :
84 : /**
85 : * @copydoc Layer::supportBackwarding()
86 : */
87 54 : bool supportBackwarding() const override { return true; }
88 :
89 : /**
90 : * @copydoc Layer::setProperty(const PropertyType type, const std::string
91 : * &value)
92 : */
93 : void setProperty(const std::vector<std::string> &values) override;
94 :
95 : /**
96 : * @copydoc Layer::setBatch(RunLayerContext &context, unsigned int batch)
97 : */
98 : void setBatch(RunLayerContext &context, unsigned int batch) override;
99 :
100 : static constexpr const char *type = "rnncell";
101 :
102 : private:
103 : enum INOUT_INDEX {
104 : INPUT = 0,
105 : INPUT_HIDDEN_STATE = 1,
106 : OUTPUT_HIDDEN_STATE = 0,
107 : };
108 :
109 : /**
110 : * Unit: number of output neurons
111 : * IntegrateBias: Integrate bias_ih, bias_hh to bias_h
112 : * HiddenStateActivation: activation type for hidden state. default is tanh
113 : * DropOutRate: dropout rate
114 : *
115 : * */
116 : std::tuple<props::Unit, props::IntegrateBias, props::HiddenStateActivation,
117 : props::DropOutRate>
118 : rnncell_props;
119 : std::array<unsigned int, 6> wt_idx; /**< indices of the weights */
120 :
121 : /**
122 : * @brief activation function for h_t : default is tanh
123 : */
124 : ActiFunc acti_func;
125 :
126 : /**
127 : * @brief to pretect overflow
128 : */
129 : float epsilon;
130 : };
131 : } // namespace nntrainer
132 :
133 : #endif /* __cplusplus */
134 : #endif /* __RNNCELL_H__ */
|