Ask Your Question

Stop Sage from overriding Python number types

asked 2019-10-20 18:37:06 +0200

anonymous user


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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-10-20 21:25:38 +0200

tmonteil gravatar image

There 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'>
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-10-20 18:37:06 +0200

Seen: 522 times

Last updated: Oct 20 '19