← 返回卷宗
數據預測

研究CNTK(三):MNIST識別之01_OneHidden.cntk

CNTKDeep Learning神經網絡

# Parameters can be overwritten on the command line
# for example: cntk configFile=myConfigFile RootDir=../..
# For running from Visual Studio add
# currentDirectory=$(SolutionDir)/<path to corresponding data folder>

command = trainNetwork:testNetwork

precision = "float";
traceLevel = 1 ;
deviceId = "auto"

rootDir = ".." ;
dataDir = "$rootDir$/DataSets/MNIST" ;
outputDir = "./Output" ;

modelPath = "$outputDir$/Models/01_OneHidden"
#stderr = "$outputDir$/01_OneHidden_bs_out"

# TRAINING CONFIG
trainNetwork = {
 action = "train"
 
 BrainScriptNetworkBuilder = {
 imageShape = 28:28:1 # image dimensions, 1 channel only
 labelDim = 10 # number of distinct labels
 featScale = 1/256

 # This model returns multiple nodes as a record, which
 # can be accessed using .x syntax.
 # modle 是返回多個節點作為一條記錄,可以使用 .x 語法訪問
 
 model(x) = {
 #對所有的圖片歸一化到0~1之間,因為字節是0~255,所以乘以1/256
 s1 = x * featScale
 
 #激活函數使用Relu,Dense代表標準全連接層
 #另外還有TimeDistributedDense是基於時間的,主要用於建立RNN(遞歸神經網絡)的
 h1 = DenseLayer {200, activation=ReLU} (s1)
 
 # 線性對應,一個圖片對應一個標籤
 z = LinearLayer {labelDim} (h1)
 }
 
 # inputs
 # 定義輸入層
 features = Input {imageShape}
 labels = Input {labelDim}

 # apply model to features
 # 直接使用model函數
 out = model (features)

 # loss and error computation
 # 誤差的計算,對多分類,由於類別是互斥的,所以使用Softmax
 ce   = CrossEntropyWithSoftmax (labels, out.z)
 errs = ClassificationError (labels, out.z)

 # declare special nodes
 # 定義特殊節點
 featureNodes = (features)
 labelNodes = (labels)
 criterionNodes  = (ce)
 evaluationNodes = (errs)
 outputNodes = (out.z)
 
 # Alternative, you can use the Sequential keyword and write the model
 # as follows. We keep the previous format because EvalClientTest needs
 # to access the internal nodes, which is not doable yet with Sequential
 #
 # Scale{f} = x => Constant(f) .* x
 # model = Sequential (
 # Scale {featScale} :
 # DenseLayer {200} : ReLU :
 # LinearLayer {labelDim}
 # )

 # # inputs
 # features = Input {imageShape}
 # labels = Input (labelDim)

 # # apply model to features
 # ol = model (features)

 # # loss and error computation
 # ce   = CrossEntropyWithSoftmax (labels, ol)
 # errs = ClassificationError (labels, ol)

 # # declare special nodes
 # featureNodes = (features)
 # labelNodes = (labels)
 # criterionNodes  = (ce)
 # evaluationNodes = (errs)
 # outputNodes = (ol)
 }

 SGD = {
 epochSize = 60000
 minibatchSize = 64
 maxEpochs = 10
 learningRatesPerSample = 0.01*5:0.005
 momentumAsTimeConstant = 0
 
 numMBsToShowResult = 500
 }

 #定義讀取數據類的工具,這裡CNTKTextFormatReader用於直接讀文本
 reader = {
 readerType = "CNTKTextFormatReader"
 
 # See ../REAMDE.md for details on getting the data (Train-28x28_cntk_text.txt).
 # 讀取已轉換好的文件
 file = "$DataDir$/Train-28x28_cntk_text.txt"
 
 # 將輸入定義好二維數組,方便使用,格式要指定好是全連接
 input = {
 features = { dim = 784 ; format = "dense" }
 labels =   { dim = 10  ; format = "dense" }
 }
 }   
}

# TEST CONFIG
testNetwork = {
 action = "test"
 # reduce this if you run out of memory
 # 一次批量檢測多少,如果內存不夠就減少這個值
 minibatchSize = 1024 

 reader = {
 readerType = "CNTKTextFormatReader"
 file = "$DataDir$/Test-28x28_cntk_text.txt"
 input = {
 features = { dim = 784 ; format = "dense" }
 labels =   { dim = 10  ; format = "dense" }
 }
 }
}

可以看出建立一個三層神經網絡非常簡單,配置好Reader後,直接讀取數據。

訓練裡,定義了model函數,這裡麵包含了一個輸入層的歸一化,然後全連接一個新層DenseLayer,再設置DenseLayer輸出結果與LABEL進行線性對應。

 

本文由 三符道長 撰於 2016年10月27日。轉載請註明出處。