Home > front end >  AttributeError: 'Tensor' object has no attribute 'numpy' while extending keras s
AttributeError: 'Tensor' object has no attribute 'numpy' while extending keras s

Time:03-02

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'}

CodePudding user response:

The direct using of this numpy function is impossible - as it's neither implemented in Tensorflow nor in Theano. Moreover, there is no direct correspondence between tensors and arrays. Tensors should be understood as algebraic variables whereas numpy arrays as numbers. Tensor is an abstract thing and applying a numpy function to it is usually impossible.

But you could still try to re-implement your function on your own using keras.backend. Then you'll use the valid tensor operations and no problem would be raised.

Another way to tackle your problem would be to use tf.numpy_function, see the documentation, this allows you to use numpy functions but there are some limitations.

  • Related