albermax / albermax/innvestigate

[BUG?] analyzer.analyze() not working properly

未关闭
#193 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Python
星标
1.3k
派生
230
PR 合并指标
30 天内没有已合并 PR

描述

I am running this code cell in a notebook
```
for i, ridx in enumerate(test_sample_paths):
x = dset[ridx]['matrix']
x = x.reshape((1, -1, 300,1))
x = x.numpy()
prob = k_model.predict_on_batch(x)[0]
y_hat = prob.argmax()

for aidx, analyzer in enumerate(analyzers):
a = np.squeeze(analyzer.analyze(x))
a = np.sum(a, axis=1) ### Add the values along the embedding dimension
analysis[i, aidx] = a
```

This code results in error in this line `a = np.squeeze(analyzer.analyze(x))` (This is for Smoothgrad analyzer) . The error is given below. When I rerun the cell, I get the same error again(This is for integrated_gradient analyzer), but on 3rd time, it runs FINE!! The print the text heatmap later and they seem to be alright as well.

> ---------------------------------------------------------------------------
> TypeError Traceback (most recent call last)
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
> 526 try:
> --> 527 str_values = [compat.as_bytes(x) for x in proto_values]
> 528 except TypeError:
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in (.0)
> 526 try:
> --> 527 str_values = [compat.as_bytes(x) for x in proto_values]
> 528 except TypeError:
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/tensorflow/python/util/compat.py in as_bytes(bytes_or_text, encoding)
> 60 raise TypeError('Expected binary or unicode string, got %r' %
> ---> 61 (bytes_or_text,))
> 62
>
> TypeError: Expected binary or unicode string, got -1
>
> During handling of the above exception, another exception occurred:
>
> TypeError Traceback (most recent call last)
> in
> 18 for aidx, analyzer in enumerate(analyzers):
> 19 print(analyzer)
> ---> 20 a = np.squeeze(analyzer.analyze(x))#, neuron_selection=y_hat))
> 21 print(a.shape)
> 22 a = np.sum(a, axis=1) ### Add the values along the embedding dimension
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/innvestigate/analyzer/wrapper.py in analyze(self, X, *args, **kwargs)
> 143 if self._keras_based_augment_reduce is True:
> 144 if not hasattr(self._subanalyzer, "_analyzer_model"):
> --> 145 self.create_analyzer_model()
> 146
> 147 ns_mode = self._neuron_selection_mode
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/innvestigate/analyzer/wrapper.py in create_analyzer_model(self)
> 129 "with this wrapper.")
> 130
> --> 131 new_inputs = iutils.to_list(self._augment(inputs))
> 132 # print(type(new_inputs), type(extra_inputs))
> 133 tmp = iutils.to_list(model(new_inputs+extra_inputs))
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/innvestigate/analyzer/wrapper.py in _augment(self, X)
> 290 tmp = super(PathIntegrator, self)._augment(X)
> 291 tmp = [ilayers.Reshape((-1, self._augment_by_n)+K.int_shape(x)[1:])(x)
> --> 292 for x in tmp]
> 293
> 294 difference = self._compute_difference(X)
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/innvestigate/analyzer/wrapper.py in (.0)
> 290 tmp = super(PathIntegrator, self)._augment(X)
> 291 tmp = [ilayers.Reshape((-1, self._augment_by_n)+K.int_shape(x)[1:])(x)
> --> 292 for x in tmp]
> 293
> 294 difference = self._compute_difference(X)
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
> 455 # Actually call the layer,
> 456 # collecting output(s), mask(s), and shape(s).
> --> 457 output = self.call(inputs, **kwargs)
> 458 output_mask = self.compute_mask(inputs, previous_mask)
> 459
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/innvestigate/layers.py in call(self, x)
> 489
> 490 def call(self, x):
> --> 491 return K.reshape(x, self._shape)
> 492
> 493 def compute_output_shape(self, input_shapes):
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in reshape(x, shape)
> 1967 A tensor.
> 1968 """
> -> 1969 return tf.reshape(x, shape)
> 1970
> 1971
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py in reshape(tensor, shape, name)
> 6480 if _ctx is None or not _ctx._eager_context.is_eager:
> 6481 _, _, _op = _op_def_lib._apply_op_helper(
> -> 6482 "Reshape", tensor=tensor, shape=shape, name=name)
> 6483 _result = _op.outputs[:]
> 6484 _inputs_flat = _op.inputs
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
> 511 except TypeError as err:
> 512 if dtype is None:
> --> 513 raise err
> 514 else:
> 515 raise TypeError(
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
> 508 dtype=dtype,
> 509 as_ref=input_arg.is_ref,
> --> 510 preferred_dtype=default_dtype)
> 511 except TypeError as err:
> 512 if dtype is None:
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
> 1144
> 1145 if ret is None:
> -> 1146 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
> 1147
> 1148 if ret is NotImplemented:
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
> 227 as_ref=False):
> 228 _ = as_ref
> --> 229 return constant(v, dtype=dtype, name=name)
> 230
> 231
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name, verify_shape)
> 206 tensor_value.tensor.CopyFrom(
> 207 tensor_util.make_tensor_proto(
> --> 208 value, dtype=dtype, shape=shape, verify_shape=verify_shape))
> 209 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
> 210 const_tensor = g.create_op(
>
> ~/anaconda3/envs/gpu_ptorch/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
> 529 raise TypeError("Failed to convert object of type %s to Tensor. "
> 530 "Contents: %s. Consider casting elements to a "
> --> 531 "supported type." % (type(values), values))
> 532 tensor_proto.string_val.extend(str_values)
> 533 return tensor_proto
>
> TypeError: Failed to convert object of type to Tensor. Contents: (-1, 64, None, 300, 1). Consider casting elements to a supported type.

The analyzers are the following:

```
input_range = (-24.0467,25.3291)
analyzers = []
noise_scale = (input_range[1]-input_range[0]) * 0.1
ri = input_range[0] # reference input

methods = [
# NAME OPT.PARAMS
("gradient", {}, "Gradient"),
("smoothgrad", {"noise_scale": noise_scale,"postprocess": "square"}, "SmoothGrad"),
("deconvnet", {}, "Deconvnet"),
("guided_backprop", {}, "Guided Backprop",),
("pattern.net", {"pattern_type": "relu"}, "PatternNet"),
("pattern.attribution", {"pattern_type": "relu"}, "PatternAttribution"),
("deep_taylor.bounded", {"low": input_range[0],"high": input_range[1]}, "DeepTaylor"),
("input_t_gradient", {}, "Input * Gradient"),
("integrated_gradients", {"reference_inputs": ri}, "Integrated Gradients"),
#("deep_lift.wrapper", {"reference_inputs": ri}, "DeepLIFT Wrapper - Rescale"),
#("deep_lift.wrapper", {"reference_inputs": ri, "nonlinear_mode": "reveal_cancel"}, "DeepLIFT Wrapper - RevealCancel"),
("lrp.z", {}, "LRP-Z"),
("lrp.epsilon", {"epsilon": 1}, "LRP-Epsilon"),
]
for method in methods:
analyzer = innvestigate.create_analyzer(method[0], k_model,**method[1])
analyzer.fit(train_data, batch_size=1024, verbose=1)
analyzers.append(analyzer)
```
There is no error in this code cell

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。