Keras, Lstm, feeding each time step Input with previous time step Output

Viewed 100

Here is a simple RNN I made:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import utils
from tensorflow.keras.models import Model
import numpy as np



n_units = 100

inputs = keras.Input(shape=(2,5,)) # 2 steps, input dim = 5

lstm_layer = layers.LSTM(n_units, return_sequences=True, return_state=True)

outputs, _, _ = lstm_layer(inputs)

dense_layer = layers.Dense(5, activation='softmax')

outputs = dense_layer(outputs)

model = Model([inputs], outputs)

I want the first time step Input to be the vector [0,0,0,0,0], But I want the 2nd time step Input to be the 1st step output.

The only thing I know how to do for now is to feed the network with an array with a fixed input for each time step (here a vector of 0s for both time steps):

data = np.array([ [ [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.] ] ])

model.predict(data)

Any idea ?

(here is a Google Collab link)

0 Answers
Related