Training LSTM is quicker than GRU, but should not be?

Viewed 350

I have implemented a simple LSTM as well as GRU network for time-series forecast:

def LSTM1(T0, tau0, tau1, optimizer):
    model = Sequential()
    model.add(Input(shape=(T0,tau0), dtype="float32", name="Input"))
    model.add(LSTM(units=tau1, activation="tanh", recurrent_activation="tanh", name="LSTM1"))
    model.add(Dense(units=1, activation="exponential", name="Output"))
    model.compile(optimizer=optimizer, loss="mse")
    return model

def GRU1(T0, tau0, tau1, optimizer):
    model = Sequential()
    model.add(Input(shape=(T0,tau0), dtype="float32", name="Input"))
    model.add(GRU(units=tau1, activation="tanh", recurrent_activation="tanh", reset_after=False, name="GRU1"))
    model.add(Dense(units=1, activation="exponential", name="Output"))
    model.compile(optimizer=optimizer, loss="mse")
return model

The LSTM model than has obviously more parameters than the GRU model:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
LSTM1 (LSTM)                 (None, 5)                 180       
_________________________________________________________________
Output (Dense)               (None, 1)                 6         
=================================================================
Total params: 186
Trainable params: 186
Non-trainable params: 0
_________________________________________________________________

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
GRU1 (GRU)                   (None, 5)                 135      
_________________________________________________________________
Output (Dense)               (None, 1)                 6         
=================================================================
Total params: 141
Trainable params: 141
Non-trainable params: 0
_________________________________________________________________

I therefore would expect that training the GRU model would take less time.

T0        = 10     # lookback period
tau0      = 3      # dimension of x_t 
tau1      = 5      # dimension of the outputs first RNN layer
optimizer = "Adam"

# Create model
model_gru1 = GRU1(T0, tau0, tau1, optimizer)
model_lstm1 = LSTM1(T0, tau0, tau1, optimizer)

However, taking the following training data:

x_train = np.random.rand(100,T0,tau0)
x_valid = np.random.rand(100,T0,tau0)
y_train = np.random.rand(100)
y_valid = np.random.rand(100)

and training my models

# Train LSTM1 model
tf.random.set_seed(32)

start = timer()
model_lstm1.fit(x=x_train, y=y_train, 
          validation_data=(x_valid,y_valid), 
          verbose=1, 
          batch_size=10, epochs=500
         )
end = timer()
time_lstm1 = round(end-start,0)


# Train GRU1 model
tf.random.set_seed(32)

start = timer()
model_gru1.fit(x=x_train, y=y_train, 
          validation_data=(x_valid,y_valid), 
          verbose=1, 
          batch_size=10, epochs=500
         )
end = timer()
time_gru1 = round(end-start,0)

the LSTM needs less time:

print("training time GRU1 {} vs. training time LSTM1 {}".format(time_gru1,time_lstm1))

training time GRU1 80.0 vs. training time LSTM1 62.0

I use Tensorflow version 2.0.0 on a CPU.

Any ideas?

0 Answers
Related