用C#包裹CNTK的Brainscript

时间:2019-05-14 03:14:12

标签: c# wrapper cntk

CNTK有一个C#API,但是它的故障太大,我发现它在很大程度上不可用。

我看过CNTK的Brainscript,它非常C#py!

我已经部分构建了一个包装Brainscript的C#包装器,至少部分包装了它,因此我可以使用C#构造的cntk文件运行CNTK。

我正在运行简单的代码。这是一个片段:

int vocabSize = 943;
int numLabels = 129;
int numIntents = 26;

int inputDim = 0;
int labelDim = 0;
int embDim = 150;
int hiddenDim = 300;

string baseDir = ".";

Wrapper wrapper = new Wrapper
{

    // The Command:
    Command = "TrainTagger:TestTagger",

    // Needs some thought, might be able to be done better...
    ModelParameters = new Dictionary<string, int>
    {
        { "vocabSize", vocabSize },
        { "numLabels", numLabels },
        { "numIntents", numIntents }
    },

    // The Action Type:
    ActionType = ActionType.Train,

    // Set the Directory structure:
    RootDir = baseDir,
    DataDir = "Data",
    ModelDir = "Model",
    ModelPath = "slu.cmf",

    // Needs some thought, might be able to be done better...
    ModelHiddenParameters = new Dictionary<string, string>
    {
        { "inputDim", "$vocabSize$" },
        { "labelDim", "$numLabels$" },
        { "embDim", embDim.ToString() },
        { "hiddenDim", hiddenDim.ToString() }
    },

    // The actual Model:
    Sequential = new List<ILayer>
    {

        new EmbeddingLayer(embDim),
        new RecurrentLSTMLayer(hiddenDim, false),
        new DenseLayer(labelDim)
    },

    // Initialise the Input to the Model Input and Input to the Model Output:
    ModelInitialisers = new Dictionary<string, string>
    {
        { "query", "inputDim"},
        { "slotLabels", "labelDim"}
    },

    // The SGD configuration block controls the behavior of the SGD (Stochastic Gradient Descent) algorithm in CNTK:
    SGD = new SGD
    {

        MaxEpochs = 8,
        EpochSize = 36000,
        MinibatchSize = 70,
        LearningRatesPerSample = "0.003 * 2:0.0015 * 12:0.0003",
        GradUpdateType = GradUpdateType.AdaGrad,
        GradientClippingWithTruncation = true,
        ClippingThresholdPerSample = 15.0,
        FirstMBsToShowResult = 10,
        NumMBsToShowResult = 100
    },

    // The reader block is used for all types of readers and the readerType parameter determines which reader to use:
    Reader = new Reader
    {

        ReaderType = ReaderType.CNTKTextFormatReader,
        File = Path.Combine(".", "atis.test.ctf"),
        Randomize = false,
        Input = new Dictionary<string, Input>
        {
            { "features", new Input("S0", vocabSize, FormatType.Sparse) },
            { "intents", new Input("S1", numLabels, FormatType.Sparse) },
            { "labels", new Input("S2", numIntents, FormatType.Sparse) }
        },
    }
};
wrapper.Run();

我正在做的是将类转换为适当的Brainscript函数,全部转换为字符串。然后,我创建一个配置文件以与CNTK一起运行。

我的问题,有没有更快,更好的方法来进行这种包装。也许是用于构建存根之类的应用程序?

0 个答案:

没有答案