4

I want to train a model with a custom loss function, in order to do that, I need to convert the tensor to numpy array inside the method below:

def median_loss_estimation(y_true, y_predicted):
    a = y_predicted.numpy()

but I have this error:

AttributeError: 'Tensor' object has no attribute 'numpy'

Why? How can I convert the tensor to a numpy array?

2 Answers 2

5

The answer is: put run_eagerly=True in model.compile!

Sign up to request clarification or add additional context in comments.

Comments

1

You're doing the right thing, only Tensorflow 2.1 is currently broken in that aspect. This would normally happen if you run the code without eager mode enabled. However, Tensorflow 2 by default runs in eager mode... or at least it should. The issue is tracked here.

There are at least two solutions to this:

  1. Install the latest nightly build.
  2. Set model.run_eagerly = True.

3 Comments

using model.run_eagerly = True the error remains!
@FrancescoScala Try then the first option,
tried: tf 2.0.1, 2.1.0, 2.2.0rc2, tb-nightly (2.3.0something) and I got every time the same error!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.