Skip to content

Instantly share code, notes, and snippets.

@andrewssobral
Forked from fchollet/keras_intermediate.py
Created June 9, 2016 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewssobral/f29c3656842533d5ffe27ef633e8ceb1 to your computer and use it in GitHub Desktop.
Save andrewssobral/f29c3656842533d5ffe27ef633e8ceb1 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