2018-01-06
keywords: CNTK C# 神經(jīng)網(wǎng)絡(luò)
CNTK是微軟搞的開源神經(jīng)網(wǎng)絡(luò)镣典,因?yàn)槠涮峁〤#接口膳叨,并且自稱速度最快婶溯,所以決定裝一個(gè)用用间景。
1佃声、CNTK安裝
參見官方文檔,很詳細(xì):Setup CNTK on your machine
主要有兩步:
1)下載CNTK二進(jìn)制版倘要,分為單CPU版和帶GPU版圾亏,我的筆記本不支持GPU,所以下的是單CPU版,CNTK for Windows v.2.3 CPU only?志鹃,下載后解壓即可夭问。
2)運(yùn)行Install.bat
cd cntk\Scripts\install\windows
install.bat
腳本自動(dòng)完成相關(guān)代碼的安裝,包括pyphon35曹铃、OpenCv缰趋、qt等很多東東。安裝只需兩步铛只,下載&install埠胖,一切似乎很美好,但是淳玩,先別高興太早直撤,坑來了。蜕着。谋竖。
也不知道是我的網(wǎng)絡(luò)問題還是美帝搞破壞,install的時(shí)候總是會(huì)異常終止承匣。咋辦呢蓖乘,只有兩個(gè)字,堅(jiān)持啊兄弟韧骗!再次運(yùn)行install嘉抒,再三次運(yùn)行install,再八次運(yùn)行install袍暴。些侍。。終于政模,OK了岗宣。
install還是很聰明的,不會(huì)每次從頭安裝淋样,而是從上次中斷的地方開始安裝耗式。install完成時(shí)是過去了半天、一天還是一年呢趁猴,不記得了刊咳,感覺一切好遙遠(yuǎn)。
這里還要提一句儡司,使用cntk pyphon的兄弟娱挨,每次都要運(yùn)行scripts目錄下的cntkpy35.bat腳本,所以不妨將scripts目錄放在環(huán)境變量path中枫慷。
另外让蕾,將cntk運(yùn)行文件的目錄也加在path中浪规,方便以后使用。
2探孝、使用C# API
CNTK的C# API HelloWorld程序是名叫TrainingCSharp的東東笋婿,也不知道是個(gè)干啥的,我本地的目錄是C:\cntk23\Examples\TrainingCSharp顿颅,這次的目的就是讓這貨動(dòng)起來缸濒。
編譯
話不多說,直接運(yùn)行Visual Studio粱腻,加載解決方案庇配,編譯,會(huì)出現(xiàn)很多錯(cuò)绍些,主要是引用出了問題捞慌,沒關(guān)系,重新生成項(xiàng)目即可柬批。如果還有問題啸澡,那可能是你的VS沒有裝NuGit。
準(zhǔn)備訓(xùn)練文件
F5運(yùn)行氮帐,瞪嗅虏,蹦出來一個(gè)錯(cuò)誤
Input file '../../Tests/EndToEndTests/Image/Data\Train_cntk_text.txt' is not open.
還沒有訓(xùn)練文件啊,趕快搞一個(gè)吧上沐,從哪兒搞呢皮服,微軟說,從這兒搞?CNTK C#/.NET API training examples参咙,那就搞吧龄广,咋搞呢,意思就是說昂勒,在目錄Examples\Image\DataSets\CIFAR-10下面蜀细,運(yùn)行這句話
python install_cifar10.py
又是個(gè)坑舟铜,這句話運(yùn)行完了就不動(dòng)了戈盈,下載文件總該有個(gè)進(jìn)度條吧,界面真不友好谆刨,一個(gè)字塘娶,等吧,這可好痊夭,等了快一天了就是不見動(dòng)靜刁岸,終于忍無可忍,按了一個(gè)Ctrl Z她我,奇跡出現(xiàn)了虹曙,下載完成迫横,正在生成Txt文件,繩啊酝碳,這是下載程序有bug啊......也許是后門呢矾踱。
在CNTK目錄下,新建目錄Tests2\EndToEndTests\Image\Data疏哗,放入文件Train_cntk_text.txt呛讲;為什么是在CNTK目錄下呢,因?yàn)轫?xiàng)目生成的執(zhí)行程序也在CNTK目錄下的x64目錄中返奉。
修改數(shù)組維度
F5繼續(xù)贝搁,瞪,又蹦出來一個(gè)異常:
System.ApplicationException:“Reached the maximum number of allowed errors while reading the input file (../../Tests/EndToEndTests/Image/Data\Train_cntk_text.txt).
想了半天沒想明白芽偏,既然是跟Train_cntk_text.txt文件有關(guān)系雷逆,那就查吧,這個(gè)文件是由cifar10生成的污尉,這是下載頁(yè)面The CIFAR-10 dataset关面,看這句話:
The CIFAR-10 dataset consists of 60000 32x32 colour images in 10 classes, with 6000 images per class. There are 50000 training images and 10000 test images.?
也就是說,圖片是32*32的彩色圖片十厢,再看程序等太,MNISTClassifier.TrainAndEvaluate中,是這兩句
int[] imageDim = useConvolution ? new int[] { 28, 28, 1 } : new int[] { 784 };
int imageSize = 28 * 28;
改為(32*32 ?3通道)
int[] imageDim = useConvolution ? new int[] { 32, 32, 3 } : new int[] { 32 * 32 * 3 };
?int imageSize = 32 * 32 * 3;
F5繼續(xù)蛮放,瞪缩抡,又是個(gè)異常
System.ApplicationException:“GEMM convolution engine does not support this convolution configuration. It is possible to make GEMM engine work with this configuration by defining input/output/kernel using tensors of higher(+1) dimension. Geometry: Input: 16 x 16 x 12, Output: 16 x 16 x 72, Kernel: 3 x 3 x 4, Map: 1 x 1 x 24, Stride: 1 x 1 x 4, Sharing: (1, 1, 1), AutoPad: (1, 1, 1), LowerPad: 0 x 0 x 0, UpperPad: 0 x 0 x 0[CALL STACK] >?
前面改的就感覺沒完,果然包颁,還需改下面瞻想,MNISTClassifier.CreateConvolutionalNeuralNetwork中
// 28x28x1 -> 14x14x4
int kernelWidth1 = 3, kernelHeight1 = 3, numInputChannels1 = 1, outFeatureMapCount1 = 4;
改為(因?yàn)槲覀兪遣噬ǖ溃?/p>
// 28x28x3 -> 14x14x12
?int kernelWidth1 = 3, kernelHeight1 = 3, numInputChannels1 = 3, outFeatureMapCount1 = 4 * 3;
還有這句,也要改
//// 14x14x4 -> 7x7x8
//int kernelWidth2 = 3, kernelHeight2 = 3, numInputChannels2 = outFeatureMapCount1, outFeatureMapCount2 = 8;
? ?// 14x14x12 -> 7x7x24
?int kernelWidth2 = 3, kernelHeight2 = 3, numInputChannels2 = outFeatureMapCount1, outFeatureMapCount2 = 8 * 3;
放棄LSTM
F5繼續(xù)娩嚼,瞪呢蘑险?好久沒有出現(xiàn)了,程序猿的潛意識(shí)告訴我岳悟,不可能佃迄!果然,瞪又來了贵少,
Input file '../../Tests/EndToEndTests/Text/SequenceClassification/Data\Train.ctf' is not open.,
ctf文件是啥東東呵俏,查,原來跟Train_cntk_text.txt是一樣的滔灶,好吧普碎,建個(gè)目錄Tests\EndToEndTests\Text\SequenceClassification\Data,加入Train.ctf文件录平,再次運(yùn)行麻车,
瞪缀皱,果然,
WARNING: Empty input row at offset 10759 in the input file (../../Tests/EndToEndTests/Text/SequenceClassification/Data\Train.ctf).
WARNING: Could not read a row (# 1) while loading sequence (id = 0) at offset 10759 in the input file (../../Tests/EndToEndTests/Text/SequenceClassification/Data\Train.ctf).
? Message=Reached the maximum number of allowed errors while reading the input file (../../Tests/EndToEndTests/Text/SequenceClassification/Data\Train.ctf).
還是訓(xùn)練數(shù)據(jù)數(shù)組維度的問題动猬,LSTMSequenceClassifier中進(jìn)行如下修改
//const int inputDim = 2000;
? ? ? ? ? ? //const int cellDim = 25;
? ? ? ? ? ? //const int hiddenDim = 25;
? ? ? ? ? ? //const int embeddingDim = 50;
? ? ? ? ? ? //const int numOutputClasses = 5;
? ? ? ? ? ? const int inputDim = 32 * 32;
? ? ? ? ? ? const int cellDim = 3;
? ? ? ? ? ? const int hiddenDim = 25;
? ? ? ? ? ? const int embeddingDim = 50;
? ? ? ? ? ? const int numOutputClasses = 10;
還是不行唆鸡,終于晉楚殺著,直接在program.c中注釋掉了這一句
LSTMSequenceClassifier.Train(device);
這下好了枣察,終于沒人鬧心了争占,哈哈。
回顧
安裝了CNTK序目,安裝了cifar10樣本數(shù)據(jù)臂痕,用C#跑通了邏輯回歸(LogisticRegression)、multilayer perceptron (MLP)猿涨、 convolutional neural network(CNN)握童,沒有跑通 LSTM(長(zhǎng)短記憶模型),雖然跑通了叛赚,但還不知道里面都是個(gè)啥澡绩,需要一個(gè)一個(gè)再分析實(shí)踐,留到下節(jié)了俺附。