-
Notifications
You must be signed in to change notification settings - Fork 8
/
newLSTM.py
177 lines (144 loc) · 6.76 KB
/
newLSTM.py
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import tensorflow as tf
rnn=tf.nn.rnn_cell
class base_LSTMCell(rnn.BasicLSTMCell):
def __call__(self,inputs,state,scope=None):
"""Long short-term memory cell (LSTM)."""
with tf.variable_scope(scope or type(self).__name__): # "BasicLSTMCell"
# Parameters of gates are concatenated into one multiply for efficiency.
if self._state_is_tuple:
c, h = state
else:
c, h = tf.split(1, 2, state)
concat = tf.layers.dense(tf.concat([inputs, h],axis=1), 4 * self._num_units)
# i = input_gate, j = new_input, f = forget_gate, o = output_gate
i, j, f, o = tf.split(concat, 4, 1)
new_c = (c * tf.sigmoid(f + self._forget_bias) + tf.sigmoid(i) *
self._activation(j))
new_h = self._activation(new_c) * tf.sigmoid(o)
if self._state_is_tuple:
new_state = rnn.LSTMStateTuple(new_c, new_h)
else:
new_state = tf.concat(1, [new_c, new_h])
return new_h, new_state
class MI_LSTMCell(rnn.BasicLSTMCell):
"""
Multi-Input LSTM proposed in the paper, Stock Price Prediction Using Attention-based Multi-Input LSTM.
"""
def __init__(self,
num_units,
num_inputs,
forget_bias=1.0,
state_is_tuple=True,
activation=None,
reuse=None,
name=None,
dtype=None,
**kwargs):
"""
Initialize the basic LSTM cell.
args:
num_inputs: MI-LSTM의 입력의 개수.
이 파라미터에 따라 입력 게이트의 어텐션 레이어를 설정.
최소 1개이상.
1개일 경우, 어텐션 레이어를 제외하고 기본 LSTM과 동일.
"""
super(MI_LSTMCell,self).__init__(num_units,
forget_bias=1.0,
state_is_tuple=True,
activation=None,
reuse=None,
name=None,
dtype=None,
**kwargs)
if(type(num_inputs) is not int):
raise ValueError("num_inputs should be integer")
if(num_inputs < 1):
raise ValueError("num_inputs should not be less than 0")
self.num_inputs = num_inputs
self.alpha_weight=self.add_variable('alpha_weight',shape=[self._num_units,self._num_units])
self.alpha_bias=[]
for i in range(self.num_inputs):
self.alpha_bias.append(self.add_variable('alpha_bias'+str(i),shape=[1],initializer=tf.zeros_initializer()))
def __call__(self,inputs,state,scope=None):
"""Long short-term memory cell (LSTM)."""
with tf.variable_scope(scope or type(self).__name__): # "BasicLSTMCell"
# Parameters of gates are concatenated into one multiply for efficiency.
if self._state_is_tuple:
c, h = state
else:
c, h = tf.split(1, 2, state)
inputs_list = tf.split(inputs,self.num_inputs,1)
concat = tf.layers.dense(tf.concat([inputs_list[0], h],axis=1), (3+self.num_inputs) * self._num_units)
# 0 = forget_gate, 1 = output_gate, 2= main_new_input, 3 = main_input_gate, 4~ = input_gate_for_auxiliary
main_list = tf.split(concat, 3+self.num_inputs, 1)
#new_input_gate= list of all new_input.
new_input_gate=[tf.tanh(main_list[2])]
#linear layer for auxiliary inputs.
for i in range(1,self.num_inputs):
new_input_gate.append(tf.layers.dense(tf.concat([inputs_list[i], h],axis=1),self._num_units,activation=tf.tanh))
#making list of l. l = sigmoid(input_gate) * tanh(new_input)
new_l=[]
for i,new_input in enumerate(new_input_gate,3):
new_l.append(tf.sigmoid(main_list[i]) * new_input)
#making list of u.
u=[]
for i,l in enumerate(new_l):
#temp = transpos(l) X W X Cell_State.
temp1=tf.matmul(l,self.alpha_weight)
temp1=tf.expand_dims(temp1,1)
temp2=tf.matmul(temp1,tf.expand_dims(c,2))
u.append(tf.tanh(tf.squeeze(temp2+self.alpha_bias[i],axis=2)))
#making list of alpha.
alpha=tf.nn.softmax(u,axis=0)
#making L.
L=[]
for i,l in enumerate(new_l):
L.append(alpha[i]*l)
L=tf.reduce_sum(L,axis=0)
#new state = c(t-1) * f + L. new h = tanh(c) + sigmoid(o)
new_c = (c * tf.sigmoid(main_list[0] + self._forget_bias)+L)
new_h = self._activation(new_c) * tf.sigmoid(main_list[1])
if self._state_is_tuple:
new_state = rnn.LSTMStateTuple(new_c, new_h)
else:
new_state = tf.concat(1, [new_c, new_h])
return new_h, new_state
class Attention_Layer():
"""
어텐션 레이어.
(None, TimeWindow, hidden_unit_size) shape의 LSTM 출력을 입력으로 받아 (None, 1, hidden_unit_size)의 텐서 출력.
"""
def __init__(
self,
timewindow_size,
input_hidden_unit_size,
attention_size=None):
"""
Setting parameter for attention layer.
args:
timewindow_size = time window size of previous lstm layer.
input_hidden_unit_size = hidden unit number of previous lstm layer.
attention_size = size of this attention.
default = input_hidden_unit_size.
"""
if(attention_size is None):
attention_size=input_hidden_unit_size
self.o_size=attention_size
self.h_size=input_hidden_unit_size
self.t_size=timewindow_size
self.beta_weight=tf.Variable(tf.random_normal([self.h_size,self.o_size]), name='beta_weight')
self.beta_bias=tf.Variable(tf.zeros([self.o_size]),name='beta_bias')
self.v=tf.Variable(tf.random_normal([self.o_size,1]),name='beta_v')
def __call__(self,inputs):
"""
producing output with actual inputs.
shape of output will be (batch_size, 1, input_hidden_unit_size).
"""
#temp = tanh(Y X W + b) ->shape of result = (-1, self.o_size)
temp=tf.matmul(tf.reshape(inputs,[-1,self.h_size]),self.beta_weight)
temp=tf.tanh(temp+self.beta_bias)
#j=temp X v
j=tf.reshape(tf.matmul(temp,self.v),[-1,self.t_size,1])
beta=tf.nn.softmax(j)
output=beta*inputs
return output