11

I've successfully installed tensorflow with pip install tensorflow and that all works as expected.

I can also successfully install tensorflow-gpu with pip install tensorflow-gpu but I can't import it in my python script:

import tensorflow-gpu

File "<stdin>", line 1
import tensorflow-gpu
                 ^
SyntaxError: invalid syntax

i've installed CUDA v9.0 and run windows 10

Cadoiz
  • 1,446
  • 21
  • 31
Seppukki
  • 563
  • 2
  • 8
  • 24
  • Possible duplicate of https://stackoverflow.com/questions/48060769/python-cannot-import-tensorflow-gpu – amanb Dec 16 '18 at 11:44
  • 1
    You do not need to import tensorflow-gpu , if you installed it, `cuda` and `cudnn` properly, you will be able to acces the gpu from tensorflow. – Dinari Dec 16 '18 at 11:45
  • But how can i make sure that tensorflow uses my gpu? And do you mean that if i import tensorflow in my script is automatically uses my gpu? – Seppukki Dec 16 '18 at 11:46
  • https://stackoverflow.com/questions/38559755/how-to-get-current-available-gpus-in-tensorflow To see which GPU's tf knows, unless defined explicitly, it will use GPU (if possible). – Dinari Dec 16 '18 at 11:50
  • my gpu doesn't appear when i execute `device_lib.list_local_devices()` i it possible it doesn't find my gpu because i'm in an virtual environment? – Seppukki Dec 16 '18 at 11:53

2 Answers2

4

The package on pypi is called tensorflow-gpu but you just import it with "tensorflow"

 import tensorflow as tf
MJK
  • 1,381
  • 3
  • 15
  • 22
  • You could check if your GPU is being used first. If your GFX card is from NVidia, Tensorflow will use your GPU. If you have an AMD card, you will have check if your card is supported by `PlaidML`. If it is, install `PlaidML`, tell TF to use it and off you go. Check GPU usage Windows 10: https://www.windowscentral.com/how-track-gpu-performance-data-windows-10 PlaidML: https://github.com/plaidml/plaidml – pookie Dec 16 '18 at 11:53
  • It has both GPU and CPU implementation. But the GPU devices will be given priority when the operation is assigned to a device. To find out which device is used, you can enable log device placement like this - `sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))` – MJK Dec 16 '18 at 11:57
  • my GFX is from NVidia but it doesn't use my gpu (i have a gtx1050) and whatever is stored in "sess" is `>>> sess ` – Seppukki Dec 16 '18 at 12:12
4

To ensure that the tensorflow package is using your GPU, do this:

import tensorflow as tf

sess = tf.Session()

See the output on the console, if it shows your GPU information on creation of the session as shown below.

Notice "GeForce 940MX" in the information. Also note that Tensorflow will use a Nvidia GPU only if the compute capability score is above 3.5 . More about that here.

here

If it's not using the GPU, then it won't output GPU information, it'll just show something similar to this:

enter image description here

Rahul Bharadwaj
  • 2,555
  • 2
  • 18
  • 29
  • I have the second output, can that be because i'm running tf in a virtual environment? – Seppukki Dec 16 '18 at 11:57
  • no, it means you're using the GPU version instead. I'd suggest uninstalling tensorflow by doing `pip uninstall tensorflow` and trying again. – Rahul Bharadwaj Dec 16 '18 at 12:12
  • wait... which one must i delete and install again? i'll just delete them both and install the normal tensorflow again. – Seppukki Dec 16 '18 at 12:15
  • Do this: `pip uninstall tensorflow`, I think you have both CPU and GPU versions installed and thus the confusion. If you do `pip uninstall tensorflow-gpu` it uninstalls tensorflow GPU. If you do `pip uninstall tensorflow` it uninstalls tensorflow CPU. – Rahul Bharadwaj Dec 16 '18 at 12:19
  • yeah now i've tried with the normal one and nothing works, i'm now trying with the gpu one – Seppukki Dec 16 '18 at 12:23
  • ok it seems that the gpu one doenst work if you don't have the normal one installed, now i've installed them both and still get the second output when i execute `sess = tf.Session()` – Seppukki Dec 16 '18 at 12:29
  • Hmm, that's weird. Could you try this : https://stackoverflow.com/a/50538927/6400614 – Rahul Bharadwaj Dec 16 '18 at 13:01