谷歌正式開(kāi)源 Hinton 膠囊理論代碼,即刻用 TensorFlow 實(shí)現(xiàn)吧
AI 研習(xí)社消息,相信大家對(duì)于「深度學(xué)習(xí)教父」Geoffery Hinton 在去年年底發(fā)表的膠囊網(wǎng)絡(luò)還記憶猶新,在論文 Dynamic Routing between Capsules 中,Hinton 團(tuán)隊(duì)提出了一種全新的網(wǎng)絡(luò)結(jié)構(gòu)。為了避免網(wǎng)絡(luò)結(jié)構(gòu)的雜亂無(wú)章,他們提出把關(guān)注同一個(gè)類別或者同一個(gè)屬性的神經(jīng)元打包集合在一起,好像膠囊一樣。在神經(jīng)網(wǎng)絡(luò)工作時(shí),這些膠囊間的通路形成稀疏激活的樹(shù)狀結(jié)構(gòu)(整個(gè)樹(shù)中只有部分路徑上的膠囊被激活)。這樣一來(lái),Capsule 也就具有更好的解釋性。
在實(shí)驗(yàn)結(jié)果上,CapsNet 在數(shù)字識(shí)別和健壯性上都取得了不錯(cuò)的效果。詳情可以參見(jiàn) 終于盼來(lái)了Hinton的Capsule新論文,它能開(kāi)啟深度神經(jīng)網(wǎng)絡(luò)的新時(shí)代嗎?
日前,該論文的第一作者 Sara Sabour 在 GitHub 上公布了論文代碼,大家可以馬上動(dòng)手實(shí)踐起來(lái)。雷鋒網(wǎng) AI 研習(xí)社將教程編譯整理如下:
所需配置:
-
TensorFlow(點(diǎn)擊 http://www.tensorflow.org 進(jìn)行安裝或升級(jí))
-
NumPy (詳情點(diǎn)擊 http://www.numpy.org/ )
-
GPU
執(zhí)行 test 程序,來(lái)驗(yàn)證安裝是否正確,諸如:
python layers_test.py
快速 MNIST 測(cè)試:
-
下載并提取 MNIST tfrecord 到 $DATA_DIR/ 下:
https://storage.googleapis.com/capsule_toronto/mnist_data.tar.gz
-
下載并提取 MNIST 模型 checkpoint 到 $CKPT_DIR 下:
https://storage.googleapis.com/capsule_toronto/mnist_checkpoints.tar.gz
python experiment.py --data_dir=$DATA_DIR/mnist_data/ --train=false \ --summary_dir=/tmp/ --checkpoint=$CKPT_DIR/mnist_checkpoint/model.ckpt-1
快速 CIFAR10 ensemble 測(cè)試:
-
下載并提取 cifar10 二進(jìn)制文件到 $DATA_DIR/ 下:
-
下載并提取 cifar10 模型 checkpoint 到 $CKPT_DIR 下:
https://storage.googleapis.com/capsule_toronto/cifar_checkpoints.tar.gz
-
將目錄($DATA_DIR)作為 data_dir 來(lái)傳遞:
python experiment.py --data_dir=$DATA_DIR --train=false --dataset=cifar10 \ --hparams_override=num_prime_capsules=64,padding=SAME,leaky=true,remake=false \ --summary_dir=/tmp/ --checkpoint=$CKPT_DIR/cifar/cifar{}/model.ckpt-600000 \ --num_trials=7
CIFAR10 訓(xùn)練指令:
python experiment.py --data_dir=$DATA_DIR --dataset=cifar10 --max_steps=600000\ --hparams_override=num_prime_capsules=64,padding=SAME,leaky=true,remake=false \ --summary_dir=/tmp/
MNIST full 訓(xùn)練指令:
-
也可以執(zhí)行--validate=true as well 在訓(xùn)練-測(cè)試集上訓(xùn)練
-
執(zhí)行 --num_gpus=NUM_GPUS 在多塊GPU上訓(xùn)練
python experiment.py --data_dir=$DATA_DIR/mnist_data/ --max_steps=300000\ --summary_dir=/tmp/attempt0/
MNIST baseline 訓(xùn)練指令:
python experiment.py --data_dir=$DATA_DIR/mnist_data/ --max_steps=300000\ --summary_dir=/tmp/attempt1/ --model=baseline
To test on validation during training of the above model:
訓(xùn)練如上模型時(shí),在驗(yàn)證集上進(jìn)行測(cè)試(記住,在訓(xùn)練過(guò)程中會(huì)持續(xù)執(zhí)行指令):
-
在訓(xùn)練時(shí)執(zhí)行 --validate=true 也一樣
-
可能需要兩塊 GPU,一塊用于訓(xùn)練集,一塊用于驗(yàn)證集
-
如果所有的測(cè)試都在一臺(tái)機(jī)器上,你需要對(duì)訓(xùn)練集、驗(yàn)證集的測(cè)試中限制 RAM 消耗。如果不這樣,TensorFlow 會(huì)在一開(kāi)始占用所有的 RAM,這樣就不能執(zhí)行其他工作了
python experiment.py --data_dir=$DATA_DIR/mnist_data/ --max_steps=300000\ --summary_dir=/tmp/attempt0/ --train=false --validate=true
大家可以通過(guò) --num_targets=2 和 --data_dir=$DATA_DIR/multitest_6shifted_mnist.tfrecords@10 在 MultiMNIST 上進(jìn)行測(cè)試或訓(xùn)練,生成 multiMNIST/MNIST 記錄的代碼在 input_data/mnist/mnist_shift.py 目錄下。
multiMNIST 測(cè)試代碼:
python mnist_shift.py --data_dir=$DATA_DIR/mnist_data/ --split=test --shift=6 --pad=4 --num_pairs=1000 --max_shard=100000 --multi_targets=true
可以通過(guò) --shift=6 --pad=6 來(lái)構(gòu)造 affNIST expanded_mnist
論文地址: https://arxiv.org/pdf/1710.09829.pdf
GitHub 地址: https://github.com/Sarasra/models/tree/master/research/capsules