float(x) does not work if x is a concrete tracer, but int(x) works
See original GitHub issueI 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:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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?
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).
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 😃