<jupyter_start><jupyter_text><jupyter_code>from __future__ import print_function, division
from builtins import range, input
from keras.models import Model
from keras.layers import Input, LSTM, GRU, Bidirectional
import numpy as np
import matplotlib.pyplot as plt
T = 8
D = 2
M = 3
X = np.random.randn(1,T,D)
X
# return_sequence = True
input_ = Input(shape=(T,D))
rnn = Bidirectional(LSTM(M, return_state=True, return_sequences=True))
x = rnn(input_)

model = Model(inputs= input_, outputs=x)
o,h1,c1,h2,c2 = model.predict(X)
print("o:",o)
print("o shape:",o.shape)
print("h1:",h1)
print("c1:",c1)
print("h2:",h2)
print("c2:",c2)

"""
> output[-1][:3] == h1
> output[0][3:] == h2
"""
# return_sequence = False
input_1 = Input(shape=(T,D))
rnn1 = Bidirectional(LSTM(M, return_state=True, return_sequences=False))
x1 = rnn1(input_1)

model1 = Model(inputs= input_1, outputs=x1)
o1,h11,c11,h21,c21 = model1.predict(X)
print("o:",o1)
print("o shape:",o1.shape)
print("h1:",h11)
print("c1:",c11)
print("h2:",h21)
print("c2:",c21)

"""
> output[0][:3] == h1
> output[0][3:] == h2
"""<jupyter_output>WARNING:tensorflow:5 out of the last 8 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7efd891e4b00> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
o: [[0.23665865 0.39235148 0.08905505 0.03871855 0.24557176 0.02295763]]
o shape: (1, 6)
h1: [[0.23665865 0.39235148 0.08905505]]
c1: [[0.316394  0.5862307 0.1313714]]
h2: [[0.03871855 0.24557176 0.02295763]]
c2: [[0.07869172 0.40682688 0.05008529]]
{'fraction_non_alphanumeric': 0.1530317613089509, 'fraction_numerical': 0.11549566891241578, 'mean_word_length': 6.11986301369863, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '467761', 'license': 'MIT', 'n_tokens_mistral': 917, 'n_tokens_neox': 793, 'n_words': 203, 'path': 'simple_BiLSTM.ipynb', 'repo_name': 'dpk-a7/Deep-learning', 'size': '2078'}