pythonneural-networkconv-neural-networklasagnenolearn

How to get values of neurons on layer? Python-nolearn


I have neural network created with using nolearn library.

net = NeuralNet(
layers=[
    ('input', layers.InputLayer),
    ('conv1', layers.Conv2DLayer),
    ('pool1', layers.MaxPool2DLayer),
    ('dropout1', layers.DropoutLayer), 
    ('conv2', layers.Conv2DLayer),
    ('pool2', layers.MaxPool2DLayer),
    ('dropout2', layers.DropoutLayer),
    ('conv3', layers.Conv2DLayer),
    ('pool3', layers.MaxPool2DLayer),
    ('dropout3', layers.DropoutLayer),
    ('hidden4', layers.DenseLayer),
    ('output', layers.DenseLayer),
    ],
input_shape=(None, 1, imgSize, imgSize),
conv1_num_filters=32, conv1_filter_size=(param1, param1), pool1_pool_size=(2, 2),
dropout1_p=0.4,
conv2_num_filters=64, conv2_filter_size=(param2, param2), pool2_pool_size=(2, 2),
dropout2_p=0.4,
conv3_num_filters=128, conv3_filter_size=(param3, param3), pool3_pool_size=(2, 2),
dropout3_p=0.4,
hidden4_num_units=1000,
output_num_units=classNum, output_nonlinearity=lasagne.nonlinearities.softmax,

update_learning_rate=0.01,
update_momentum=0.9,

regression=False,
max_epochs=100,
verbose=1,
) 
net.fit(trainD, trainL)

How can I get values of hidden layer neurons on some x? I wont to get that values and use them in some other algorithm to get better result.


Solution

  • So, I found the solution.

    hidden_layer = layers.get_output(net.layers_['hidden4'], deterministic=True)
    input_var = net.layers_['input'].input_var
    f_hidden = theano.function([input_var], hidden_layer)
    instance = TestD[i][None, :, :, :]
    pred = f_hidden(instance)