My model is trained on digit images (MNIST dataset
). I am trying to print the output of the second layer of my network - an array of 128 numbers.
After reading a lot of examples - for instance this, and this, or this.
I did not manage to do this on my own network. Neither of the solutions work of my own algorithm.
I received a lot of different error messages. I tried to handle each of them, but couldn't figure it on my own.
What am I missing? How to output the Second layer?
If my Shape is (28,28)
- what should be the type & value of input_shape
?
Failed trials & Errors for example:
(1)
for layer in model.layers:
get_2nd_layer_output = K.function([model.layers[0].input],[model.layers[2].output])
layer_output = get_2nd_layer_output(layer)[0]
print('\nlayer output: get_2nd_layer_output=, layer=', layer, '\nlayer output: get_2nd_layer_output=', get_2nd_layer_output)
TypeError: inputs should be a list or tuple.
(2)
input_shape=(28, 28)
inp = model.input # input placeholder
outputs = [layer.output for layer in model.layers] # all layer outputs
functor = K.function([inp, K.learning_phase()], outputs ) # evaluation function
# Testing
test = np.random.random(input_shape)[np.newaxis,...]
layer_outs = functor([test, 0.])
print('layer_outs',layer_outs)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable dense_1/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/dense_1/bias) [[{{node dense_1/BiasAdd/ReadVariableOp}}]]