Rafael
Rafael

Reputation: 671

Get Ouput Of A Keras Model/Layer

My Keras model is the babi_rnn example in the Keras repository.

I want to get the output (in words) of the model on a dataset.

I tried:

layer = model.layers[-1] # For the last layer
    f = K.function([model.get_input(train=False)], [layer.get_output(train=False)])
    print(f([x])[0])  # your activation tensor

but I get the error:

AttributeError: 'Sequential' object has no attribute 'get_input'

How can I simply get the output of the model or layer upon being fed by an input?

That is, I need

    # I supply the X list and want to get the Y list.

    Y = Model(X) # X and Y are both lists. Model.Layer[Index] can also be a use case.

    # The responses are the set of label probabilities for each word in the vocabulary.

So that I could do: for x, y in zip(X,Y): print(x,y) for seeing what is the model actually doing.

I think this should have been the simplest use case, but it looks troublesome to implement.

Any help would be greatly appreciated. Thanks.

Upvotes: 1

Views: 1935

Answers (1)

indraforyou
indraforyou

Reputation: 9099

You can simply use model.predict to get Y the predict function internally calls _make_predict_function() which does what you are trying to do.

But your model is trained to map certain type of input to certain type of output... so you need to take care of those while using the predict function and also interpreting the same. In this example this conversion is done in vectorize_stories() so try and understand what it is doing.

In this case to get the word predicted all you need to do after training the model is:

Y_pred = model.predict([tX, tXq])
for pred in Y_pred:
    print (vocab[pred.argmax()-1])

Note again tX is the vectorized test story tXq is the vectorized test query and Y_pred is your vectorized predicted answer by the model.

Upvotes: 2

Related Questions