Skip to content

AttributeError: 'tuple' object has no attribute 'layer' when using ActivationsVisualizationCallback #643

Open
@rao208

Description

@rao208

Hi, I am getting the same error.
**print(tf.__version__)**:

2.0

**print(tf.keras.__version__)**

2.2.4-tf

I have tensorflow-gpu installed. I am getting this error when I run the following code:

import tf_explain
from tf_explain.callbacks.activations_visualization import ActivationsVisualizationCallback

callbacks = [
    ActivationsVisualizationCallback(validation_data=(x_val, y_val),
                                     layers_name=["hidden_layer_1"],
        output_dir="/home/raov/Desktop/Share/uni_freiburg/Datasets",
    ),
]


model.fit(x= x_train, y = y_train, batch_size=2, epochs=2, verbose= 1, callbacks=callbacks, validation_data= (x_val, y_val))

I guess it has something to do with the callback function. The source of the above code is https://github.com/sicara/tf-explain

Please note, that I tried model.fit with and without the validation data. Both the times I get the same error, so it has nothing to do with the validation data I guess

I did see the answer from the similar post "AttributeError: 'tuple' object has no attribute 'layer' #478". It didn't help me

Any idea how to solve this problem?

Output

Train on 40000 samples, validate on 10000 samples
Epoch 1/2
40000/40000 [==============================] - 245s 6ms/step - loss: 0.7130 - accuracy: 0.8195 - val_loss: 0.6355 - val_accuracy: 0.8540

Traceback

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-49-9049e3b5856c> in <module>
     36 
     37 
---> 38 model.fit(x= x_train, y = y_train, batch_size=2, epochs=2, verbose= 1, callbacks=callbacks, validation_data= (x_val, y_val))

~/.virtualenvs/deep_learning/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
   1237                                         steps_per_epoch=steps_per_epoch,
   1238                                         validation_steps=validation_steps,
-> 1239                                         validation_freq=validation_freq)
   1240 
   1241     def evaluate(self,

~/.virtualenvs/deep_learning/lib/python3.6/site-packages/keras/engine/training_arrays.py in fit_loop(model, fit_function, fit_inputs, out_labels, batch_size, epochs, verbose, callbacks, val_function, val_inputs, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq)
    214                         epoch_logs['val_' + l] = o
    215 
--> 216         callbacks.on_epoch_end(epoch, epoch_logs)
    217         if callbacks.model.stop_training:
    218             break

~/.virtualenvs/deep_learning/lib/python3.6/site-packages/keras/callbacks/callbacks.py in on_epoch_end(self, epoch, logs)
    150         logs = logs or {}
    151         for callback in self.callbacks:
--> 152             callback.on_epoch_end(epoch, logs)
    153 
    154     def on_train_batch_begin(self, batch, logs=None):

~/.virtualenvs/deep_learning/lib/python3.6/site-packages/tf_explain/callbacks/activations_visualization.py in on_epoch_end(self, epoch, logs)
     48         """
     49         explainer = ExtractActivations()
---> 50         grid = explainer.explain(self.validation_data, self.model, self.layers_name)
     51 
     52         # Using the file writer, log the reshaped image.

~/.virtualenvs/deep_learning/lib/python3.6/site-packages/tf_explain/core/activations.py in explain(self, validation_data, model, layers_name)
     29             np.ndarray: Grid of all the activations
     30         """
---> 31         activations_model = self.generate_activations_graph(model, layers_name)
     32 
     33         predictions = activations_model.predict(

~/.virtualenvs/deep_learning/lib/python3.6/site-packages/tf_explain/core/activations.py in generate_activations_graph(model, layers_name)
     53         """
     54         outputs = [layer.output for layer in model.layers if layer.name in layers_name]
---> 55         activations_model = tf.keras.models.Model(model.inputs, outputs=outputs)
     56         activations_model.compile(optimizer="sgd", loss="categorical_crossentropy")
     57 

~/.virtualenvs/deep_learning/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py in __init__(self, *args, **kwargs)
    144 
    145   def __init__(self, *args, **kwargs):
--> 146     super(Model, self).__init__(*args, **kwargs)
    147     _keras_api_gauge.get_cell('model').set(True)
    148     # initializing _distribution_strategy here since it is possible to call

~/.virtualenvs/deep_learning/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py in __init__(self, *args, **kwargs)
    165         'inputs' in kwargs and 'outputs' in kwargs):
    166       # Graph network
--> 167       self._init_graph_network(*args, **kwargs)
    168     else:
    169       # Subclassed network

~/.virtualenvs/deep_learning/lib/python3.6/site-packages/tensorflow_core/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
    455     self._self_setattr_tracking = False  # pylint: disable=protected-access
    456     try:
--> 457       result = method(self, *args, **kwargs)
    458     finally:
    459       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

~/.virtualenvs/deep_learning/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py in _init_graph_network(self, inputs, outputs, name, **kwargs)
    271 
    272     self._base_init(name=name, **kwargs)
--> 273     self._validate_graph_inputs_and_outputs()
    274 
    275     # A Network does not create weights of its own, thus it is already

~/.virtualenvs/deep_learning/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/network.py in _validate_graph_inputs_and_outputs(self)
   1284       # Check that x is an input tensor.
   1285       # pylint: disable=protected-access
-> 1286       layer = x._keras_history.layer
   1287       if len(layer._inbound_nodes) > 1 or (
   1288           layer._inbound_nodes and layer._inbound_nodes[0].inbound_layers):

AttributeError: 'tuple' object has no attribute 'layer'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions