2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Python] [R] tensorflow-2.xでGPUを認識させる

Posted at

環境

  • Ubuntu 18.04 LTS
  • GeForce GTX 1050
  • NVIDIA driver version: 435.21
  • conda 4.8.3
  • R version 3.5.3

tensorflow-2.xでは一発でGPUが認識されました

conda create -y -n tf2 -c anaconda tensorflow-gpu
conda activate tf2
python -c '
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]="3" # Tensorflowのログメッセージをオフにします
import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices("GPU")))'

# Num GPUs Available:  1

GPUが認識されました!:smiley:

パッケージを確認してみます。

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がインストールされなかったのですが、
いまは一発で全てインストールされています:clap:

Rでも同じです

# Rのインストール
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)

# tf2の環境が呼び出されているか確認します: 
print(reticulate::py_config()[1])

# GPUが認識されているか確認します。
Sys.setenv(TF_CPP_MIN_LOG_LEVEL = 3) # Tensorflowのログメッセージをオフにします
tf <- import("tensorflow")

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

Rscript r_env.R

# 実行結果
# $python
# [1] "XXX/anaconda3/envs/tf2/bin/python"

# [1] "Num GPUs Available: 1"

Rでも同様にGPUが認識されました!
これでGPUを使ってゴリゴリ計算を回すことができますね!

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?