GithubHelp home page GithubHelp logo

hiranumn / integratedgradients Goto Github PK

View Code? Open in Web Editor NEW
216.0 216.0 51.0 884 KB

Python/Keras implementation of integrated gradients presented in "Axiomatic Attribution for Deep Networks" for explaining any model defined in Keras framework.

License: MIT License

Python 3.57% Jupyter Notebook 96.43%

integratedgradients's People

Contributors

hiranumn avatar kmader avatar neutralino avatar slundberg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

integratedgradients's Issues

Theano Implementation

Thank you for implementing Integrated Gradient.

Currently, my CNN model is based on Theano.
May I ask you how I can implement Integrated Gradient with Theano backend?

Thanks.

Problem to calculate gradients in a netural network with multiple input

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)**

Got TypeError when running VGG example

The error log is below:

TypeError                                 Traceback (most recent call last)
<ipython-input-15-73a1b345b022> in <module>()
----> 1 ig = integrated_gradients(model)

/home/ubuntu/GA/IntegratedGradients-master/IntegratedGradients.py in __init__(self, model, outchannels, verbose)
     73                 gradients = self.model.optimizer.get_gradients(self.model.output[:, c], self.model.input)
     74             if K.backend() == "theano":
---> 75                 gradients = self.model.optimizer.get_gradients(self.model.output[:, c].sum(), self.model.input)
     76 
     77             # Build computational graph that computes the tensors given inputs

/home/ubuntu/anaconda3/envs/theano_p27/lib/python2.7/site-packages/keras/optimizers.pyc in get_gradients(self, loss, params)
     88     def get_gradients(self, loss, params):
     89         grads = K.gradients(loss, params)
---> 90         if None in grads:
     91             raise ValueError('An operation has `None` for gradient. '
     92                              'Please make sure that all of your ops have a '

/home/ubuntu/anaconda3/envs/theano_p27/lib/python2.7/site-packages/theano/tensor/var.pyc in __iter__(self)
    630         except TypeError:
    631             # This prevents accidental iteration via builtin.sum(self)
--> 632             raise TypeError(('TensorType does not support iteration. '
    633                              'Maybe you are using builtin.sum instead of '
    634                              'theano.tensor.sum? (Maybe .max?)'))

TypeError: TensorType does not support iteration. Maybe you are using builtin.sum instead of theano.tensor.sum? (Maybe .max?)

Running IntegratedGradients with text input

Hi authors,

Thanks a lot for the integrated gradients technique and code. I am trying to use this for a text classification task that I am working on. Unfortunately, the tf.gradient function returns None while trying to get the gradient op of output w.r.t. input. After spending a fair amount of time trying to debug it, I realized that the input layer for me is just the ids of the words which are converted to word vectors after being passed through an embedding layer. However in the paper, results have been reported for text inputs too. Hence I wanted to confirm that this is the issue before changing the implementation to handle text inputs? Thanks in advance for your help.

run VGG exmaple but always out of memory

hi , when i run VGG example , it always out of memory when do ig.explain(), i have 2 T80 Gpu,
and i try to constrain the usage of GPU but failed , do you know what the problem?

global name 'model1' not defined

When trying to run with a dense feedforward network I get the following error:

Traceback (most recent call last):
  File "avocado_fit.py", line 66, in <module>
    ig = integrated_gradients(model)
  File "/net/noble/vol1/home/jmschr/proj/avocado/exps/2_13_18_Intepretability/IntegratedGradients.py", line 57, in __init__
    self.outchannels = range(model1.output._keras_shape[1])
NameError: global name 'model1' is not defined

This can be traced back to this code:

if len(self.outchannels) == 0:
            if verbose: print("Evaluated output channel (0-based index): All")
            if K.backend() == "tensorflow":
                self.outchannels = range(self.model.output.shape[1]._value)
            elif K.backend() == "theano":
                self.outchannels = range(model1.output._keras_shape[1])

model1 isn't defined. I switched it over to model, is that the right fix?

Missing licensing information

This code base seems to lack an explicit license file. If you don't have any particular preference may I suggest a license to reuse the same license as Keras itself, that is the MIT license?

This error comes with tensorflow 2.x

AttributeError                            Traceback (most recent call last)
<ipython-input-21-73a1b345b022> in <module>()
----> 1 ig = integrated_gradients(model)

/content/IntegratedGradients/IntegratedGradients.py in __init__(self, model, outchannels, verbose)
     33         #load model supports keras.Model and keras.Sequential
     34         if isinstance(model, Sequential):
---> 35             self.model = model.model
     36         elif isinstance(model, Model):
     37             self.model = model

AttributeError: 'Sequential' object has no attribute 'model'

Issue with multiple input

HI,
I'm still receiving error output
(enclosed error log and model)

Thank you


MissingInputError Traceback (most recent call last)
in ()
----> 1 plot_roc( t_valores, t_grupos, t_conservation, 'model_used_to_paper.hdf5')

in plot_roc(v_teste, c_teste, t_conservation, model_file)
109
110
--> 111 integrated_gradients(model=merged_model)

/home/lucas/PycharmProjects/MEGS_introns/deeplearning/IntegratedGradients.py in init(self, model, outchannels, verbose)
77
78 # This takes a lot of time for a big model with many tasks.
---> 79 # So lets pring the progress.
80 if verbose:
81 sys.stdout.write('\r')

/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_34), 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 "", line 1, in
plot_roc( t_valores, t_grupos, t_conservation, 'model_used_to_paper.hdf5')
File "", 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 "", line 1, in
plot_roc( t_valores, t_grupos, t_conservation, 'model_used_to_paper.hdf5')
File "", 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)

`

`python
def plot_roc( v_teste,c_teste, t_conservation, model_file):

filter_first_layer_size = 6

model_up = Sequential()
model_up.add(Convolution1D(20, filter_first_layer_size, input_shape=(SIZE_SEQUENCE, 4), name='up_intron_layer'))
model_up.add(Activation('relu'))
#model_up.add(MaxPooling1D(3))
model_up.add(Dropout(0.5))
model_up.add(Convolution1D(100, 3))
model_up.add(Activation('relu'))
model_up.add(MaxPooling1D(2))
#model_up.add(Dropout(0.1))

model_up_conservation = Sequential()
model_up_conservation.add(Convolution1D(100, filter_first_layer_size, input_shape=(SIZE_SEQUENCE, 1), name='up_conservation_intron_layer'))
model_up_conservation.add(Activation('relu'))



merged_conservation_up = Sequential()
merged_conservation_up.add(Merge([model_up, model_up_conservation], mode='concat', concat_axis=1))


model_down = Sequential()
model_down.add(Convolution1D(20, filter_first_layer_size, input_shape=(SIZE_SEQUENCE, 4), name='down_intron_layer'))
model_down.add(Activation('relu'))
#model_down.add(MaxPooling1D(3))
model_down.add(Dropout(0.5))
model_down.add(Convolution1D(100, 3))
model_down.add(Activation('relu'))
model_down.add(MaxPooling1D(2))
#model_down.add(Dropout(0.1))


model_down_conservation = Sequential()
model_down_conservation.add(Convolution1D(100, filter_first_layer_size, input_shape=(SIZE_SEQUENCE, 1), name='down_conservation_intron_layer'))
model_down_conservation.add(Activation('relu'))


merged_conservation_down = Sequential()
merged_conservation_down.add(Merge([model_down, model_down_conservation], mode='concat', concat_axis=1))



merged_model = Sequential()
merged_model.add(Merge([merged_conservation_up, merged_conservation_down], mode='concat', concat_axis=1))
#print merged_model.summary()
#model.add(MaxPooling2D(pool_size=(2, 2)))

merged_model.add(Flatten())
#model.add(Dense(12, input_dim=[4, 200], init='uniform', activation='relu'))
#model.add(Dense(8, init='uniform', activation='relu'))
#model.add(Dense(1, init='uniform', activation='sigmoid'))
merged_model.add(Dense(100))
merged_model.add(Activation('relu'))
merged_model.add(Dropout(0.5))
merged_model.add(Dense(100))
merged_model.add(Activation('relu'))
#merged_model.add(Dropout(0.75))
merged_model.add(Dense(output_dim=1))
merged_model.add(Activation('sigmoid'))


conser_up_teste = np.array([v for v_u in t_conservation for v in v_u[0]])
print conser_up_teste.shape
conser_up_teste = conser_up_teste.reshape(len(conser_up_teste)/SIZE_SEQUENCE, SIZE_SEQUENCE, 1)
print 'reshaped', conser_up_teste.shape
conser_down_teste = np.array([v for v_u in t_conservation for v in v_u[1]])
print conser_down_teste.shape
conser_down_teste = conser_down_teste.reshape(len(conser_down_teste)/SIZE_SEQUENCE, SIZE_SEQUENCE, 1)
print 'reshaped', conser_down_teste.shape
print conser_down_teste[0].shape
print 'saindo'
print np.array([v_down[SIZE_SEQUENCE:] for v_down in v_teste]).shape

merged_model.load_weights(model_file)
merged_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])



scores = merged_model.evaluate(
        [
            np.array([v_up[:SIZE_SEQUENCE] for v_up in v_teste]),  conser_up_teste,  np.array([v_down[SIZE_SEQUENCE:] for v_down in v_teste]), conser_down_teste],


            np.array([c_ts for c_ts in c_teste]),

        verbose=0)
print("%s: %.2f%%" % (merged_model.metrics_names[1], scores[1] * 100))
print('Predicting on test data')

y_score = merged_model.predict(
        [
            np.array([v_up[:SIZE_SEQUENCE] for v_up in v_teste]),
            conser_up_teste,
            np.array([v_down[SIZE_SEQUENCE:] for v_down in v_teste]),
            conser_down_teste
        ])
    


#y_score = model.predict(X_test)

print('Generating results')
generate_results(np.array([c_ts for c_ts in c_teste]),
                y_score[:, 0])



integrated_gradients(model=merged_model)

`

AttributeError: 'int' object has no attribute 'op'

I tried to run the example you provided with iris dataset with keras. But I am getting AttributeError: 'int' object has no attribute 'op' error. My TensorFlow version is 2.2. The error is occurring in the below line:

 #Build a computational graph that computes the tensors given inputs
self.get_gradients[c] = K.function(inputs=self.input_tensors, outputs=gradients)

Problem of calculating gradient from embedding to word

The model can only get gradient for embedding layer. If the input of the model is word id and use Embedding Layer, the Integrated-Gradients return an error.

ValueError                                Traceback (most recent call last)
<ipython-input-8-223e71b35bf0> in <module>()
----> 1 ig = integrated_gradients(model)
      2 exp = ig.explain([X_val[0][1], X_val[1][1], X_val[2][1], X_val[3][1]] )

./IntegratedGradients/IntegratedGradients.py in __init__(self, model, outchannels, verbose)
     74             # Get tensor that calculates gradient
     75             if K.backend() == "tensorflow":
---> 76                 gradients = self.model.optimizer.get_gradients(self.model.output[:, c], self.model.input)
     77             if K.backend() == "theano":
     78                 gradients = self.model.optimizer.get_gradients(self.model.output[:, c].sum(), self.model.input)

~/.local/lib/python3.6/site-packages/keras/optimizers.py in get_gradients(self, loss, params)
     78         grads = K.gradients(loss, params)
     79         if None in grads:
---> 80             raise ValueError('An operation has `None` for gradient. '
     81                              'Please make sure that all of your ops have a '
     82                              'gradient defined (i.e. are differentiable). '

ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.

However, I can get the gradient of embedding, how to determine the value of single words, for example in paper Section 6.3 Question Classification?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.