一、NDL语言基本概念
The Network Description Language (NDL)的计算网络工具包(CNTK)提供了一种简单的方式来定义一个网络代码的方式。它包含变量和宏,和其他容易理解的概念。它类似于一种脚本语言在语法,但是它不是一个编程“语言”,而是简单的方法来定义一个网络。
SDim=784
HDim=256
LDim=10
B0=Parameter(HDim)
W0=Parameter(HDim, SDim)
features=Input(SDim)
labels=Input(LDim)
Times1=Times(W0, features)
Plus1=Plus(Times1, B0)
RL1=RectifiedLinear(Plus1)
B1=Parameter(LDim, 1)
W1=Parameter(LDim, HDim)
Times2=Times(W1, RL1)
Plus2=Plus(Times2, B1)
CrossEntropy=CrossEntropyWithSoftmax(labels, Plus2)
ErrPredict=ErrorPrediction(labels, Plus2)
FeatureNodes=(features)
LabelNodes=(labels)
CriteriaNodes=(CrossEntropy)
EvalNodes=(ErrPredict)
OutputNodes=(Plus2)
SDim=784
HDim=256
LDim=10
这些是变量字符串变量名并给与赋值。
Times1=Times(W0, features)
Plus1=Plus(Times1, B0)
RL1=RectifiedLinear(Plus1)
B1=Parameter(LDim, 1)
这些事NDL Function,NDL全部函数包括:
- Input, InputValue
- ImageInput
- Parameter, LearnableParameter
- Sum
- Scale
- Times
- Plus
- Minus
- Negate
- RectifiedLinear
- Sigmoid
- Tanh
- Log
- Softmax
- SquareError
- CrossEntropyWithSoftmax, CEWithSM
- MatrixL1Reg, L1Reg
- MatrixL2Reg, L2Reg
- PerDimMeanVarNormalization, PerDimMVNorm
- ErrorPrediction
- Dropout
- Mean
- Convolution, Convolve
- MaxPooling
- AveragePooling
- Delay
NDL Macros
NDL的宏有两种定义方法,第一种是嵌套方法:
RFF(x1, w1, b1)=RectifiedLinear(Plus(Times(w1,x1),b1))
函数调用和评估是从最里层到最外层的,Times(w1,x1)先被计算然后作为Plus()的第一个参数传入,Plus()的结果作为RectifiedLinear()的参数,解决了结果储存在用户指定变量中的问题如:
RFFResult=RFF(x1, w1, b1)
第二种方法是块定义
FF(X1, W1, B1)
{
T=Times(W1,X1);
FF=Plus(T, B1);
}
这种情况下中间变量可以通过点方法访问。
L1 = FF(features, HDim, SDim)
L1.T
宏有一下几点注意:
- Parameters used in Macros are local to each Macro
- Return Value of a Macro is returned via the local Macro variable that has the same name as the Macro. If no such variable found, the last variable in the Macro will be returned
- Parameters can be declared within Macros
- A Macro can call another Macro, but recursion is not supported
#
declares a comment line