LSTM is one such network. In this article, we saw how different variants of the LSTM algorithm can be used to solve one-to-one and many-to-one sequence problems. This is the first part of the article. In the second part, we will see how to solve one-to-many and many-to-many sequence problems.
In Deep Learning, Recurrent Neural Networks (RNN) are a family of neural networks that excels in learning from sequential data. A class of RNN that has found practical applications is Long Short-Term Memory (LSTM) because it is robust against the problems of long-term dependency.
What Are Many-to-Many Sequence Problems? Many-to-Many sequence learning can be used for machine translation where the input sequence is in some language, and the output sequence is in some other language.
There are three different gates in an LSTM cell: a forget gate, an input gate, and an output gate.
So:
One-to-one: you could use a Dense
layer as you are not processing sequences:
model.add(Dense(output_size, input_shape=input_shape))
One-to-many: this option is not supported well as chaining models is not very easy in Keras
, so the following version is the easiest one:
model.add(RepeatVector(number_of_times, input_shape=input_shape))
model.add(LSTM(output_size, return_sequences=True))
Many-to-one: actually, your code snippet is (almost) an example of this approach:
model = Sequential()
model.add(LSTM(1, input_shape=(timesteps, data_dim)))
Many-to-many: This is the easiest snippet when the length of the input and output matches the number of recurrent steps:
model = Sequential()
model.add(LSTM(1, input_shape=(timesteps, data_dim), return_sequences=True))
Many-to-many when number of steps differ from input/output length: this is freaky hard in Keras. There are no easy code snippets to code that.
EDIT: Ad 5
In one of my recent applications, we implemented something which might be similar to many-to-many from the 4th image. In case you want to have a network with the following architecture (when an input is longer than the output):
O O O
| | |
O O O O O O
| | | | | |
O O O O O O
You could achieve this in the following manner:
model = Sequential()
model.add(LSTM(1, input_shape=(timesteps, data_dim), return_sequences=True))
model.add(Lambda(lambda x: x[:, -N:, :])) #Select last N from output
Where N
is the number of last steps you want to cover (on image N = 3
).
From this point getting to:
O O O
| | |
O O O O O O
| | |
O O O
is as simple as artificial padding sequence of length N
using e.g. with 0
vectors, in order to adjust it to an appropriate size.
Great Answer by @Marcin Możejko
I would add the following to NR.5 (many to many with different in/out length):
A) as Vanilla LSTM
model = Sequential()
model.add(LSTM(N_BLOCKS, input_shape=(N_INPUTS, N_FEATURES)))
model.add(Dense(N_OUTPUTS))
B) as Encoder-Decoder LSTM
model.add(LSTM(N_BLOCKS, input_shape=(N_INPUTS, N_FEATURES))
model.add(RepeatVector(N_OUTPUTS))
model.add(LSTM(N_BLOCKS, return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.add(Activation('linear'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With