2k1
  • Home
  • Programming
  • System
  • Design
  • Applications
  • Tech
No Result
View All Result
  • Login
2k1
  • Home
  • Programming
  • System
  • Design
  • Applications
  • Tech
No Result
View All Result
2k1
No Result
View All Result

Cách cài đặt TensorFlow GPU

Nguyen Pham by Nguyen Pham
16/05/2023
in Programming, AI, Computers, Python, System, Tech, Ubuntu, Windows
Reading Time: 5 mins read
A A
0

1. Giới thiệu

Mình đã cài đặt được tensorflow-gpu trên windows 10 và ubuntu 20.04:

Cấu hình hiện tại

CPU: Intel core I7 9750H
GPU: Nvidia gtx1050ti max-q

Để sử dụng được GPU trong tensorfow bạn cần cài rất nhiều thứ nhưng mình sẽ chỉ các bạn cách dễ nhất là dùng anaconda

2. Windows

Trong windows các bạn cài thư viện anaconda: https://docs.anaconda.com/anaconda/install/windows/

Sau đó các bạn chạy lệnh sau

conda create -n tf-gpu tensorflow-gpu
conda activate tf-gpu

Lệnh này sẽ tạo một môi trường có tên tf-gpu và cài các gói cần thiết

Để vào môi trường này:

activate tf-gpu

Các gói trong tf-gpu

conda list
# packages in environment at C:\Users\nguyen\.conda\envs\tf-gpu:
#
# Name                    Version                   Build  Channel
_tflow_select             2.3.0                       gpu  
absl-py                   0.12.0           py38haa95532_0  
aiohttp                   3.7.4            py38h2bbff1b_1  
astunparse                1.6.3                      py_0  
async-timeout             3.0.1            py38haa95532_0  
attrs                     20.3.0             pyhd3eb1b0_0  
autopep8                  1.5.6              pyhd3eb1b0_0  
blas                      1.0                         mkl  
blinker                   1.4              py38haa95532_0  
brotlipy                  0.7.0           py38h2bbff1b_1003  
ca-certificates           2021.4.13            haa95532_1  
cachetools                4.2.2              pyhd3eb1b0_0  
certifi                   2020.12.5        py38haa95532_0  
cffi                      1.14.5           py38hcd4344a_0  
chardet                   3.0.4           py38haa95532_1003  
click                     7.1.2              pyhd3eb1b0_0  
coverage                  5.5              py38h2bbff1b_2  
cryptography              3.4.7            py38h71e12ea_0  
cudatoolkit               10.1.243             h74a9793_0  
cudnn                     7.6.5                cuda10.1_0  
cython                    0.29.23          py38hd77b12b_0  
gast                      0.4.0                      py_0  
google-auth               1.30.0             pyhd3eb1b0_0  
google-auth-oauthlib      0.4.4              pyhd3eb1b0_0  
google-pasta              0.2.0                      py_0  
grpcio                    1.36.1           py38hc60d5dd_1  
h5py                      2.10.0           py38h5e291fa_0  
hdf5                      1.10.4               h7ebc959_0  
icc_rt                    2019.0.0             h0cc432a_1  
idna                      2.10               pyhd3eb1b0_0  
importlib-metadata        3.10.0           py38haa95532_0  
intel-openmp              2021.2.0           haa95532_616  
keras-applications        1.0.8                      py_1  
keras-preprocessing       1.1.2              pyhd3eb1b0_0  
libprotobuf               3.14.0               h23ce68f_0  
markdown                  3.3.4            py38haa95532_0  
mkl                       2021.2.0           haa95532_296  
mkl-service               2.3.0            py38h2bbff1b_1  
mkl_fft                   1.3.0            py38h277e83a_2  
mkl_random                1.2.1            py38hf11a4ad_2  
multidict                 5.1.0            py38h2bbff1b_2  
numpy                     1.20.1           py38h34a8a5c_0  
numpy-base                1.20.1           py38haf7ebc8_0  
oauthlib                  3.1.0                      py_0  
openssl                   1.1.1k               h2bbff1b_0  
opt_einsum                3.1.0                      py_0  
pip                       21.0.1           py38haa95532_0  
protobuf                  3.14.0           py38hd77b12b_1  
pyasn1                    0.4.8                      py_0  
pyasn1-modules            0.2.8                      py_0  
pycodestyle               2.7.0              pyhd3eb1b0_0  
pycparser                 2.20                       py_2  
pyjwt                     1.7.1                    py38_0  
pyopenssl                 20.0.1             pyhd3eb1b0_1  
pyreadline                2.1                      py38_1  
pysocks                   1.7.1            py38haa95532_0  
python                    3.8.8                hdbf39b2_5  
requests                  2.25.1             pyhd3eb1b0_0  
requests-oauthlib         1.3.0                      py_0  
rsa                       4.7.2              pyhd3eb1b0_1  
scipy                     1.6.2            py38h66253e8_1  
setuptools                52.0.0           py38haa95532_0  
six                       1.15.0           py38haa95532_0  
sqlite                    3.35.4               h2bbff1b_0  
tensorboard               2.4.0              pyhc547734_0  
tensorboard-plugin-wit    1.6.0                      py_0  
tensorflow                2.3.0           mkl_py38h1fcfbd6_0  
tensorflow-base           2.3.0           gpu_py38h7339f5a_0  
tensorflow-estimator      2.3.0              pyheb71bc4_0  
tensorflow-gpu            2.3.0                he13fc11_0  
termcolor                 1.1.0            py38haa95532_1  
toml                      0.10.2             pyhd3eb1b0_0  
typing-extensions         3.7.4.3              hd3eb1b0_0  
typing_extensions         3.7.4.3            pyh06a4308_0  
urllib3                   1.26.4             pyhd3eb1b0_0  
vc                        14.2                 h21ff451_1  
vs2015_runtime            14.27.29016          h5e58377_2  
werkzeug                  1.0.1              pyhd3eb1b0_0  
wheel                     0.36.2             pyhd3eb1b0_0  
win_inet_pton             1.1.0            py38haa95532_0  
wincertstore              0.2                      py38_0  
wrapt                     1.12.1           py38he774522_1  
yarl                      1.6.3            py38h2bbff1b_0  
zipp                      3.4.1              pyhd3eb1b0_0  
zlib                      1.2.11               h62dcd97_4  

Khi bạn chạy mà vẫn gặp lỗi không nhận GPU thì bạn có thể fix như sau:

Python 3.7: conda install tensorflow-gpu=2.3 tensorflow=2.3=mkl_py37h936c3e2_0
Python 3.8: conda install tensorflow-gpu=2.3 tensorflow=2.3=mkl_py38h1fcfbd6_0

Tham khảo từ:

  • https://github.com/ContinuumIO/anaconda-issues/issues/12194#issuecomment-751700156
  • https://stackoverflow.com/questions/65273118/why-is-tensorflow-not-recognizing-my-gpu-after-conda-install

3. Ubuntu

Ubuntu các bạn cũng cài anaconda và tensorflow như ở windows. các lệnh cũng tương tự.

Ngoài ra nếu muốn dùng nvidia docker để sử dụng GPU trong container thì các bạn cần cài thêm một số package nữa:

Link tham khảo và cài đặt:

  • https://github.com/NVIDIA/nvidia-docker
  • https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#docker
  • https://www.tensorflow.org/install/docker

4. Cài trực tiếp:

Cài trực tiếp cũng được nhưng việc cài đặt khá khó khăn vì mỗi phiên bản tensorflow lại tương thích riêng với một phiên bản cuda và cudnn. Mình đã thử bằng cách này nhưng vẫn bị lỗi và không nhận GPU.

Code test tensorflow

import timeit
import tensorflow as tf
# device_name = tf.test.gpu_device_name()
# # print(device_name)
# if device_name != '/device:GPU:0':
#   raise SystemError('GPU device not found')
# print('Found GPU at: {}'.format(device_name))



device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
  print(
      '\n\nThis error most likely means that this notebook is not '
      'configured to use a GPU.  Change this in Notebook Settings via the '
      'command palette (cmd/ctrl-shift-P) or the Edit menu.\n\n')
  raise SystemError('GPU device not found')


def cpu():
  with tf.device('/cpu:0'):
    random_image_cpu = tf.random.normal((100, 100, 100, 3))
    net_cpu = tf.keras.layers.Conv2D(32, 7)(random_image_cpu)
    return tf.math.reduce_sum(net_cpu)


def gpu():
  with tf.device('/device:GPU:0'):
    random_image_gpu = tf.random.normal((100, 100, 100, 3))
    net_gpu = tf.keras.layers.Conv2D(32, 7)(random_image_gpu)
    return tf.math.reduce_sum(net_gpu)


# We run each op once to warm up; see: https://stackoverflow.com/a/45067900
cpu()
gpu()

# Run the op several times.
print('Time (s) to convolve 32x7x7x3 filter over random 100x100x100x3 images '
      '(batch x height x width x channel). Sum of ten runs.')
print('CPU (s):')
cpu_time = timeit.timeit('cpu()', number=10, setup="from __main__ import cpu")
print(cpu_time)
print('GPU (s):')
gpu_time = timeit.timeit('gpu()', number=10, setup="from __main__ import gpu")
print(gpu_time)
print('GPU speedup over CPU: {}x'.format(int(cpu_time/gpu_time)))

Minh test bằng code trên và thầy trên linux hiêu năng của tensorflow tăng đáng kể. tỉ lể cpu/gpu

windows: x6
ubuntu : x30

Nếu máy bạn yếu hoặc không có GPU rời thì hãy dùng google colab

Previous Post

Cách thay đổi tần số quét của màn hình rời trên ubuntu

Next Post

Sử dụng Tailwind CSS trong NextJS

Related Posts

Xây dựng todo app với smartcontract
Go

Giới thiệu về đa luồng trong ngôn ngữ lập trình go có code minh họa.

by Nguyen Pham
29/06/2023
Giới thiệu Docker
Tech

Giới thiệu Docker

by Nguyen Pham
15/05/2023
Giới thiệu về ChatGPT
Tech

Giới thiệu về ChatGPT

by Nguyen Pham
16/05/2023
Programming

Lập trình rust cơ bản – vòng lặp

by Nguyen Pham
22/12/2022
Programming

Lập trình python cơ bản – Hello world

by Nguyen Pham
16/11/2022
Programming

Lập trình go cơ bản – Hello world

by Nguyen Pham
04/12/2022
Load More
Next Post

Sử dụng Tailwind CSS trong NextJS

Please login to join discussion
Stock

Phân tích mã cổ phiếu VCB

by Nguyen Pham
26/04/2025
0

Phân tích mã cổ phiếu VCB (Ngân hàng TMCP Ngoại thương Việt Nam - Vietcombank) 1. Tổng quan về Vietcombank...

Read more

Facebook, Instagram bất ngờ sập trên diện rộng, liên tục đăng xuất người dùng!

05/03/2024
Xây dựng todo app với smartcontract

Web3 là gì?

30/06/2023
Xây dựng todo app với smartcontract

Giới thiệu về đa luồng trong ngôn ngữ lập trình go có code minh họa.

29/06/2023

Chạy ứng dụng react native đầu tiên của bạn

29/06/2023

@2021 2k1.org [email protected]

No Result
View All Result
  • Home
  • Review
  • Applications
  • Computers
  • Gaming
  • Microsoft

© 2021 NData

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In