Skip to content

Instantly share code, notes, and snippets.

@Smerity
Last active September 20, 2015 01:22
Show Gist options
  • Save Smerity/39ffcc67f82d42faef84 to your computer and use it in GitHub Desktop.
Save Smerity/39ffcc67f82d42faef84 to your computer and use it in GitHub Desktop.
Test the difference between Dense and TimeDistributedDense in Keras
from __future__ import print_function
import numpy as np
np.random.seed(1337)
import sys
from keras.utils.test_utils import get_test_data
from keras.models import Sequential
from keras.layers.core import Dense, TimeDistributedDense
from keras.layers.recurrent import GRU
(X_train, y_train), (X_test, y_test) = get_test_data(nb_train=1000, nb_test=200, input_shape=(5, 10), output_shape=(5, 10), classification=False)
print('X_train:', X_train.shape)
print('X_test:', X_test.shape)
print('y_train:', y_train.shape)
print('y_test:', y_test.shape)
DLAYER = Dense if sys.argv[1] == 'DENSE' else TimeDistributedDense
dee = DLAYER(X_train.shape[-1] + 1, y_train.shape[-1])
model = Sequential()
model.add(GRU(X_train.shape[-1], X_train.shape[-1] + 1, return_sequences=True))
model.add(dee)
model.compile(loss='hinge', optimizer='rmsprop')
history = model.fit(X_train, y_train, nb_epoch=5, batch_size=16, validation_data=(X_test, y_test), verbose=2)
print(dee.get_weights())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment