Keras: compile() should not require arguments when not training

Created on 5 May 2016  ·  3Comments  ·  Source: keras-team/keras

edit: A less obscure situation: after loading a model from disk, it's necessary to call compile() with optimizer and loss, even if you're only using it for prediction. A more obscure situation follows.

I'm using the new functional API to predict intermediate output from my network. First I set up the network and compile the model:

inputs = Input(shape=(1, img_rows, img_cols))
x = Convolution2D(32, 3, 3, activation='relu')(x)
...
x = Flatten()(x)
e = Dense(512, name='e', activation='relu')(x)
x = Dense(nb_classes, activation='softmax')(e)

model = Model(input=inputs, output=x)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

Then I train it. After it's done training, I want to evaluate the output at e.

Previously with the Sequential model I did something like this:

def matching_layers(model, name):
    return [l for l in model.layers if l.name == name]
get_encoding = K.function([model.layers[0].input, K.learning_phase()],
                          [matching_layers(model, 'e')[0].output])
encoded = get_encoding([data, 1])[0]

That worked correctly, but I usually have to split the data into chunks and then recombine it because I run out of GPU memory otherwise.

Now I can do something much simpler:

emodel = Model(input=inputs, output=e)
emodel.compile(optimizer='rmsprop', loss='categorical_crossentropy')
encoded = emodel.predict(data)

Awesome. It's less code, and makes more sense. And for some reason I don't have to split up the data (not sure what's different behind the scenes...)

But why do I need to specify optimizer and loss if I'm not training the model?

Most helpful comment

Good point, I have allowed the use of predict without compilation. Note that test or evaluate will still need compilation, since they need to know about metrics, sample weighting, and so on.

All 3 comments

Awesome. It's less code, and makes more sense. And for some reason I don't have to split up the data (not sure what's different behind the scenes...)

It splits the data into batches, see declaration: predict(self, x, batch_size=32, verbose=0)

But why do I need to specify optimizer and loss if I'm not training the model?

No clue, but you're still able to do the K.function stuff:

emodel = Model(input=inputs, output=e)
inputs = emodel.inputs
if emodel.uses_learning_phase:
   inputs.append(K.learning_phase())
predict = K.function(inputs, emodel.outputs, updates=emodel.state_updates)
result = predict([data, 0])

Good point, I have allowed the use of predict without compilation. Note that test or evaluate will still need compilation, since they need to know about metrics, sample weighting, and so on.

"Sequential object has no attribute predict". Is this only for Model object? Can we get it for Sequential as well?
Edit: Scratch that. Just needed to download from github and not the pip distrubtion

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Imorton-zd picture Imorton-zd  ·  3Comments

oweingrod picture oweingrod  ·  3Comments

amityaffliction picture amityaffliction  ·  3Comments

fredtcaroli picture fredtcaroli  ·  3Comments

nryant picture nryant  ·  3Comments