question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

float(x) does not work if x is a concrete tracer, but int(x) works

See original GitHub issue

I expected the following code to work:

 def func(x):
    return x if float(x) >= 0. else -x
 print(api.grad(func)(3.))

because int(x) works. Instead that code gives an error

TypeError: JAX Tracer object cannot be interpreted as a float. Try using `x.astype(float)` instead.

Using x.astype(float) works, but I am wondering if it would not be more uniform to support float(x) as well.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
gneculacommented, Apr 22, 2020

Thanks for explaining. I do think that this is a subtle issue, I do not know if people run into it in practice, or just when poking curiously at JAX. Do you think it is worth an entry in the FAQ?

WDYT? In particular, do you think we should make float(x) behave exactly like x.astype(‘float’), at least on Tracer instances?

I think that x.astype(float) will produce a tracer if x is a tracer, which would not make sense as the result of float(x).

0reactions
mattjjcommented, Apr 22, 2020

Hmm hard to say: what’s the bar for adding to the FAQ? We could just wait until it’s asked before adding it.

(I just remembered, based on a JAXers thread and a realization just now, that the original reason for adding the __float__ override may have been for printing with %f.)

I defer to your judgement on the FAQ, though my own bias is towards inaction 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

JAX Frequently Asked Questions (FAQ)
Another example is when explicitly casting a concrete tracer value to a regular type, e.g., int(x) or x.astype(float) . Another such situation is...
Read more >
i am doing a triangle identifier and my problem is that i cant ...
This line: if str(x) == x and int(x) != x and float(x) != x: does not actually turn x into either an int...
Read more >
Better performance with tf.function | TensorFlow Core
This guide will help you conceptualize how tf.function works under the hood, ... Graph and wraps it in a ConcreteFunction , also known...
Read more >
MotionEvent - Android Developers
Returns a historical X coordinate, as per getX(int) , that occurred between ... You should treat this as an up event, but not...
Read more >
Groovy Language Documentation
String if there's no interpolated expression, but are groovy.lang. ... toUpperCase() } (1) def doSomething(Integer x) { 2*x } (2) def reference =...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found