Tensorflow 2.0의 변화

  • tf.compat.v1을 통해 2.0 버전에서도 1.x 버전의 API를 사용할 수 있다.
  • tf_upgrade_v2로 1.x 버전의 파이썬 코드를 2.0 버전으로 자동 변환할 수 있다. 하지만 일부 제대로 변환되지 않아 사용자가 직접 제거 또는 수정해야하는 경우도 존재하니 유의하기 바란다. (tf.nn.conv2d()use_cudnn_on_gpu 인자 등)
  • 일관성 없는 1.x버전의 API들을 개선하기 위하여, 최적화 함수는 tf.optimizer에, loss 함수는 tf.losses에 모두 모았다.
  • 기존에는 그래프를 정의한 뒤 생성된 그래프를 이용하여 계산을 수행하였으나, 2.0버전부터는 그래프를 생성하지 않고도 계산을 할 수 있다. 이를 eager execution(즉시 실행 모드)이라 한다. 1.x버전에서 이를 수행하기 위해서는 tf.enable_eager_execution()을 통해 활성화 해야한다. 이러한 즉시 실행모드는 편리하지만 그래프에 비해 연산 속도가 비교적 느림에 주의하자.
  • @tf.function을 이용해 일반 연산을 그래프 연산으로 변환할 수 있다. 이는 1.x 버전의 tf.Session과 동일하다.
  • 1.4 버전부터 지원된 텐서플로 API인 keras가 2.0 버전부터는 텐서플로의 유일한 고수준 API로써 제공된다. 이를 통해 간결한 프로그래밍을 가능케한다.

텐서플로 2.0 설치 (Ubuntu)

Python3 설치

: 다음 명령어를 통해 Python3와 관련된 툴들을 설치한다.

apt-get update
apt-get install python3 python3-dev python3-pip python3-venv
apt-get install build-essential

...

jay@:~/tutorial $ python3 --version
Python 3.8.5
jay@:~/tutorial $ pip3 --version
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

: 다음 명령으로 가상환경을 만들고 활성화 시킨다.

python3 -m venv --system-site-packages ~/venv # 새로운 가상환경을 만든다.(경로는 임의 설정)

source ~/venv/bin/activate # 가상 환경을 활성화 한다 (sh, bash or zsh의 경우)
. ~/venv/bin/activate.fish # 가상 환경을 활성화 한다 (fish의 경우)
source ~/venv/bin/activate.csh # 가상 환경을 활성화 한다 (csh or tcsh의 경우)

# 가상환경이 활성화 되었다. 프롬프트에는 (venv) 가 보일 것이다.
# 가상환경을 종료하고 싶다면 deactivate 를 수행한다.

Tensorflow 및 CUDA 설치

: 다음 명령으로 가상 환경 내에 tesnorflow를 설치 및 정상 설치여부를 확인한다.

pip3 install --upgrade tensorflow

python3 -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

:다음과 같은 실행 결과가 보일 것이다. 아래의 Warning들은 적절한 cuda version이 없다는 내용이므로 무시해도 좋다. 하지만 GPU를 이용한 가속을 원한다면 https://medium.com/@stephengregory_69986/installing-cuda-10-1-on-ubuntu-20-04-e562a5e724a0 를 참고해 CUDA를 설치하길 바란다.
(사용중인 GPU의 갯수는 다음 명령어 python3 -c "import tensorflow as tf;print(len(tf.config.experimental.list_physical_devices('GPU')))" 로 확인할 수 있다.)

(venv) jay@:~/tutorial $ python3 -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
2020-12-06 16:45:18.518048: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory
2020-12-06 16:45:18.518152: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2020-12-06 16:45:19.110871: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2020-12-06 16:45:19.110971: W tensorflow/stream_executor/cuda/cuda_driver.cc:312] failed call to cuInit: UNKNOWN ERROR (303)
2020-12-06 16:45:19.111008: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (jay-VirtualBox): /proc/driver/nvidia/version does not exist
2020-12-06 16:45:19.111228: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-12-06 16:45:19.116012: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 3593250000 Hz
2020-12-06 16:45:19.116114: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x54fbc80 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-12-06 16:45:19.116150: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
tf.Tensor(-23.87439, shape=(), dtype=float32)

필자는 Virtual Box에서 tutorial 목적으로 사용중인데, Virtual Box에서는 GPU 사용이 안되는 듯 하다...ㅠ

참고링크

Tensorflow 설치 공식 가이드

+ Recent posts