I am trying to compile a Keras Sequential model (in TF2) in the eager execution mode.
Following is my custom layer:
class CustomLayer(Layer):
def __init__(self, output_shape, **kwargs):
self.output_shape = output_shape
super(CustomLayer, self).__init__(**kwargs)
def build(self, input_shape):
assert len(input_shape) == 2
input_dim = input_shape[1]
def call(self, inputs, mask=None, **kwargs):
y_pred = inputs.numpy() #<---- raise error
return y_pred
I use this layer to extend another Sequential Model as follow:
encoder = Sequential(encoders) # encoders is a bunch of Dense layers
encoder.compile(
loss='mse',
optimizer=SGD(lr=self.learning_rate, decay=0, momentum=0.9),
run_eagerly=True
)
self.MyModel = Sequential([encoder, CustomLayer(output_shape=output_shape)])
self.MyModel.compile(
loss='MSE',
optimizer=sgd,
run_eagerly=True
)
The original model is an autoencoder with denoising layers, and I'm adding a new layer after the bottleneck to make some customized predictions. I need to have access to the tensor values within the new layer. Doing so raises the following error:
Traceback (most recent call last):
File "main.py", line 185, in initialize
self.DEC = Sequential([encoder, CustomingLayer(input_shape=input_shape,
File ".virtualenvs/vision/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py", line 530, in _method_wrapper
result = method(self, *args, **kwargs)
File ".virtualenvs/vision/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File ".virtualenvs/vision/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py", line 699, in wrapper
raise e.ag_error_metadata.to_exception(e)
AttributeError: Exception encountered when calling layer "custom_layer" (type CustomLayer).
in user code:
File "custom_layer.py", line 25, in call *
y_pred = inputs.numpy()
AttributeError: 'Tensor' object has no attribute 'numpy'
Call arguments received:
• inputs=tf.Tensor(shape=(None, 10), dtype=float32)
• mask=None
• kwargs={'training': 'None'}
inputs.numpy()?