[Python] [R] Make GPU recognized by tensorflow-2.x

environment

GPU was recognized in one shot in tensorflow-2.x

conda create -y -n tf2 -c anaconda tensorflow-gpu
conda activate tf2
python -c '
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]="3" #Turn off Tensorflow log messages
import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices("GPU")))'

# Num GPUs Available:  1

GPU recognized! : smiley:

Let's check the package.

conda list -n tf2 | grep -e python -e cud -e tensorflow

# cudatoolkit               10.1.243             h6bb024c_0    anaconda
# cudnn                     7.6.5                cuda10.1_0    anaconda
# python                    3.7.7           hcf32534_0_cpython    anaconda
# tensorflow                2.1.0           gpu_py37h7a4bb67_0    anaconda
# tensorflow-base           2.1.0           gpu_py37h6c5654b_0    anaconda
# tensorflow-estimator      2.1.0              pyhd54b08b_0    anaconda
# tensorflow-gpu            2.1.0                h0d30ee6_0    anaconda

CUDA, CuDNN was not installed by this time last year Now it's all installed in one shot: clap:

Same for R

#R installation
conda install -y -n tf2 r-essentials r-base

cat << 'EOF' > r_env.R
options(repos = "https://cran.rstudio.com")
if (!requireNamespace("reticulate", quietly = T)) install.packages("reticulate")
reticulate::use_condaenv("tf2")
library(reticulate)

#Check if the tf2 environment is called: 
print(reticulate::py_config()[1])

#Check if the GPU is recognized.
Sys.setenv(TF_CPP_MIN_LOG_LEVEL = 3) #Turn off Tensorflow log messages
tf <- import("tensorflow")

print(sprintf("Num GPUs Available: %d", 
length(tf$config$experimental$list_physical_devices("GPU"))))
EOF

Rscript r_env.R

#Execution result
# $python
# [1] "XXX/anaconda3/envs/tf2/bin/python"

# [1] "Num GPUs Available: 1"

GPU was recognized in R as well! Now you can use the GPU to spin the gorigori calculations!

Recommended Posts

[Python] [R] Make GPU recognized by tensorflow-2.x
Make Python dict accessible by Attribute
62-decimal <-> decimal conversion scripts by language (R, Python, Java)
Make a joyplot-like plot of R in python
Primality test by Python
Visualization memo by Python
Communication processing by Python
Beamformer response by python
[Python] How to make a list of character strings character by character