Getting model activations with K.function doesn't work since v2.2.0
See original GitHub issueHi there,
I’m running into problems with getting the activations of a model using K.function since Keras 2.2.0. With 2.1.6, everything works fine. I’m using the tensorflow backend with a convnet with multiple outputs.
A (shortened) code snippet can be found below:
saved_model = ks.models.load_model('...') # the model has been saved with 2.2.0
inp = saved_model.input
generator = generate_batches_from_hdf5_file(...)
model_inputs, ys = next(generator)
lp = 0. if learning_phase == 'test' else 1.
outputs = [layer.output for layer in saved_model.layers if
layer.name == layer_name or layer_name is None] # all layer outputs -> empty tf.tensors
funcs = [K.function([inp] + [K.learning_phase()], [out]) for out in outputs] # evaluation functions
list_inputs = [model_inputs, lp]
layer_outputs = [func(list_inputs)[0] for func in funcs] <- throws an error since Keras 2.2.0
...
The last line yields the following error since 2.2.0:
File ".../visualization_tools.py", line 135, in get_activations_and_weights
layer_outputs = [func(list_inputs)[0] for func in funcs]
File "/home/hpc/capn/mppi033h/.local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2661, in __call__
return self._call(inputs)
File "/home/hpc/capn/mppi033h/.local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2630, in _call
session)
File "/home/hpc/capn/mppi033h/.local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2582, in _make_callable
callable_fn = session._make_callable_from_options(callable_opts)
File "/home/hpc/capn/mppi033h/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1480, in _make_callable_from_options
return BaseSession._Callable(self, callable_options)
File "/home/hpc/capn/mppi033h/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1441, in __init__
session._session, options_ptr, status)
File "/home/hpc/capn/mppi033h/.local/lib/python2.7/site-packages/tensorflow/python/framework/errors_impl.py", line 519, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: input_1_1:0 is both fed and fetched.
Exception tensorflow.python.framework.errors_impl.InvalidArgumentError: InvalidArgumentError() in <bound method _Callable.__del__ of <tensorflow.python.client.session._Callable object at 0x7f35b1f20a10>> ignored
Any idea what could have changed in 2.2.0?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How to get the activations after layer k with Keras?
To run a model after you've finished training it you need to use keras predict(). This will evaluate the graph, given your input...
Read more >Creating Custom Activation Functions with Lambda Layers in ...
Learning to create a simple custom ReLU activation function using lambda layers in TensorFlow 2 ... In this article, we look at how...
Read more >3 ways to create a Keras model with TensorFlow 2.0 ...
Our models.py contains three functions to build Keras/TensorFlow 2.0 models using the Sequential, Functional and Model subclassing APIs, ...
Read more >tf.keras.Sequential | TensorFlow v2.11.0
Sequential groups a linear stack of layers into a tf.keras.Model. ... your Model cannot be run inside a tf.function . run_eagerly=True is not...
Read more >TensorFlow 2 Tutorial: Get Started in Deep Learning with tf.keras
How to develop deep learning models with tf.keras ... of the model, configuring each layer with a number of nodes and activation function, ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@ViaFerrata After checking your code, I found the
input_layerwas passed into bothfeedandfetch. This is illegal for TF since keras switch to usetf.Session._make_callable_from_options. You can insertoutputs = outputs[1:]beforeto remove the first
input_layerfromfetch, then it can work well.Similar issue here too. I’m trying to run the example script for ‘Activation Maximization on VGGNet’.