Colab can not connect to GPU from a python file

I am trying to run a github deep learning repository in Colab but I can not connect the python files to colab GPU.

I can connect to GPU when writing a script in the colab notebook e.g. when I run this cod from a notebook cell :

import os, torch
print('Torch', torch.__version__, 'CUDA', torch.version.cuda)
print('Device:', torch.device('cuda:0'))
print(torch.cuda.is_available())  

I get:

Torch 1.4.0 CUDA 10.1
Device: cuda:0
True

but when I run it from a file called myExample.py e.g. using

!python myExample.py

I get:

Torch 1.4.0 CUDA 10.1
Device: cuda:0
False

Is there any solution to this?

Topic colab torch learning

Category Data Science


Why dont you import the file into notebook and run your function.

Eg: if your example.py has something like this

if __name__ == '__main__':
    myfunction()

Then in your colab,

import os
os.chdir("path/to/cloned/dir/")
import example
example.myfunction()

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.