hiranumn / IntegratedGradients

Python/Keras implementation of integrated gradients presented in "Axiomatic Attribution for Deep Networks" for explaining any model defined in Keras framework.
MIT License
216 stars 51 forks source link

Problem to calculate gradients in a netural network with multiple input #1

Closed LucasSilvaFerreira closed 6 years ago

LucasSilvaFerreira commented 6 years ago

HI, I have a 4 inputs convolutional network that uses different merged types of inputs.

Its like: 4 separate inputs, which one if 2 with 1 x100 [discrete encoded variables] 2 with 1x100[continuous values] I received this error ouput when I tried to run the model using this 4 input network.

Is it possible to capture the individual contribution values using this technique or It just works to non-merged models?

Thanks

python
**MissingInputError                         Traceback (most recent call last)
<ipython-input-33-26950f8e5048> in <module>()
----> 1 plot_roc( t_valores, t_grupos, t_conservation, 'model_used_to_paper.hdf5')

<ipython-input-32-5d76776930b0> in plot_roc(v_teste, c_teste, t_conservation, model_file)
    109 
    110 
--> 111     integrated_gradients(model=merged_model, outchannels=[0])

/home/lucas/PycharmProjects/MEGS_introns/deeplearning/IntegratedGradients.py in __init__(self, model, outchannels, verbose)
     77 
     78             # Build computational graph that calculates the tensfor given inputs
---> 79             self.get_gradients[c] = K.function(inputs=self.input_tensors, outputs=gradients)
     80 
     81             # This takes a lot of time for a big model with many tasks.

/home/lucas/miniconda2/lib/python2.7/site-packages/keras/backend/theano_backend.pyc in function(inputs, outputs, updates, **kwargs)
    680                 msg = "Invalid argument '%s' passed to K.function" % key
    681                 raise ValueError(msg)
--> 682     return Function(inputs, outputs, updates=updates, **kwargs)
    683 
    684 

/home/lucas/miniconda2/lib/python2.7/site-packages/keras/backend/theano_backend.pyc in __init__(self, inputs, outputs, updates, **kwargs)
    666                                         allow_input_downcast=True,
    667                                         on_unused_input='ignore',
--> 668                                         **kwargs)
    669 
    670     def __call__(self, inputs):

/home/lucas/.local/lib/python2.7/site-packages/theano/compile/function.pyc in function(inputs, outputs, mode, updates, givens, no_default_updates, accept_inplace, name, rebuild_strict, allow_input_downcast, profile, on_unused_input)
    324                    on_unused_input=on_unused_input,
    325                    profile=profile,
--> 326                    output_keys=output_keys)
    327     # We need to add the flag check_aliased inputs if we have any mutable or
    328     # borrowed used defined inputs

/home/lucas/.local/lib/python2.7/site-packages/theano/compile/pfunc.pyc in pfunc(params, outputs, mode, updates, givens, no_default_updates, accept_inplace, name, rebuild_strict, allow_input_downcast, profile, on_unused_input, output_keys)
    484                          accept_inplace=accept_inplace, name=name,
    485                          profile=profile, on_unused_input=on_unused_input,
--> 486                          output_keys=output_keys)
    487 
    488 

/home/lucas/.local/lib/python2.7/site-packages/theano/compile/function_module.pyc in orig_function(inputs, outputs, mode, accept_inplace, name, profile, on_unused_input, output_keys)
   1792                    profile=profile,
   1793                    on_unused_input=on_unused_input,
-> 1794                    output_keys=output_keys).create(
   1795             defaults)
   1796 

/home/lucas/.local/lib/python2.7/site-packages/theano/compile/function_module.pyc in __init__(self, inputs, outputs, mode, accept_inplace, function_builder, profile, on_unused_input, fgraph, output_keys)
   1444             # OUTPUT VARIABLES)
   1445             fgraph, additional_outputs = std_fgraph(inputs, outputs,
-> 1446                                                     accept_inplace)
   1447             fgraph.profile = profile
   1448         else:

/home/lucas/.local/lib/python2.7/site-packages/theano/compile/function_module.pyc in std_fgraph(input_specs, output_specs, accept_inplace)
    175 
    176     fgraph = gof.fg.FunctionGraph(orig_inputs, orig_outputs,
--> 177                                   update_mapping=update_mapping)
    178 
    179     for node in fgraph.apply_nodes:

/home/lucas/.local/lib/python2.7/site-packages/theano/gof/fg.pyc in __init__(self, inputs, outputs, features, clone, update_mapping)
    178 
    179         for output in outputs:
--> 180             self.__import_r__(output, reason="init")
    181         for i, output in enumerate(outputs):
    182             output.clients.append(('output', i))

/home/lucas/.local/lib/python2.7/site-packages/theano/gof/fg.pyc in __import_r__(self, variable, reason)
    349         # Imports the owners of the variables
    350         if variable.owner and variable.owner not in self.apply_nodes:
--> 351                 self.__import__(variable.owner, reason=reason)
    352         elif (variable.owner is None and
    353                 not isinstance(variable, graph.Constant) and

/home/lucas/.local/lib/python2.7/site-packages/theano/gof/fg.pyc in __import__(self, apply_node, check, reason)
    395                                      % (node.inputs.index(r), str(node)))
    396                         error_msg += get_variable_trace_string(r)
--> 397                         raise MissingInputError(error_msg, variable=r)
    398 
    399         for node in new_nodes:

MissingInputError: Input 0 of the graph (indices start from 0), used to compute InplaceDimShuffle{0,1,2,x}(convolution1d_input_26), was not provided and not given a value. Use the Theano flag exception_verbosity='high', for more information on this error. 
Backtrace when that variable is created:

  File "/home/lucas/miniconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-33-26950f8e5048>", line 1, in <module>
    plot_roc( t_valores, t_grupos, t_conservation, 'model_used_to_paper.hdf5')
  File "<ipython-input-32-5d76776930b0>", line 16, in plot_roc
    model_up_conservation.add(Convolution1D(100, filter_first_layer_size, input_shape=(SIZE_SEQUENCE, 1), name='up_conservation_intron_layer'))
  File "/home/lucas/miniconda2/lib/python2.7/site-packages/keras/models.py", line 276, in add
    layer.create_input_layer(batch_input_shape, input_dtype)
  File "/home/lucas/miniconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 367, in create_input_layer
    dtype=input_dtype, name=name)
  File "/home/lucas/miniconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 1085, in Input
    input_tensor=tensor)
  File "/home/lucas/miniconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 1008, in __init__
    name=self.name)
  File "/home/lucas/miniconda2/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 54, in placeholder
    x = T.TensorType(dtype, broadcast)(name)

Backtrace when the variable is created:
  File "/home/lucas/miniconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-33-26950f8e5048>", line 1, in <module>
    plot_roc( t_valores, t_grupos, t_conservation, 'model_used_to_paper.hdf5')
  File "<ipython-input-32-5d76776930b0>", line 16, in plot_roc
    model_up_conservation.add(Convolution1D(100, filter_first_layer_size, input_shape=(SIZE_SEQUENCE, 1), name='up_conservation_intron_layer'))
  File "/home/lucas/miniconda2/lib/python2.7/site-packages/keras/models.py", line 276, in add
    layer.create_input_layer(batch_input_shape, input_dtype)
  File "/home/lucas/miniconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 367, in create_input_layer
    dtype=input_dtype, name=name)
  File "/home/lucas/miniconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 1085, in Input
    input_tensor=tensor)
  File "/home/lucas/miniconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 1008, in __init__
    name=self.name)
  File "/home/lucas/miniconda2/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 54, in placeholder
    x = T.TensorType(dtype, broadcast)(name)**
hiranumn commented 6 years ago

This technique can deal with multiple inputs, but the way I wrote the current package does not. I plan to adjust to that but I am currently busy with other stuff. I could perhaps take a look at this within a week.

hiranumn commented 6 years ago

Should be fixed now.