Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidArgumentError: input_1:0 is both fed and fetched

I found this GitHub example about print activation maps. Code is quite simple. All I did was copy pasted the function.

def get_activations(model, model_inputs, print_shape_only=False, layer_name=None):
    print('----- activations -----')
    activations = []
    inp = model.input

    model_multi_inputs_cond = True
    if not isinstance(inp, list):
        # only one input! let's wrap it in a list.
        inp = [inp]
        model_multi_inputs_cond = False

    outputs = [layer.output for layer in model.layers if
               layer.name == layer_name or layer_name is None]  # all layer outputs

    funcs = [K.function(inp + [K.learning_phase()], [out]) for out in outputs]  # evaluation functions

    if model_multi_inputs_cond:
        list_inputs = []
        list_inputs.extend(model_inputs)
        list_inputs.append(0.)
    else:
        list_inputs = [model_inputs, 0.]


    print list_inputs
    layer_outputs = [func(list_inputs)[0] for func in funcs]
    for layer_activations in layer_outputs:
        activations.append(layer_activations)
        if print_shape_only:
            print(layer_activations.shape)
        else:
            print(layer_activations)
    return activations

And then I passed my model and inputs. However, it generates this error

Traceback (most recent call last):
  File "test_cnn_128.py", line 80, in <module>
    get_activations(model, test_x)
  File "test_cnn_128.py", line 45, in get_activations
    layer_outputs = [func(list_inputs)[0] for func in funcs]
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2666, in __call__
    return self._call(inputs)
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2635, in _call
    session)
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2587, in _make_callable
    callable_fn = session._make_callable_from_options(callable_opts)
  File "/home/fatima/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1414, in _make_callable_from_options
    return BaseSession._Callable(self, callable_options)
  File "/home/fatima/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1368, in __init__
    session._session, options_ptr, status)
  File "/home/fatima/.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: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 0x7f44de0cd210>> ignored

I am unsure of how to fix this.

like image 398
Nerd Giraffe Avatar asked Oct 02 '18 10:10

Nerd Giraffe


1 Answers

As I posted on the thread Keras, How to get the output of each layer?, the way to solve this is to replace the line

outputs = [
    layer.output
    for layer in model.layers
    if layer.name == layer_name or layer_name is None
]

with

outputs = [
    layer.output
    for layer in model.layers
    if layer.name == layer_name or layer_name is None
][1:]

...in order to skip the input layer.

like image 152
KamKam Avatar answered Oct 28 '22 11:10

KamKam