Skip to content

Commit 347d629

Browse files
author
Me
committed
Keep activations in bidirectional LSTM (fixes: #2211)
1 parent d5b7f39 commit 347d629

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

tests/test_lstm.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,31 @@ def func(x):
775775
return tf.identity(y[0], name="output"), tf.identity(y[1], name="output1")
776776
self.run_test_case(func, {"input:0": x_val}, [], ["output:0", "output1:0"], rtol=1e-05, atol=1e-06)
777777

778+
@check_tf_min_version("2.0")
779+
@skip_tf_versions("2.1", "Bug in TF 2.1")
780+
def test_keras_bilstm_recurrent_activation_is_hard_sigmoid(self):
781+
in_shape = [10, 3]
782+
x_val = np.random.uniform(size=[2, 10, 3]).astype(np.float32)
783+
784+
model_in = tf.keras.layers.Input(tuple(in_shape), batch_size=2)
785+
x = tf.keras.layers.Bidirectional(
786+
tf.keras.layers.LSTM(
787+
units=5,
788+
return_sequences=True,
789+
return_state=True,
790+
kernel_initializer=tf.random_uniform_initializer(0.0, 1.0, seed=42),
791+
recurrent_initializer=tf.random_uniform_initializer(0.0, 1.0, seed=44),
792+
bias_initializer=tf.random_uniform_initializer(0.0, 1.0, seed=43),
793+
recurrent_activation="hard_sigmoid",
794+
)
795+
)(model_in)
796+
model = tf.keras.models.Model(inputs=model_in, outputs=x)
797+
798+
def func(x):
799+
y = model(x)
800+
return tf.identity(y[0], name="output"), tf.identity(y[1], name="output1")
801+
self.run_test_case(func, {"input:0": x_val}, [], ["output:0", "output1:0"], rtol=1e-05, atol=1e-06)
802+
778803
@check_tf_min_version("2.0")
779804
@skip_tfjs("TFJS converts model incorrectly")
780805
def test_keras_lstm_sigmoid_dropout(self):

tf2onnx/rewriter/bilstm_rewriter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ def process_bilstm(g, bi_lstms):
5454
if len(lstm_fw.inputs) > 4:
5555
lstm_inputs.extend([lstm_fw.input[4], h_node.output[0], c_node.output[0]])
5656

57-
attr = {"direction": "bidirectional"}
57+
attr = {
58+
"direction": "bidirectional",
59+
"activations": lstm_bw.get_attr_value("activations") + lstm_fw.get_attr_value("activations"),
60+
}
5861
for name in rnn_utils.onnx_rnn_attr_mapping[rnn_utils.ONNX_RNN_TYPE.LSTM]:
5962
attr_val = lstm_fw.get_attr_value(name)
6063
if attr_val:

0 commit comments

Comments
 (0)