user1384636
user1384636

Reputation: 501

Lasagne/Theano, problems loading pickled model

I think I'm losing my mind at this point.

I'm using Lasagne for a small convolutional neural network. It trains perfectly, I can compute the error on training and validation set as well, but I cannot save the trained model on the disk. Better, I can save it and load it, but I cannot use it to predict for new data.

This is what I do after training

model = {'network': network, 'params': get_all_params(network), 'params_values': get_all_param_values(network)}
pickle.dump(model, open('models/model_1.pkl', 'wb'), protocol=pickle.HIGHEST_PROTOCOL)

And this is what I do to load the model

with open('models/model.pkl', 'rb') as pickle_file:
    model = pickle.load(pickle_file)

network = model['network']
values = model['params_values']

set_all_param_values(network, values)

T_input = T.tensor4('input', dtype='float32')
T_target = T.ivector('target')

predictions = get_output(network, deterministic=True)

loss = (cross_entropy(predictions, T_target)).mean()

acc = T.mean(T.eq(T.argmax(predictions, axis=1), T_target), dtype=config.floatX)

test_fn = function([T_input, T_target], [loss, acc])

I cannot even pass the real numpy input, that I get this error

theano.compile.function_module.UnusedInputError: theano.function was asked to create a 
function computing outputs given certain inputs, but the provided input variable at index 0 
is not part of the computational graph needed to compute the outputs: input.
To make this error into a warning, you can pass the parameter   
on_unused_input='warn' to theano.function. To disable it completely, use 
on_unused_input='ignore'.

I tried to set the parameter on_unused_input='warn' then, and this is the result

theano.gof.fg.MissingInputError: An input of the graph, used to compute (..)   
was not provided and not given a value.Use the Theano flag 
exception_verbosity='high',for more information on this error.

Upvotes: 2

Views: 187

Answers (1)

Shubham Bansal
Shubham Bansal

Reputation: 388

The problem is that your T_input is not tied to the input layer and hence theano can't compile it

T_input = lasagne.layers.get_all_layers(network)[0].input_var

Upvotes: 0

Related Questions