ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 20 Oct 2019 21:25:38 +0200Stop Sage from overriding Python number typeshttps://ask.sagemath.org/question/48442/stop-sage-from-overriding-python-number-types/ I'm trying to play around with machine learning inside of the SageMath environment, but since Sage implicitly casts ints, floats, etc. to Integers, RealNumbers, etc., I'm running into problems such as this one:
```
...
--> model.add(Dense(Integer(1), activation="softmax"))
...
ValueError: Can't convert Python sequence with mixed types to Tensor.
```
So far, I've gotten around this by explicitly casting, so my line of code now looks like this:
```
...
model.add(Dense(int(1), activation="softmax"))
...
```
but I would rather not do this every time I create a model. Is there a way to stop Sage from implicitly casting, and instead use the default Python types?Sun, 20 Oct 2019 18:37:06 +0200https://ask.sagemath.org/question/48442/stop-sage-from-overriding-python-number-types/Answer by tmonteil for <p>I'm trying to play around with machine learning inside of the SageMath environment, but since Sage implicitly casts ints, floats, etc. to Integers, RealNumbers, etc., I'm running into problems such as this one:</p>
<p><code>
...
--> model.add(Dense(Integer(1), activation="softmax"))
...
ValueError: Can't convert Python sequence with mixed types to Tensor.
</code></p>
<p>So far, I've gotten around this by explicitly casting, so my line of code now looks like this:</p>
<p><code>
...
model.add(Dense(int(1), activation="softmax"))
...
</code></p>
<p>but I would rather not do this every time I create a model. Is there a way to stop Sage from implicitly casting, and instead use the default Python types?</p>
https://ask.sagemath.org/question/48442/stop-sage-from-overriding-python-number-types/?answer=48444#post-id-48444There are at least two possilbilies:
disable the preparser with `preparser(False)`:
sage: type(1)
<type 'sage.rings.integer.Integer'>
sage: preparser(False)
sage: type(1)
<type 'int'>
or use `r` suffix to say that the numbers are raw:
sage: type(1)
<type 'sage.rings.integer.Integer'>
sage: type(1r)
<type 'int'>
sage: type(1.0r)
<type 'float'>
Sun, 20 Oct 2019 21:25:38 +0200https://ask.sagemath.org/question/48442/stop-sage-from-overriding-python-number-types/?answer=48444#post-id-48444