About RNN parameter

Hi, I have a question regarding the parameter “units” of SimpleRNN.

In the following function, what does the value “40” do?

tf.keras.layers.SimpleRNN(40, return_sequences=True)

Hi @heeseong_kim ,
it’s the number of units in this layer, in this case you have 40 SimpleRNN units that can be stacked with another layer, because you put return_sequences=True. You could do something like this:

tf.keras.layers.SimpleRNN(40, return_sequences=True)
tf.keras.layers.SimpleRNN(40, return_sequences=False)

in this case, the 2nd layer have a 40-dimensional sequence input, coming from the 1st layer.

Best

A little more expansive description is available at the RNN with Keras Guide in the section on Outputs and States, which reads in part…

By default, the output of a RNN layer contains a single vector per sample. This vector is the RNN cell output corresponding to the last timestep, containing information about the entire input sequence. The shape of this output is (batch_size, units) where units corresponds to the units argument passed to the layer’s constructor.

https://www.tensorflow.org/guide/keras/rnn

Could you elaborate on how the second line of your code fragment knows anything about the shape of the output of the first?

Yeah @ai_curious,
We are using the Sequential API, meaning the output of the 1st row simple_RNN is the input of the 2nd row simple_RNN. Take a look at this piece of model summary:

The classic RNN model has of a single hidden RNN layer, usually followed by a feedforward output layer. A Stacked RNN, like this one, is an extension to this model that has more hidden RNN layers where each layer contains multiple memory cells.

Best

1 Like

Thanks for the clarification. From the 2-line code fragment in the previous post, it didn’t seem to be using syntax I recognized as either Functional or Sequential.

You’re welcome,
and sorry for the misunderstanding: I’d skipped one comma and that could create some confusion.