forked from Tensor-Array/Tensor-Array
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecurrent.cc
More file actions
114 lines (99 loc) · 4.48 KB
/
recurrent.cc
File metadata and controls
114 lines (99 loc) · 4.48 KB
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
/*
Copyright 2024 TensorArray-Creators
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "recurrent.hh"
namespace tensor_array
{
namespace layers
{
value::Tensor RecurrentImpl::calculate(const value::Tensor& input)
{
value::Tensor temp = this->act(add(this->layer_input(input), this->layer_hidden(this->hidden)));
this->change_hidden_value(temp);
return temp;
}
RecurrentImpl::RecurrentImpl(const value::Tensor& start, LayerFunction act) :
hidden(start),
layer_input(start.get_buffer().shape().end()[-1]),
layer_hidden(start.get_buffer().shape().end()[-1]),
act(act)
{
this->map_layer.insert(std::make_pair("input", layer_input.get_shared()));
this->map_layer.insert(std::make_pair("hidden", layer_hidden.get_shared()));
}
RecurrentImpl::RecurrentImpl(const value::Tensor& start) :
RecurrentImpl(start, NoActivation)
{
}
RecurrentImpl::RecurrentImpl(unsigned int list_dim_size, LayerFunction act) :
RecurrentImpl(value::values({ 1, list_dim_size }, 0), act)
{
}
void RecurrentImpl::change_hidden_value(const value::Tensor& hidden_value)
{
this->hidden = hidden_value;
if (this->copy_after_calculate)
this->hidden = this->hidden.clone();
}
void RecurrentImpl::set_copy_after_calculate(bool copy_after_calculate)
{
this->copy_after_calculate = copy_after_calculate;
}
value::Tensor LSTM_Impl::calculate(const value::Tensor& input)
{
value::Tensor temp_cell = add(multiply(this->gate_forget(input), this->lstm_cell), multiply(this->gate_in(input), this->gate_cell(input)));
value::Tensor temp_hidden = multiply(this->gate_out(input), tanh(temp_cell));
this->change_hidden_value(temp_hidden, temp_cell);
return temp_hidden;
}
LSTM_Impl::LSTM_Impl(const value::Tensor& start_hidden) :
lstm_cell(value::values({}, 0.f)),
gate_in(start_hidden, Sigmoid),
gate_forget(start_hidden, Sigmoid),
gate_cell(start_hidden, tanh),
gate_out(start_hidden, Sigmoid)
{
this->map_layer.insert(std::make_pair("gate_in", gate_in.get_shared()));
this->map_layer.insert(std::make_pair("gate_forget", gate_forget.get_shared()));
this->map_layer.insert(std::make_pair("gate_cell", gate_cell.get_shared()));
this->map_layer.insert(std::make_pair("gate_out", gate_out.get_shared()));
}
LSTM_Impl::LSTM_Impl(unsigned int list_dim_size) :
LSTM_Impl(value::values({ 1, list_dim_size }, 0.f))
{}
void LSTM_Impl::change_hidden_value(const value::Tensor& hidden_value, const value::Tensor& cell_value)
{
value::Tensor temp_copy = hidden_value;
if (hidden_value.has_tensor() && this->copy_after_calculate)
temp_copy = temp_copy.clone();
this->gate_forget.get()->hidden = temp_copy;
this->gate_in.get()->hidden = temp_copy;
this->gate_cell.get()->hidden = temp_copy;
this->gate_out.get()->hidden = temp_copy;
this->lstm_cell = cell_value;
if (cell_value.has_tensor() && this->copy_after_calculate)
this->lstm_cell = this->lstm_cell.clone();
}
void LSTM_Impl::set_copy_after_calculate(bool copy_after_calculate)
{
this->copy_after_calculate = copy_after_calculate;
this->gate_forget->copy_after_calculate = copy_after_calculate;
this->gate_in->copy_after_calculate = copy_after_calculate;
this->gate_cell->copy_after_calculate = copy_after_calculate;
this->gate_out->copy_after_calculate = copy_after_calculate;
}
const value::Tensor& LSTM_Impl::get_cell() const
{
return this->lstm_cell;
}
}
}