Reputation: 1390
Here there is a reduced example of the code:
class MyCustomLayer(keras.layers.Layer):
def __init__(self, num_filters=64, kernel_size=3):
keras.layers.Layer.__init__(self)
self.conv_1 = keras.layers.Conv2D(filters=num_filters,
kernel_size=kernel_size)
def call(self, inputs):
return self.conv_1(inputs)
x = keras.Input(shape=(None, None, 3))
my_custom_layer = MyCustomLayer()
y = my_custom_layer(x)
The following line works:
my_custom_layer.output
# Out: <KerasTensor: shape=(None, None, None, 64) dtype=float32 (created by layer 'my_custom_layer')>
However, this breaks in Tensorflow 2.4.1:
my_custom_layer.conv_1.output
with the following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-6-74d9fc3b4fbb> in <module>
----> 1 my_custom_layer.conv_1.output
~/anaconda3/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in output(self)
2152 """
2153 if not self._inbound_nodes:
-> 2154 raise AttributeError('Layer ' + self.name + ' has no inbound nodes.')
2155 return self._get_node_attribute_at_index(0, 'output_tensors', 'output')
2156
AttributeError: Layer conv2d has no inbound nodes.
If I run the same code in Tensorflow 2.3.1 it works without any problem. I looked through the changelog, but I did not spot what could break it. What could be the reason?
GitHub Issue #48196: https://github.com/tensorflow/tensorflow/issues/48196
Upvotes: 1
Views: 1514
Reputation: 3
Here is a temporary fix to make it work (only tested on TF2.4), you can disable the use of KerasTensor with the following command:
from tensorflow.python.keras.engine import keras_tensor
keras_tensor.disable_keras_tensors()
# .... Rest of your code
Using your example, you can have access to inner component:
my_custom_layer.conv_1.output
# <tf.Tensor 'my_custom_layer_2/conv2d_2/BiasAdd:0' shape=(None, None, None, 64) dtype=float32>
Upvotes: 0
Reputation: 17239
I've also faced similar issue. Now I've also tested your code with tf 2.0
, 2.1
, 2.2
, 2.3
in both CPU and GPU mode and it's working. But it breaks on tf 2.4
and also tf-nightly 2.6.0-dev20210330
. It seems like a bug. With tf < 2.4
, the my_custom_layer.conv_1.inbound_nodes
returns
[<tensorflow.python.keras.engine.node.Node at 0x7f751f301bd0>]
unlike empty []
in tf > = 2.4
.
Upvotes: 3