技術工具
- 工具首頁\
- TensorFlow/Dev-CPU
### 容器說明
本環境整合了 Uber Horovod + OpenMPI,這使得單或多節點多 CPU 模型訓練可以更為輕鬆的實現。
為了使 CPU 能夠快速地執行數值運算,或是減少 CPU 處理資料的耗時,此容器環境亦安裝了高效能的數值運算函式庫: Intel Math Kernel Library (Intel MKL)。此函式庫的整合,能夠讓 Scikit-learn (機器學習套件), NumPy/SciPy (數值/科學套件) 確保其計算性能可達最高水準。
本容器建置於2020年3月。
### 下載方式
請於終端機執行以下指令:
```bash
docker pull moeaidb/aigo:cpu-tf-20.03
```
### 使用範例
#### 使用範例 1: 於背景啟動 Jupyterlab 服務
掛載當前位置目錄 (```$PWD```) 至容器內部的 ```/workspace``` 資料夾,並且讓 Jupyterlab 服務監聽本機的 port ```9999```:
```bash
# 決定 Jupyterlab 該監聽本機的哪一個 port
host_port=9999
# 啟動容器並取得容器 ID
container_id=$(docker run --rm -d -p ${host_port}:8888 -v $PWD:/workspace moeaidb/aigo:cpu-tf-20.03) # 休息一會,靜待容器服務啟動
# 等待服務啟動
sleep 2.
# 擷取容器的 Jupyterlab token
notebook_token=$(docker logs ${container_id} 2>&1 | grep -nP "(LabApp.*token=).*" | cut -d"=" -f 2)
# 顯示連線至 Jupyterlab 服務的網址
printf "Open a browser and connect to:\n
http://[your_ip]:${host_port}/?token=${notebook_token}\n
"
```
輸入以上指令於終端機後,應該會顯示一個網址:
```
Open a browser and connect to:
http://[your_ip]:9999/?token=87f6f7ad1455b7dde323f8a570897d4bf9dace8659e0e9bd
```
這代表我們已經在容器內啟動了 Jupyterlab 服務。接著,請開啟瀏覽器,並貼上此網址,即可使用 Jupyterlab 來撰寫 Python 筆記本。
注意事項:
* 網址當中可見 ```token=87f6f7ad14...```,其中 ```87f6f7ad14...``` 是亂數產生的一串 token。由於 token 為隨機字串,因此您實際取得的 token 應和本範例不同。
* 需將 ```[your_ip]``` 更改為機器的 IP 位址。 若您於本地端使用,則 ```[your_ip]``` 應為 ```127.0.0.1```。
* 開啟 Jupyterlab 後,會自動進入 ```/workspace``` 資料夾。因為建立容器時已將本機當前目錄 ```${PWD}``` 掛載至容器內的 ```/workspace```,所以您應該會在 ```/workspace``` 內看到先前存放於本機 ```${PWD}``` 的檔案。
#### 使用範例 2: 利用容器環境執行 Python 腳本
```bash
# 建立一個測試腳本。此腳本將單純的匯入 TensorFlow 套件,並且印出其當前版本。
printf "import tensorflow as tf \
\nprint('TensorFlow version=', tf.__version__)" \
> check_tf_version.py
# 我們已建立一個位於 ${PWD} 的 Python 腳本。接著,我們試跑一個容器來執行它:
docker run -it --rm -v ${PWD}:/workspace \
moeaidb/aigo:cpu-tf-20.03 python3 check_tf_version.py
```
輸入以上指令於終端機後,應會顯示出 Docker 容器內部所安裝的 TensorFlow 版本,如下:
```
TensorFlow version = 2.1.0
```
若您能見到此訊息,則表示 Python 腳本已順利執行完畢。
#### 使用範例 3: 訓練神經網路 (使用兩個 CPU Process)
```bash
docker run -it --privileged --rm moeaidb/aigo:cpu-tf-20.03 horovodrun -np 2 python3 horovod_examples/tensorflow2_mnist.py
```
以上指令使用兩個 CPU Process 做運算。執行後,應出現類同於以下的輸出結果 :
```
[1,1]<stdout>:Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
[1,0]<stdout>:Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 1s 0us/step]<stdout>:[1,0]<stdout>:
11493376/11490434 [==============================] - 1s 0us/step]<stdout>:
[1,1]<stderr>:2020-03-06 14:31:38.898644: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2000015000 Hz
[1,1]<stderr>:2020-03-06 14:31:38.906897: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x5568efc63df0 initialized for platform Host (this does not guarantee that XLA will be used). Devices: [1,1]<stderr>:2020-03-06 14:31:38.906936: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
[1,1]<stderr>:2020-03-06 14:31:38.907137: I tensorflow/core/common_runtime/process_util.cc:147] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
[1,0]<stderr>:2020-03-06 14:31:38.978892: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2000015000 Hz
[1,0]<stderr>:2020-03-06 14:31:38.986523: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55f3d763ddf0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
[1,0]<stderr>:2020-03-06 14:31:38.986567: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
[1,0]<stderr>:2020-03-06 14:31:38.986800: I tensorflow/core/common_runtime/process_util.cc:147] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
[1,0]<stdout>:Step #0 Loss: 2.310934
[1,0]<stdout>:Step #10 Loss: 0.719196
[1,0]<stdout>:Step #20[1,0]<stdout>: [1,0]<stdout>:Loss: 0.369734
[1,0]<stdout>:Step #30[1,0]<stdout>: [1,0]<stdout>:Loss: 0.361713
[1,0]<stdout>:Step #40 Loss: 0.197321
[1,0]<stdout>:Step #50 Loss: 0.308889
...
```
備註: 需要有網路連線, 因為此範例將會透過網路下載 MNIST 資料集。
#### 使用範例 4: 顯示套件資訊
AIGO 容器內含一個小程式: ```versions_summary```。它可以讓您迅速的了解容器內安裝了哪些套件, 以及所安裝的套件是何種版本。 請於終端機執行以下指令:
```bash
docker run -it --privileged --rm moeaidb/aigo:cpu-tf-20.03 versions_summary
```
執行後,您應該會看到類同於以下的輸出結果:
```
System INFO:
Python v3.7.3
Installed Python3 Packages:
[Base]:
tensorflow v2.1.0
keras v2.3.1
horovod v0.19.0
mpi4py v3.0.3
numba v0.48.0
[Numerical]:
numexpr v2.7.1
numpy v1.18.1
scipy v1.4.1
[Data Science]:
sklearn v0.22.2.post1
pandas v1.0.1
matplotlib v3.2.0
seaborn v0.10.0
bokeh v1.4.0
jupyterlab v2.0.0
pyodbc v4.0.30
yacs v0.1.6
[NLP]:
[CV]:
```
### 套件資訊
| 套件/軟體/函式庫名稱 | 版本 | 套件說明 |
|:---------|:---------:|:---------|
| [TensorFlow](https://www.tensorflow.org) | 2.1.0 | An open source machine learning library for research and production. 由 Google 維護的,開源的 AI 模型開發框架。 |
| [Keras](https://keras.io) | 2.3.1 | Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano. Keras 是基於底層 AI 框架的高階 API。有了它,我們就可以便捷的去建立,訓練和使用 AI 模型。 |
| [Python](https://docs.python.org/3.7/whatsnew/changelog.html#python-3-7-3-final) | 3.7.5 | Python is powerful... and fast; plays well with others; runs everywhere; is friendly & easy to learn; is Open. 我們環境採用 Python 3.7,它於字串處理和檔案搜索方面較 Python3.6 快很多。 |
| [Horovod](https://github.com/horovod/horovod) | 0.19.0 | Distributed training framework for TensorFlow, Keras, PyTorch, and Apache MXNet. 使用 Uber Horovod 可簡易的將 AI 訓練利用多 GPU 做加速。 |
| [OpenMPI](https://www.open-mpi.org) | 4.0.2 | A High Performance Message Passing Library. (Required by Uber Horovod) OpenMPI 為 Uber Horovod 所需,可支持跨卡/跨伺服器節點的溝通。 |
| [Intel Math Kernel Library (Intel MKL)](https://software.intel.com/en-us/mkl) | 2020.0-088 | Intel® Math Kernel Library (Intel® MKL) optimizes code with minimal effort for future generations of Intel® processors. 針對 Intel CPU 做快速的數值運算。 |
| [NumPy](https://www.numpy.org) (Intel-MKL-acclerated) | 1.18.1 | NumPy is the fundamental package for scientific computing with Python. 常用的數值運算套件 (利用 Intel MKL 加速)。 |
| [SciPy](https://www.scipy.org) (Intel-MKL-acclerated) | 1.4.1 | SciPy (pronounced “Sigh Pie”) is a Python-based ecosystem of open-source software for mathematics, science, and engineering. 常用的科研套件,提供一些基礎算法,統計方法 (利用 Intel MKL 加速)。 |
| [Scikit-learn](https://scikit-learn.org/stable/#) (Intel-MKL-acclerated) | 0.22.2.post1 | Machine Learning in Python. 常用的機器學習套件,提供一些基礎算法,統計方法 (利用 Intel MKL 加速)。 |
| [Numba](http://numba.pydata.org) | 0.48.0 | Numba is an open source JIT compiler that translates a subset of Python and NumPy code into fast machine code. Python 程式碼經 JIT 編譯器編譯後,可加速百倍至千倍。 |
| [Numexpr](https://github.com/pydata/numexpr) | 2.7.1 | Fast numerical array expression evaluator for Python, NumPy, PyTables, pandas, bcolz and more. 數學表達式經過計算優化後,可提升最高至4倍速。 |
| [pyodbc](https://github.com/mkleehammer/pyodbc) | 4.0.30 | pyodbc is an open source Python module that makes accessing ODBC databases simple. 連結資料庫使用。 |
| [Jupyterlab](https://github.com/jupyterlab/jupyterlab) | 2.0.0 | An extensible environment for interactive and reproducible computing, based on the Jupyter Notebook and Architecture. 程式碼運行,紀錄,筆記撰寫,皆可存放並整理至筆記本。 |
| [pandas](https://pandas.pydata.org) | 1.0.1 | pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language. 建立並整理資料表,並且提供簡易的方式將資料表視覺化。 |
| [Matplotlib](https://matplotlib.org) | 3.2.0 | Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. 資料視覺化套件,可繪製長條圖,直方統計圖,散點圖等。 |
| [Seaborn](https://seaborn.pydata.org) | 0.10.0 | Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. 基於 Matplotlib 的高階繪圖 API; 可接收資料表,自動做 groupby 後繪圖。 |
| [Bokeh](https://bokeh.pydata.org/en/latest/) | 1.4.0 | Bokeh is an interactive visualization library that targets modern web browsers for presentation. 可嵌入至網頁,實現互動式的數據呈現。|
| 套件/軟體/函式庫名稱 | 版本 | 套件說明 |
|:---------|:---------:|:---------|
| [TensorFlow](https://www.tensorflow.org) | 2.1.0 | An open source machine learning library for research and production. 由 Google 維護的,開源的 AI 模型開發框架。 |
| [Keras](https://keras.io) | 2.3.1 | Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano. Keras 是基於底層 AI 框架的高階 API。有了它,我們就可以便捷的去建立,訓練和使用 AI 模型。 |
| [Python](https://docs.python.org/3.7/whatsnew/changelog.html#python-3-7-3-final) | 3.7.5 | Python is powerful... and fast; plays well with others; runs everywhere; is friendly & easy to learn; is Open. 我們環境採用 Python 3.7,它於字串處理和檔案搜索方面較 Python3.6 快很多。 |
| [Horovod](https://github.com/horovod/horovod) | 0.19.0 | Distributed training framework for TensorFlow, Keras, PyTorch, and Apache MXNet. 使用 Uber Horovod 可簡易的將 AI 訓練利用多 GPU 做加速。 |
| [OpenMPI](https://www.open-mpi.org) | 4.0.2 | A High Performance Message Passing Library. (Required by Uber Horovod) OpenMPI 為 Uber Horovod 所需,可支持跨卡/跨伺服器節點的溝通。 |
| [Intel Math Kernel Library (Intel MKL)](https://software.intel.com/en-us/mkl) | 2020.0-088 | Intel® Math Kernel Library (Intel® MKL) optimizes code with minimal effort for future generations of Intel® processors. 針對 Intel CPU 做快速的數值運算。 |
| [NumPy](https://www.numpy.org) (Intel-MKL-acclerated) | 1.18.1 | NumPy is the fundamental package for scientific computing with Python. 常用的數值運算套件 (利用 Intel MKL 加速)。 |
| [SciPy](https://www.scipy.org) (Intel-MKL-acclerated) | 1.4.1 | SciPy (pronounced “Sigh Pie”) is a Python-based ecosystem of open-source software for mathematics, science, and engineering. 常用的科研套件,提供一些基礎算法,統計方法 (利用 Intel MKL 加速)。 |
| [Scikit-learn](https://scikit-learn.org/stable/#) (Intel-MKL-acclerated) | 0.22.2.post1 | Machine Learning in Python. 常用的機器學習套件,提供一些基礎算法,統計方法 (利用 Intel MKL 加速)。 |
| [Numba](http://numba.pydata.org) | 0.48.0 | Numba is an open source JIT compiler that translates a subset of Python and NumPy code into fast machine code. Python 程式碼經 JIT 編譯器編譯後,可加速百倍至千倍。 |
| [Numexpr](https://github.com/pydata/numexpr) | 2.7.1 | Fast numerical array expression evaluator for Python, NumPy, PyTables, pandas, bcolz and more. 數學表達式經過計算優化後,可提升最高至4倍速。 |
| [pyodbc](https://github.com/mkleehammer/pyodbc) | 4.0.30 | pyodbc is an open source Python module that makes accessing ODBC databases simple. 連結資料庫使用。 |
| [Jupyterlab](https://github.com/jupyterlab/jupyterlab) | 2.0.0 | An extensible environment for interactive and reproducible computing, based on the Jupyter Notebook and Architecture. 程式碼運行,紀錄,筆記撰寫,皆可存放並整理至筆記本。 |
| [pandas](https://pandas.pydata.org) | 1.0.1 | pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language. 建立並整理資料表,並且提供簡易的方式將資料表視覺化。 |
| [Matplotlib](https://matplotlib.org) | 3.2.0 | Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. 資料視覺化套件,可繪製長條圖,直方統計圖,散點圖等。 |
| [Seaborn](https://seaborn.pydata.org) | 0.10.0 | Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. 基於 Matplotlib 的高階繪圖 API; 可接收資料表,自動做 groupby 後繪圖。 |
| [Bokeh](https://bokeh.pydata.org/en/latest/) | 1.4.0 | Bokeh is an interactive visualization library that targets modern web browsers for presentation. 可嵌入至網頁,實現互動式的數據呈現。|
經由
小編
評論
‧ 629 天前