Ask Your Question

Using Sage with TensorFlow

asked 2016-03-09 06:04:12 -0500

zqcm gravatar image

I've written something in TensorFlow that makes use of some nice group theory functions that work very easily in Sage (and seem prohibitively difficult to code from scratch). However, I can't get TensorFlow and Sage to work together. Each works on its own, but I think they rely on different Python versions and therefore won't run together. I think Sage uses Python 2.6 and TensorFlow 2.7.

Specifically, I can make a small Python script that uses some Sage functions and run it using

sage --python

and it runs with no problem. But trying to import the TensorFlow module in throws an error saying the tensorflow module doesn't exist. Similarly, I get errors trying to import sage.all inside my .py script that uses TensorFlow. So I can neither add TensorFlow to Sage nor add Sage to Tensorflow.

I first encountered this problem in Sage 6.10 and upgrading to Sage 7.0 hasn't helped.

I'm not sure if this is relevant, but if I fire up normal Python (the kind TensorFlow uses), I get this:

from sage.env import SAGE_LOCAL


which outputs '$SAGE_ROOT/local'.

However if I fire up Sage first I get this:


which outputs ''/usr/lib/sagemath/local'.

Any possible workaround?


edit retag flag offensive close merge delete


slelievre gravatar imageslelievre ( 2016-03-09 08:35:28 -0500 )edit

Note: to display code blocks, indent them by 4 spaces. You can also select full lines and click the "code" button, which is the button with '101 010'.

slelievre gravatar imageslelievre ( 2016-03-09 08:39:44 -0500 )edit

The Python version shouldn't be the problem:

$ sage -ipython
Python 2.7.9 (default, Oct 13 2015, 12:06:06) 
Type "copyright", "credits" or "license" for more information.
kcrisman gravatar imagekcrisman ( 2016-03-09 08:55:27 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-03-09 09:08:57 -0500

kcrisman gravatar image

Here's something NOT to do (yet):

$ sage -pip install

Even though this "works", it also had several worrying messages about upgrading numpy and six, which completely broke the numpy part of my Sage installation. This was with Sage-6.9.

Which means you have to make sure you have a Sage that has the right versions of Numpy and six. With the latest development version, we do, apparently:

$ ./sage -pip install
Collecting tensorflow==0.7.1 from
  Using cached
Requirement already satisfied (use --upgrade to upgrade): six>=1.10.0 in ./local/lib/python2.7/site-packages/six-1.10.0-py2.7.egg (from tensorflow==0.7.1)
Collecting protobuf==3.0.0b2 (from tensorflow==0.7.1)
  Using cached protobuf-3.0.0b2-py2.py3-none-any.whl
Collecting wheel (from tensorflow==0.7.1)
  Using cached wheel-0.29.0-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.10.1 in ./local/lib/python2.7/site-packages (from tensorflow==0.7.1)
Requirement already satisfied (use --upgrade to upgrade): setuptools in ./local/lib/python2.7/site-packages/setuptools-20.1.1-py2.7.egg (from protobuf==3.0.0b2->tensorflow==0.7.1)
Installing collected packages: protobuf, wheel, tensorflow
Successfully installed protobuf-3.0.0b2 tensorflow-0.7.1 wheel-0.29.0
You are using pip version 8.0.2, however version 8.1.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

And then I don't get any failures.

So one has to be careful, but apparently it's possible. However, you definitely have to use it from "within" Sage; Sage-as-distribution wouldn't allow you to use your other tensorflow with it.

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: 2016-03-09 06:04:12 -0500

Seen: 415 times

Last updated: Mar 09 '16