質問編集履歴
1
コードの修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
とある論文(有料論文のため本文・図の引用は
|
1
|
+
とある論文(有料論文のため本文・図の引用は避けます)で、非時系列データを含む LSTM モデルというものがあり、それを Keras/TensorFlow のカスタムレイヤーで作成しようとしています。
|
2
2
|
構造としては、時系列データは従来の LSTM と同様に入力ゲート、出力ゲート、忘却ゲート、ブロック入力に入力されますが、非時系列データは4つのうちブロック入力には入力されず、これにより記憶する必要のない非時系列データがメモリセルに記憶されず、非時系列データを含んだデータの学習精度が向上するといったものです。
|
3
3
|
|
4
4
|
時系列データと非時系列データを入力とするため、複数の入力としてリストでそれぞれ入力するようにしています。レイヤー内での計算は、下記の Qiita 記事を参考に call メソッドでその時刻での状態 h と、次の時刻に伝える状態 h と 記憶セルの出力 c のリストを返すようにしています。実行すると、call メソッドの呼び出しでエラーが出て行き詰まっています。__init__ メソッド、もしくは build メソッドの中に誤りがあるように感じています。
|
@@ -39,7 +39,7 @@
|
|
39
39
|
```Python
|
40
40
|
import tensorflow as tf
|
41
41
|
import numpy as np
|
42
|
-
from tensorflow.keras.layers import Input, RNN, AbstractRNNCell
|
42
|
+
from tensorflow.keras.layers import Input, Dense, RNN, AbstractRNNCell
|
43
43
|
from tensorflow.python.keras import activations, constraints, initializers, regularizers
|
44
44
|
from tensorflow.python.keras import backend as K
|
45
45
|
from tensorflow.python.keras.utils import tf_utils
|
@@ -89,12 +89,12 @@
|
|
89
89
|
input_dim1 = input_shape[0][-1] # 時系列データの入力
|
90
90
|
input_dim2 = input_shape[1][-1] # 非時系列データの入力
|
91
91
|
self.kernel1 = self.add_weight(shape=(input_dim1, self.units * 4),
|
92
|
-
name='
|
92
|
+
name='kernel1',
|
93
93
|
initializer=self.kernel_initializer,
|
94
94
|
regularizer=self.kernel_regularizer,
|
95
95
|
constraint=self.kernel_constraint)
|
96
96
|
self.kernel2 = self.add_weight(shape=(input_dim2, self.units * 3),
|
97
|
-
name='
|
97
|
+
name='kernel2',
|
98
98
|
initializer=self.kernel_initializer,
|
99
99
|
regularizer=self.kernel_regularizer,
|
100
100
|
constraint=self.kernel_constraint)
|
@@ -153,9 +153,9 @@
|
|
153
153
|
x_o = K.bias_add(x_o, b_o)
|
154
154
|
|
155
155
|
f = self.recurrent_activation(x_f + K.dot(h_tm1, self.recurrent_kernel[:, :self.units])) # 忘却ゲート
|
156
|
-
u = self.activation(x_u + K.dot(h_tm1, self.recurrene_kernel[:, self.units:self.units * 2])) #
|
156
|
+
u = self.activation(x_u + K.dot(h_tm1, self.recurrene_kernel[:, self.units:self.units * 2])) # ブロック入力
|
157
|
-
i = self.recurrent_activation(x_i + K.dot(h_tm1, self.recurrent_kernel[:, self.units * 2:self.units * 3])) #
|
157
|
+
i = self.recurrent_activation(x_i + K.dot(h_tm1, self.recurrent_kernel[:, self.units * 2:self.units * 3])) # 入力ゲート
|
158
|
-
o = self.recurrent_activation(x_o + K.dot(h_tm1, self.recurrent_kernel[:, self.units * 3:self.units * 4])) #
|
158
|
+
o = self.recurrent_activation(x_o + K.dot(h_tm1, self.recurrent_kernel[:, self.units * 3:self.units * 4])) # 出力ゲート
|
159
159
|
|
160
160
|
c = f * c_tm1 + u * i
|
161
161
|
h = self.activation(c) * o
|
@@ -169,11 +169,9 @@
|
|
169
169
|
|
170
170
|
```Python
|
171
171
|
t_input = Input(shape=(30, 1))
|
172
|
-
print(t_input.shape)
|
173
172
|
n_input = Input(shape=(30, 1))
|
174
|
-
print(n_input.shape)
|
175
173
|
h = NontsLSTM(128)([t_input, n_input])
|
176
|
-
output = Dense(1, activation='linear')
|
174
|
+
output = Dense(1, activation='linear')(h)
|
177
175
|
|
178
176
|
model = Model([t_input, n_input], output)
|
179
177
|
model.compile(optimizer='adam', loss='mse', metrics='loss')
|