0

What is the meaning of two values separated only by parenthesis on the RHS of = in Keras?

LSTM_layer = LSTM(units=256)(embedding)

Full code:

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, LSTM, Dense, Embedding
from tensorflow.keras import optimizers

sentence_input = Input(shape=(None,))
embedding =  Embedding(input_dim = len(tokenizer.word_index)+1, output_dim = 100)(sentence_input)
LSTM_layer = LSTM(units=256)(embedding)
output_dense = Dense(1, activation='sigmoid')(LSTM_layer)
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Sherum
  • 63
  • 7

1 Answers1

1

It's just ordinary call syntax without a temporary variable. LSTM_Layer = LSTM(units=256)(embedding) is equivalent to

# assuming t isn't already defined
t = LSTM(units=256)
LSTM_Layer = t(embedding)
del t
chepner
  • 497,756
  • 71
  • 530
  • 681
  • This is first time I've seen this syntax and combined with the API for LSTM not showing a __call__ method it was still confusing. I found a reference here: http://man.hubwiz.com/docset/TensorFlow.docset/Contents/Resources/Documents/api_docs/python/tf/keras/layers/LSTM.html. Now this makes sense. – Sherum Jun 15 '22 at 18:27
  • `__call__` is inherited from the base class `keras.layers.rnn.base_rnn.RNN`. – chepner Jun 15 '22 at 18:30