Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / builders / ie_lstm_sequence_layer.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <builders/ie_lstm_sequence_layer.hpp>
6 #include <ie_cnn_layer_builder.h>
7
8 #include <vector>
9 #include <string>
10
11 using namespace InferenceEngine;
12
13 Builder::LSTMSequenceLayer::LSTMSequenceLayer(const std::string& name): LayerDecorator("LSTMSequence", name) {
14     getLayer()->getOutputPorts().resize(3);
15     getLayer()->getInputPorts().resize(7);
16     getLayer()->getInputPorts()[1].setParameter("type", "weights");
17     getLayer()->getInputPorts()[2].setParameter("type", "biases");
18     getLayer()->getInputPorts()[3].setParameter("type", "optional");
19     getLayer()->getInputPorts()[6].setParameter("type", "weights");
20 }
21
22 Builder::LSTMSequenceLayer::LSTMSequenceLayer(const Layer::Ptr& layer): LayerDecorator(layer) {
23     checkType("LSTMSequence");
24 }
25
26 Builder::LSTMSequenceLayer::LSTMSequenceLayer(const Layer::CPtr& layer): LayerDecorator(layer) {
27     checkType("LSTMSequence");
28 }
29
30 Builder::LSTMSequenceLayer& Builder::LSTMSequenceLayer::setName(const std::string& name) {
31     getLayer()->setName(name);
32     return *this;
33 }
34
35 const std::vector<Port>& Builder::LSTMSequenceLayer::getInputPorts() const {
36     return getLayer()->getInputPorts();
37 }
38
39 Builder::LSTMSequenceLayer& Builder::LSTMSequenceLayer::setInputPorts(const std::vector<Port>& ports) {
40     getLayer()->getInputPorts() = ports;
41     return *this;
42 }
43
44 const std::vector<Port>& Builder::LSTMSequenceLayer::getOutputPorts() const {
45     return getLayer()->getOutputPorts();
46 }
47
48 Builder::LSTMSequenceLayer& Builder::LSTMSequenceLayer::setOutputPorts(const std::vector<Port>& ports) {
49     getLayer()->getOutputPorts() = ports;
50     return *this;
51 }
52 int Builder::LSTMSequenceLayer::getHiddenSize() const {
53     return getLayer()->getParameters().at("hidden_size");
54 }
55 Builder::LSTMSequenceLayer& Builder::LSTMSequenceLayer::setHiddenSize(int size) {
56     getLayer()->getParameters()["hidden_size"] = size;
57     return *this;
58 }
59 bool Builder::LSTMSequenceLayer::getSequenceDim() const {
60     return getLayer()->getParameters().at("sequence_dim");
61 }
62 Builder::LSTMSequenceLayer& Builder::LSTMSequenceLayer::setSqquenceDim(bool flag) {
63     getLayer()->getParameters()["sequence_dim"] = flag;
64     return *this;
65 }
66 const std::vector<std::string>& Builder::LSTMSequenceLayer::getActivations() const {
67     return getLayer()->getParameters().at("activations");
68 }
69 Builder::LSTMSequenceLayer& Builder::LSTMSequenceLayer::setActivations(const std::vector<std::string>& activations) {
70     getLayer()->getParameters()["activations"] = activations;
71     return *this;
72 }
73 const std::vector<float>& Builder::LSTMSequenceLayer::getActivationsAlpha() const {
74     return getLayer()->getParameters().at("activations_alpha");
75 }
76 Builder::LSTMSequenceLayer& Builder::LSTMSequenceLayer::setActivationsAlpha(const std::vector<float>& activations) {
77     getLayer()->getParameters()["activations_alpha"] = activations;
78     return *this;
79 }
80 const std::vector<float>& Builder::LSTMSequenceLayer::getActivationsBeta() const {
81     return getLayer()->getParameters().at("activations_beta");
82 }
83 Builder::LSTMSequenceLayer& Builder::LSTMSequenceLayer::setActivationsBeta(const std::vector<float>& activations) {
84     getLayer()->getParameters()["activations_beta"] = activations;
85     return *this;
86 }
87 float Builder::LSTMSequenceLayer::getClip() const {
88     return getLayer()->getParameters().at("clip");
89 }
90 Builder::LSTMSequenceLayer& Builder::LSTMSequenceLayer::setClip(float clip) {
91     getLayer()->getParameters()["clip"] = clip;
92     return *this;
93 }
94
95 bool Builder::LSTMSequenceLayer::getInputForget() const {
96     return getLayer()->getParameters().at("input_forget");
97 }
98 Builder::LSTMSequenceLayer& Builder::LSTMSequenceLayer::setInputForget(bool flag) {
99     getLayer()->getParameters()["input_forget"] = flag;
100     return *this;
101 }
102 const std::string& Builder::LSTMSequenceLayer::getDirection() const {
103     return getLayer()->getParameters().at("direction");
104 }
105 Builder::LSTMSequenceLayer& Builder::LSTMSequenceLayer::setDirection(const std::string& direction) {
106     getLayer()->getParameters()["direction"] = direction;
107     return *this;
108 }
109
110 REG_CONVERTER_FOR(LSTMSequence, [](const CNNLayerPtr& cnnLayer, Builder::Layer& layer) {
111     layer.getParameters()["hidden_size"] = cnnLayer->GetParamAsInt("hidden_size");
112     layer.getParameters()["sequence_dim"] = cnnLayer->GetParamsAsBool("sequence_dim", true);
113     std::vector<std::string> activations;
114     std::istringstream stream(cnnLayer->GetParamAsString("activations"));
115     std::string str;
116     while (getline(stream, str, ',')) {
117          activations.push_back(str);
118     }
119     layer.getParameters()["activations"] = activations;
120     layer.getParameters()["activations_alpha"] = cnnLayer->GetParamAsFloats("activations_alpha");
121     layer.getParameters()["activations_beta"] = cnnLayer->GetParamAsFloats("activations_beta");
122     layer.getParameters()["clip"] = cnnLayer->GetParamAsFloat("clip");
123     layer.getParameters()["input_forget"] = cnnLayer->GetParamsAsBool("input_forget", true);
124     layer.getParameters()["direction"] = cnnLayer->GetParamAsString("direction", "");
125 });
126
127