c# - CNTK LSTM 评估

标签 c# lstm cntk

我无法弄清楚如何在 c#.net 中为我创建的 LSTM 网络进行评估。任何帮助都会很棒 :)

def create_model(x,num_classes,LSTM_dim):
with default_options(initial_state=0.1):
    m =  C.layers.Sequential([
         C.layers.Recurrence(C.layers.LSTM(LSTM_dim), go_backwards=False),
         C.sequence.last,
         C.layers.Dense(num_classes)
    ])
    return m(x);

数据看起来像这样

0 |features 0.23 0.24 0 0.245 0.9723383 -0.1125 |labels -8.88333333333333
0 |features -0.01 0.02 0.23 0.075 0.2361691 -0.1725 
0 |features 0 0.02 -0.01 0.04 0.1574461 -0.1916667 
0 |features 0.02 0.05 0.03 0.15 0.2942928 -0.208125 
0 |features -0.02 0.05 -0.01 0.115 0.09446766 -0.215 
0 |features 0.04 0.09 0.03 0.165 0.2193967 -0.2204167 

我的 C# 代码 ... 我在 C# 中的数据在浮点列表中,我尝试在每个 6 处拼接,但在第二个序列中出现以下错误! ...我似乎无法弄清楚我做错了什么:(

System.ApplicationException: 'GetColumnIndex: Attempted to access a time step that is accessing a portion of a sequence that is not included in current minibatch.

public CntkDnn(String FileName)
            {
                device = DeviceDescriptor.GPUDevice(0);
                Function modelFunc2 = Function.Load(FileName, device);
                // var z = CNTK.CNTKLib.Softmax(modelFunc2);
                var z = modelFunc2;
                inputVar = z.Arguments.FirstOrDefault();



                outputVar = z.Output;

                inputShape = inputVar.Shape;

                dataCount = inputShape[0];

                nn = z;
            }
  public List<float> EvalSeq(List<float> input,int SplitBy)
            {
                //if (dataCount != input.Count) return null;
                var output = new List<float>();
                var starting = true;
                for (int jindex = 0; jindex < input.Count; jindex += 6)
                {

                    var thisInput = input.Skip(jindex).Take(6).ToList();
                    var inputDataMap = new Dictionary<Variable, Value>();
                    var inputValue = Value.CreateSequence<float>(inputShape, thisInput, starting, device);

                    inputDataMap.Add(inputVar, inputValue);

                    var outputDataMap = new Dictionary<Variable, Value>();

                    outputDataMap.Add(outputVar, null);

                    nn.Evaluate(inputDataMap, outputDataMap, device);


                    var outputVal = outputDataMap[outputVar];
                    var outputData = outputVal.GetDenseData<float>(outputVar)[0];

                    output.AddRange(outputData.ToList());
                    starting = false;
                }
                return output;
            }

最佳答案

C# 与 CNTK 的支持非常差。

一个代码示例:

public static Function Model { get; set; }


public Function Load(string modelFilePath, DeviceDescriptor device)
{

    return Function.Load(modelFilePath, device);
}

然后:

public void Evaluate(string word, function model, DeviceDescriptor device)
{

// Create Input Dictionary Pair:
Dictionary<Variable, Value> ModelInput = new Dictionary<Variable, Value>
{
    { model.Arguments.Single(), Value.CreateBatch<float>(XDim, new int[] { 7 }, DeviceDescriptor.GPUDevice(0), true) }
};

// Vector the Model's Output Variable.
Variable OutputVariable = model.Output;

// Create Output Dictionary Pair:
Dictionary<Variable, Value> ModelOutput = new Dictionary<Variable, Value>
{
    { OutputVariable, null }
};

// Evaluate the Model using the Device:
model.Evaluate(ModelInput, ModelOutput, device);

// Vector evaluate result as dense output
IList<IList<float>> OutputValue = ModelOutput[OutputVariable].GetDenseData<float>(OutputVariable);

IList<float> t = OutputValue[0];
int index = t.IndexOf(t.Max());

// Do what you need with your index...

}

注意:

Value.CreateBatch<float>(XDim, new int[] { 7 }

其中 7 作为单个稀疏变量在您的输入类长度内。您会注意到,有更好的方法可以做到这一点,这只是一个非常基本的示例。

我已经建立了一个网站:http://www.cntking.com/尝试在 C# 和 CNTK 上有所作为。

希望对您有所帮助!

关于c# - CNTK LSTM 评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46960003/

相关文章:

c# - .NET 4.5 如何创建此自定义声明类型?

c# - 在 C# Blazor 的部分类中初始化 RenderFragment

python - 模型训练时间较长

C# 字典层次结构到 xml 字符串?

python - Multi RNN 不适用于相同 Base LSTM 单元的列表

python - Tensorflow:使用不同的 "call"函数创建 LSTM 单元的自定义子类

python - CNTK python api - 继续分类器训练

python - CNTK中用户定义的激活函数

python - CNTK中如何设置设备进行特定操作?

c# - LINQ 的嵌套组