Thiết lập Môi Trường Học Sâu trên Windows và Linux

1. Thiết lập Môi Trường Học Sâu trên Windows

1.1 Cài đặt Anaconda

Anaconda là một bản phân phối Python dành cho tính toán khoa học, hỗ trợ các hệ điều hành Linux, Mac, và Windows, cung cấp chức năng quản lý gói và môi trường. Anaconda bao gồm conda, phiên bản Python cụ thể, và nhiều gói phần mềm khác.

  • Tải về từ gương của Đại học Khoa học và Công nghệ Trung Quốc:
  • Chọn đường dẫn archive để tải Anaconda3-4.2.0-Windows-x86_64.exe
  • Đường dẫn cài đặt: D:\Program Files (x86)\Anaconda

1.2 Cài đặt MinGW và libpython

Mở Anaconda Prompt và nhập lệnh sau:

conda install mingw libpython

Nếu quá trình cài đặt chậm hoặc gặp lỗi, tham khảo: Hướng dẫn cài đặt Theano + CUDA:

Cấu hình biến môi trường:

  • Mở Tính toán -> Tính chất -> Cài đặt hệ thống nâng cao -> Biến môi trường
  • Chỉnh sửa biến hệ thống path và thêm: D:\Program Files (x86)\Anaconda\MinGW\x86_64-w64-mingw32\lib;D:\Program Files (x86)\Anaconda\MinGW\bin;

1.3 Cài đặt Theano

Trong Anaconda Prompt, nhập lệnh:

conda install theano pygpu

Nếu cài đặt thất bại, hãy gỡ cài đặt và thử lại:

conda uninstall theano
conda install theano

Tạo biến môi trường PYTHONPATH và thêm giá trị: D:\Program Files (x86)\Anaconda\Lib\site-packages\theano;

Nếu máy tính không có GPU, dừng ở đây.

1.4 Cài đặt PTVS và Cấu hình Biến Môi Trường VS

Cài đặt PTVS (Python Tools for Visual Studio) cho Visual Studio:

  • Tham khảo: Cài đặt Hỗ trợ Python trong Visual Studio trên Windows
  • Nếu bạn đang sử dụng Visual Studio 2013, cần cài đặt Python 3.5. Tham khảo: Cách cập nhật Anaconda đến phiên bản Python mong muốn

Đối với Visual Studio 2013, cài đặt Python 3.5:

conda update conda
conda create --name python35 python=3.5
activate python35
python --version

Đảm bảo cài đặt MinGW, libpython, và Theano cho môi trường Python 3.5.

1.5 Cấu hình Tệp Đường Dẫn

Tạo tệp .theanorc.txt tại C:\Users\Tên người dùng và thêm nội dung sau:

[global]
openmp=False

[blas]
ldflags=

[nvcc]
fastmath=True
flags=-LD:\Program Files (x86)\Anaconda\libs
compiler_bindir=C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin

[gcc]
cxxflags=-ID:\Program Files (x86)\Anaconda\MinGW

Chỉnh sửa đường dẫn theo cấu hình của bạn.

1.6 Kiểm tra Cấu hình

  • Kiểm tra bằng cách chạy lệnh sau trong shell Python:
>>> import theano
>>> theano.test()
  • Nếu gặp lỗi, tham khảo: Lỗi khi cài đặt Theano: ImportError: cannot import name 'configparser'

  • Kiểm tra bằng đoạn mã sau:

import numpy as np
import time
import theano
A = np.random.rand(1000, 10000).astype(theano.config.floatX)
B = np.random.rand(10000, 1000).astype(theano.config.floatX)
np_start = time.time()
AB = A.dot(B)
np_end = time.time()
X, Y = theano.tensor.matrices('XY')
mf = theano.function([X, Y], X.dot(Y))
t_start = time.time()
tAB = mf(A, B)
t_end = time.time()
print("NP time: %f[s], theano time: %f[s] (times should be close when run on CPU!)" % (np_end - np_start, t_end - t_start))
print("Result difference: %f" % (np.abs(AB - tAB).max(), ))

1.7 Cài đặt CUDA

  • Kiểm tra phiên bản CUDA tương thích với card đồ họa của bạn.
  • Tải và cài đặt CUDA từ trang web chính thức của NVIDIA.
  • Tải và cài đặt cuDNN từ trang web của NVIDIA.

1.8 Kiểm tra Hoàn Chỉnh

Chạy đoạn mã sau để kiểm tra:

from theano import function, config, shared, tensor
import numpy
import time

vlen = 10 * 30 * 768
iters = 1000

rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], tensor.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(iters):
    r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, tensor.Elemwise) and
              ('Gpu' not in type(x.op).__name__)
              for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')

1.9 Cài đặt TensorFlow

  • Đối với CPU: conda install tensorflow
  • Đối với GPU: pip install --upgrade https://mirrors.tuna.tsinghua.edu.cn/tensorflow/windows/gpu/tensorflow_gpu-1.3.0rc0-cp35-cp35m-win_amd64.whl

Kiểm tra TensorFlow:

import tensorflow as tf
print(tf.test.gpu_device_name())
print(tf.test.is_gpu_available())

2. Thiết lập Môi Trường Học Sâu trên Linux

2.1 Cài đặt Anaconda

  • Tải về từ gương của Đại học Khoa học và Công nghệ Trung Quốc:
  • Chọn đường dẫn archive để tải Anaconda3-5.0.1-linux-86_64.sh
  • Cài đặt: bash Anaconda3-5.0.1-Linux-x86_64.sh

2.2 Cài đặt Driver NVIDIA

  • Cập nhật hệ thống: sudo apt-get update && sudo apt-get upgrade
  • Cài đặt driver NVIDIA: sudo add-apt-repository ppa:graphics-drivers/ppa && sudo apt-get update && sudo apt-get install nvidia-390
  • Khởi động lại hệ thống: sudo shutdown -r now
  • Kiểm tra driver: nvidia-smi

2.3 Cài đặt CUDA

  • Tải và cài đặt CUDA từ trang web chính thức của NVIDIA.
  • Cấu hình biến môi trường: export PATH=/usr/local/cuda/bin:$PATH && export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

2.4 Kiểm tra Cài đặt CUDA (Tùy chọn)

  • Biên dịch và chạy ví dụ CUDA: /usr/local/cuda/bin/cuda-install-samples-8.0.sh ~/cuda-samples && cd ~/cuda-samples/NVIDIA*Samples && make -j $(($(nproc) + 1)) && bin/x86_64/linux/release/deviceQuery

2.5 Cài đặt cuDNN

  • Tải cuDNN từ trang web của NVIDIA.
  • Sao chép các tệp cuDNN vào thư mục CUDA: tar -zxvf cudnn-8.0-linux-x64-v7.1.tgz && sudo cp cuda/include/cudnn.h /usr/local/cuda/include/ && sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/ && sudo chmod a+r /usr/local/cuda/include/cudnn.h && sudo chmod a+r /usr/local/cuda/lib64/libcudnn* && sudo ldconfig /usr/local/cuda/lib64

2.6 Cài đặt Theano

  • Cài đặt Theano: conda install theano-gpu && conda install mkl=2017

2.7 Cấu hình Tệp .theanorc

  • Tạo tệp .theanorc tại ~/.theanorc và thêm nội dung sau:
[global]
floatX=float32
device=cuda
[cuda]
root=/usr/local/cuda-8.0
[nvcc]
flags=-D_FORCE_INLINES
fastmath=True

2.8 Kiểm tra Cài đặt

  • Chạy đoạn mã sau để kiểm tra:
from theano import function, config, shared, tensor
import numpy
import time

vlen = 10 * 30 * 768
iters = 1000

rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], tensor.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(iters):
    r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, tensor.Elemwise) and
              ('Gpu' not in type(x.op).__name__)
              for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')

2.9 Cài đặt TensorFlow

  • Cài đặt TensorFlow: conda install tensorflow && conda install tensorflow-gpu
  • Kiểm tra TensorFlow: import tensorflow as tf && print(tf.test.gpu_device_name()) && print(tf.test.is_gpu_available())

Thẻ: anaconda theano tensorflow CUDA cudnn

Đăng vào ngày 30 tháng 7 lúc 01:56