Skip to content

Instantly share code, notes, and snippets.

@fchollet
Created May 28, 2015 17:34
Show Gist options
  • Save fchollet/fcd51ecae50f00c91e69 to your computer and use it in GitHub Desktop.
Save fchollet/fcd51ecae50f00c91e69 to your computer and use it in GitHub Desktop.
Defining a Theano function to output intermediate transformations in a Keras model
import theano
from keras.models import Sequential
from keras.layers.core import Dense, Activation
X_train, y_train = ... # load some training data
X_batch = ... # a batch of test data
# this is your initial model
model = Sequential()
model.add(Dense(20, 64))
model.add(Activation('tanh'))
model.add(Dense(64, 1))
# we train it
model.compile(loss='mse', optimizer='sgd')
model.fit(X_train, y_train, nb_epoch=20, batch_size=16)
# we define a function that returns the activation of layer 1 (after the tanh)
get_layer_1 = theano.function([model.layers[0].input], model.layers[1].output(train=False), allow_input_downcast=True)
transformed_data = get_layer_1(X_batch) # activation of layer 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment