Principle:Tensorflow Tfjs Layer Wrapping
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Sequence_Modeling, Neural_Network_Architecture |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
Meta-layers that wrap an existing layer to modify its behavior, enabling patterns like applying a layer independently to each time step or running a recurrent layer in both directions.
Description
Wrapper layers provide compositional flexibility by decorating an inner layer with additional behavior:
- TimeDistributed: Applies a layer independently to every time step of a sequence. Useful for applying the same Dense or Conv layer to each element in a sequence.
- Bidirectional: Wraps a recurrent layer (SimpleRNN, LSTM, GRU) to process the input sequence in both forward and backward directions, then combines the outputs via concatenation, summation, averaging, or multiplication.
Usage
Use TimeDistributed when you need to apply the same transformation to each time step (e.g., a Dense layer after an LSTM with returnSequences=true). Use Bidirectional when context from both past and future is important (e.g., named entity recognition, sentiment analysis).
Theoretical Basis
Pseudo-code Logic:
# TimeDistributed applies layer to each timestep:
for t in range(sequence_length):
output[t] = inner_layer(input[t])
# Bidirectional runs forward and backward:
forward_output = rnn(input, direction='forward')
backward_output = rnn(input, direction='backward')
output = merge(forward_output, backward_output, mode='concat')