Skip to content

Instantly share code, notes, and snippets.

@dipspb
Forked from fchollet/new_stacked_rnns.py
Created October 2, 2017 16:59
Show Gist options
  • Select an option

  • Save dipspb/764995abe277d1321bdd7f0c0072a2d2 to your computer and use it in GitHub Desktop.

Select an option

Save dipspb/764995abe277d1321bdd7f0c0072a2d2 to your computer and use it in GitHub Desktop.

Revisions

  1. @fchollet fchollet revised this gist Sep 22, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion new_stacked_rnns.py
    Original file line number Diff line number Diff line change
    @@ -7,10 +7,11 @@
    batch_size = 128
    output_dim = 64

    print('Classic stacked LSTM: 35s/epoch on CPU')
    # Test data.
    x_np = np.random.random((samples, timesteps, input_dim))
    y_np = np.random.random((samples, output_dim))

    print('Classic stacked LSTM: 35s/epoch on CPU')
    inputs = keras.Input((timesteps, input_dim))
    x = keras.layers.LSTM(output_dim, return_sequences=True)(inputs)
    x = keras.layers.LSTM(output_dim, return_sequences=True)(x)
  2. @fchollet fchollet created this gist Sep 21, 2017.
    35 changes: 35 additions & 0 deletions new_stacked_rnns.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import keras
    import numpy as np

    timesteps = 60
    input_dim = 64
    samples = 10000
    batch_size = 128
    output_dim = 64

    print('Classic stacked LSTM: 35s/epoch on CPU')
    x_np = np.random.random((samples, timesteps, input_dim))
    y_np = np.random.random((samples, output_dim))

    inputs = keras.Input((timesteps, input_dim))
    x = keras.layers.LSTM(output_dim, return_sequences=True)(inputs)
    x = keras.layers.LSTM(output_dim, return_sequences=True)(x)
    x = keras.layers.LSTM(output_dim)(x)

    classic_model = keras.models.Model(inputs, x)
    classic_model.compile(optimizer='rmsprop', loss='mse')
    classic_model.fit(x_np, y_np, batch_size=batch_size, epochs=4)

    print('New stacked LSTM: 30s/epoch on CPU (15pct faster)')
    cells = [
    keras.layers.LSTMCell(output_dim),
    keras.layers.LSTMCell(output_dim),
    keras.layers.LSTMCell(output_dim),
    ]

    inputs = keras.Input((timesteps, input_dim))
    x = keras.layers.RNN(cells)(inputs)

    new_model = keras.models.Model(inputs, x)
    new_model.compile(optimizer='rmsprop', loss='mse')
    new_model.fit(x_np, y_np, batch_size=batch_size, epochs=4)