lo tolmencre
lo tolmencre

Reputation: 3954

Keras Visualization Toolkit throwing `<layer>input:0 is both fed and fetched.` for simple `Sequential` model

I am trying to get the getting started example from Keras Visualization Toolkit to run.

My test code is this:

# simple CNN definition
from keras.models import Sequential
from keras.layers import Dense, Conv1D, Flatten
#create model
model = Sequential()
#add model layers
model.add(Conv1D(1000, kernel_size=3, activation='relu', input_shape=(28,3)))
model.add(Conv1D(50, name='conv1D_2', kernel_size=3, activation='relu'))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))

# code from github page
from vis.losses import ActivationMaximization
from vis.regularizers import TotalVariation, LPNorm

filter_indices = [1, 2, 3]

# Tuple consists of (loss_function, weight)
# Add regularizers as needed.
losses = [
    (ActivationMaximization(model.layers[1], filter_indices), 1),
#     (LPNorm(model.input[1:]), 10),
#     (TotalVariation(model.input), 10)
]

from vis.optimizer import Optimizer

optimizer = Optimizer(model.input, losses)
opt_img, grads, _ = optimizer.minimize()

I am getting the following error:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-38-eb157373ee04> in <module>
     15 
     16 optimizer = Optimizer(model.input, losses)
---> 17 opt_img, grads, _ = optimizer.minimize()

~/anaconda3/envs/ccnn2/lib/python3.7/site-packages/vis/optimizer.py in minimize(self, seed_input, max_iter, input_modifiers, grad_modifier, callbacks, verbose)
    141         best_loss = float('inf')
    142         best_input = None
--> 143 
    144         grads = None
    145         wrt_value = None

~/anaconda3/envs/ccnn2/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
   2713                 return self._legacy_call(inputs)
   2714 
-> 2715             return self._call(inputs)
   2716         else:
   2717             if py_any(is_tensor(x) for x in inputs):

~/anaconda3/envs/ccnn2/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in _call(self, inputs)
   2669                                 feed_symbols,
   2670                                 symbol_vals,
-> 2671                                 session)
   2672         if self.run_metadata:
   2673             fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)

~/anaconda3/envs/ccnn2/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in _make_callable(self, feed_arrays, feed_symbols, symbol_vals, session)
   2621             callable_opts.run_options.CopyFrom(self.run_options)
   2622         # Create callable.
-> 2623         callable_fn = session._make_callable_from_options(callable_opts)
   2624         # Cache parameters corresponding to the generated callable, so that
   2625         # we can detect future mismatches and refresh the callable.

~/anaconda3/envs/ccnn2/lib/python3.7/site-packages/tensorflow/python/client/session.py in _make_callable_from_options(self, callable_options)
   1469     """
   1470     self._extend_graph()
-> 1471     return BaseSession._Callable(self, callable_options)
   1472 
   1473 

~/anaconda3/envs/ccnn2/lib/python3.7/site-packages/tensorflow/python/client/session.py in __init__(self, session, callable_options)
   1423         with errors.raise_exception_on_not_ok_status() as status:
   1424           self._handle = tf_session.TF_SessionMakeCallable(
-> 1425               session._session, options_ptr, status)
   1426       finally:
   1427         tf_session.TF_DeleteBuffer(options_ptr)

~/anaconda3/envs/ccnn2/lib/python3.7/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    526             None, None,
    527             compat.as_text(c_api.TF_Message(self.status.status)),
--> 528             c_api.TF_GetCode(self.status.status))
    529     # Delete the underlying status object from memory otherwise it stays alive
    530     # as there is a reference to status from this from the traceback due to

InvalidArgumentError: conv1d_2_input:0 is both fed and fetched.

There are quite a few posts with this error. So far I could not figure out what the problem is however. I guess it has to do with the model definition. But that is a very vanilla kind of model. So, I don't see a problem there.

Any idea?

Upvotes: 0

Views: 133

Answers (1)

Dr. Snoopy
Dr. Snoopy

Reputation: 56377

Well the problem is that keras-vis latest release is from 2017 (the one available at PyPI), so if you install it using pip, you won't get the fixed version that is available on github. The solution is quite simple, remove the current version you have installed and install the one from github:

pip uninstall keras-vis
pip install --user git+https://github.com/raghakot/keras-vis.git

More information on this issue.

Upvotes: 1

Related Questions