選自GitHub
機器之心編譯
參與:張倩、王淑婷
本文介紹了一個面向 PyTorch 和 TensorFlow 神經網絡計算圖和訓練度量(metric)的輕量級庫——HiddenLayer,它適用於快速實驗,且與 Jupyter Notebook 兼容。
GitHub鏈接:https://github.com/waleedka/hiddenlayer
HiddenLayer 非常簡單,易於擴展,且與 Jupyter Notebook 完美兼容。開發該工具的目的不是爲了取代 TensorBoard 等高級工具,而是用在那些無需使用高級工具的用例中(殺雞焉用宰牛刀)。HiddenLayer 由 Waleed Abdulla 和 Phil Ferriere 編寫,已獲得 MIT 許可證。
1. 可讀的圖
使用 HiddenLayer 在 Jupyter Notebook 中渲染你的神經網絡圖,或者渲染 pdf 或 png 文件。Jupyter notebook 示例請參考以下鏈接:
- TensorFlow:https://github.com/waleedka/hiddenlayer/blob/master/demos/tf_graph.ipynb
- Pytorch:https://github.com/waleedka/hiddenlayer/blob/master/demos/pytorch_graph.ipynb
這些圖用於溝通高級架構。因此,低級細節在默認狀態下是隱藏的(如權重初始化 ops、梯度、一般層類型的內部 ops 等)。HiddenLayer 還將常用層序列疊在一起。例如,Convolution -> RELU -> MaxPool 序列比較常用,爲簡單起見,它們被合併在一個盒子裏。
自定義圖
隱藏、摺疊節點的規則是完全可定製的。你可以用 graph expressions 和 transforms 添加自己的規則。例如,使用以下命令可以將 ResNet101 中 bottleneck 塊的所有節點摺疊爲一個節點。
# Fold bottleneck blocks ht.Fold("((ConvBnRelu > ConvBnRelu > ConvBn) | ConvBn) > Add > Relu", "BottleneckBlock", "Bottleneck Block"),
2. Jupyter Notebook 中的訓練度量
在 Jupyter Notebook 中運行訓練試驗非常有用。你可以繪製損失函數和準確率圖、權重直方圖,或者可視化一些層的激活函數。
在 Jupyter Notebook 之外:
在 Jupyter Notebook 外同樣可以使用 HiddenLayer。在 Python 腳本中運行 HiddenLayer,可以打開度量的單獨窗口。如果你使用的服務器沒有 GUI,可以將圖像截圖存儲爲 png 文件以備後查。該用例示例參見:https://github.com/waleedka/hiddenlayer/blob/master/demos/history_canvas.py。
3. Hackable
HiddenLayer 是一個小型庫。它覆蓋基礎元素,但你可能需要爲自己的用例進行擴展。例如,如果你想將模型準確率表示爲餅圖,就需要擴展 Canbas 類,並添加新方法,如下所示:
class MyCanvas(hl.Canvas): """Extending Canvas to add a pie chart method.""" def draw_pie(self, metric): # set square aspect ratio self.ax.axis('equal') # Get latest value of the metric value = np.clip(metric.data[-1], 0, 1) # Draw pie chart self.ax.pie([value, 1-value], labels=["Accuracy", ""])
示例參見:https://github.com/waleedka/hiddenlayer/blob/master/demos/pytorch_train.ipynb 或 https://github.com/waleedka/hiddenlayer/blob/master/demos/tf_train.ipynb。
Demo
PyTorch:
- pytorch_graph.ipynb:此 notebook 展示瞭如何爲一些流行的 PyTorch 模型生成圖。
- 地址:https://github.com/waleedka/hiddenlayer/blob/master/demos/pytorch_graph.ipynb
- pytorch_train.ipynb:展示瞭如何在 PyTorch 中追蹤和可視化訓練度量。
- 地址:https://github.com/waleedka/hiddenlayer/blob/master/demos/pytorch_train.ipynb
- history_canvas.py:在沒有 GUI 的情況下使用 HiddenLayer 的示例。
- 地址:https://github.com/waleedka/hiddenlayer/blob/master/demos/history_canvas.py
TensorFlow:
- tf_graph.ipynb:此 notebook 介紹瞭如何爲不同的 TF SLIM 模型生成圖。
- 地址:https://github.com/waleedka/hiddenlayer/blob/master/demos/tf_graph.ipynb
- tf_train.ipynb:展示瞭如何在 TensorFlow 中追蹤和可視化訓練度量。
- 地址:https://github.com/waleedka/hiddenlayer/blob/master/demos/tf_train.ipynb
- history_canvas.py:在沒有 GUI 的情況下使用 HiddenLayer 的示例。
- 地址:https://github.com/waleedka/hiddenlayer/blob/master/demos/history_canvas.py
安裝
1. 先決條件
a. Python3、Numpy、Matplotlib 和 Jupyter Notebook。
b. 用 TensorFlow 或 PyTorch。
c. 用 GraphViz 及其 Python 封裝來生成網絡圖。最簡單的安裝方式是:
**If you use Conda:** ```bash conda install graphviz python-graphviz ``` **Otherwise:** * [Install GraphViz](https://graphviz.gitlab.io/download/) * Then install the [Python wrapper for GraphViz](https://github.com/xflr6/graphviz) using pip: ``` pip3 install graphviz ```
2. 安裝 HiddenLayer
A. 從 GitHub 中安裝(開發者模式)
如果要在本地編輯或自定義庫,使用此選項。
# Clone the repository git clone [email protected]:waleedka/hiddenlayer.git cd hiddenlayer # Install in dev mode pip install -e .
B. 使用 PIP
pip install hiddenlayer
本文爲機器之心編譯,轉載請聯繫本公衆號獲得授權。