f0nzie / rTorch.old

PyTorch bindings for R
https://f0nzie.github.io/rTorch/
Other
78 stars 12 forks source link

How do I install a specific PyTorch version in rTorch #4

Open f0nzie opened 4 years ago

f0nzie commented 4 years ago

From the R prompt in RStudio.

This will install the latest stable PyTorch for Python 3.6:

rTorch:::install_conda(package="pytorch", envname="r-torch", conda="auto", conda_python_version = "3.6", pip=FALSE, channel="pytorch", extra_packages=c("torchvision", "cpuonly", "matplotlib", "pandas"))

This will install PyTorch 1.2 under Python 3.7:

rTorch:::install_conda(package="pytorch=1.2", envname="r-torch", conda="auto", conda_python_version = "3.7", pip=FALSE, channel="pytorch", extra_packages=c("torchvision", "cpuonly", "matplotlib", "pandas"))

This will install PyTorch 1.5 under Python 3.8:

rTorch:::install_conda(package="pytorch=1.5", envname="r-torch", conda="auto", conda_python_version = "3.8", pip=FALSE, channel="pytorch", extra_packages=c("torchvision", "cpuonly", "matplotlib", "pandas", "bokeh"))

These commands have been tested in:

beew commented 4 years ago

Hi, I wonder if there is a way for rTorch to choose from among python and pytorch versions already installed in the system without conda. What I mean is like tenorflow-r, where you can use the function use_python('path/to/python/binary") or Sys.setenv(RETICULATE_PYTHON="path/to/python/binary") to choose from the python versions already installed in the system without needing to create a conda along with all the extra junks that it pulls in.

I have 4 pythons on my system. python2.7 and python3.5.4 which come with the system and I don't use for developmental purpose. I have python-3.7.8 and 3.8.5 installed in local folders for development (each with its own configuration script) and I have tested tensorflow-r using the use_python function and the RETICULATE_PYTHON environmental variable described above. Is it possible to do something similar with rTorch?

OS is Ubuntu Linux.

beew commented 4 years ago

OK, it works just like r-tensorflow. use_python() will do the job

f0nzie commented 4 years ago

I borrowed functions from reticulate and tensorflow for the PyTorch installation under Python. I adapted those functions for PyTorch. They worked fine until PyTorch 1.2. Then, PyTorch introduced changes that made it a little bit different. So, my function install_pytorch() stopped working. But the lower level functions do work. Such as rTorch:::install_conda(). I am working these days on a newer version that takes changes in PyTorch 1.6. If you want to try, all is the develop branch.

beew commented 4 years ago

Actually I just tested with PyTorch-1.4 and it works.

beew commented 4 years ago

Actually it works for PyTorch 1.6.0 as well (I have several pythons installed locally) , except for a small issue with reticulate https://github.com/rstudio/reticulate/issues/836#issuecomment-683352476

R version 4.0.2 (2020-06-22) -- "Taking Off Again"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> Sys.setenv(LD_LIBRARY_PATH = paste("/home/bee/opt/python385/lib", Sys.getenv("LD_LIBRARY_PATH"), sep = ":"))
> library(rTorch)
> use_python("/home/bee/opt/python385/bin/python")
> torch_config()
PyTorch v1.6.0 (~/opt/python385/lib/python3.8/site-packages/torch)

Python v3.8 (~/opt/python385/bin/python)

> bt <- torch$ByteTensor(3L, 3L)
> ft <- torch$FloatTensor(3L, 3L)
> dt <- torch$DoubleTensor(3L, 3L
+ )
> lt <- torch$LongTensor(3L, 3L)
> Bt <- torch$BoolTensor(5L, 5L)
> ft
tensor([[1.2019e+36, 4.5758e-41, 1.2019e+36],
        [4.5758e-41, 0.0000e+00, 0.0000e+00],
        [0.0000e+00, 0.0000e+00, 0.0000e+00]])
> Bt
tensor([[ True,  True,  True,  True,  True],
        [ True, False, False,  True,  True],
        [ True,  True,  True,  True, False],
        [False,  True, False, False, False],
        [False, False, False, False,  True]])
> 
f0nzie commented 4 years ago

Awesome!