microsoft / CNTK

Microsoft Cognitive Toolkit (CNTK), an open source deep-learning toolkit
https://docs.microsoft.com/cognitive-toolkit/
Other
17.5k stars 4.29k forks source link

(C#) The type of 1st parameter "arguments" of the function Trainer.TestMinibatch() #3189

Open sconan32 opened 6 years ago

sconan32 commented 6 years ago

I'm copying the python example CNTK 105 to a C# version. The example implements a simple autoencoder.

When it comes to the Error Testing part, I use the trainter.TestMinibatch function.

    # We are loading test data in batches specified by test_minibatch_size
    # Each data point in the minibatch is a MNIST digit image of 784 dimensions
    # with one pixel per dimension that we will encode / decode with the
    # trained model.
    data = reader_test.next_minibatch(test_minibatch_size,
                                   input_map = test_input_map)

    # Specify the mapping of input variables in the model to actual
    # minibatch data to be tested with
    eval_error = trainer.test_minibatch(data)

In the above Python version, The type of 1st parameter seems to be the same as the return type of the function MinibatchSource.GetNextMinibatch(). But in the C# version, when I do it in the same way, I get an ERROR, which saying "Cannot convert the type from UnorderedMapStreamInfomationMinibatchData to UnorderedMapVariableMinibatchData".

Then, how can I get or build a UnorderedMapVariableMinibatchData object? Or is there a different way from the python version in using the Trainer class?

liqunfu commented 6 years ago

@sconan32 thanks for trying CNTK C#. We still need to work on high level C# API to ensure that it matches python API. Before that, an easier way will be to try out C# examples first.

for example, this usage: trainer.TrainMinibatch(new Dictionary<Variable, MinibatchData>() { { imageInput, minibatchData[imageStreamInfo] }, { labelsVar, minibatchData[labelStreamInfo] } }, device);

sconan32 commented 6 years ago

@liqunfu Thank you for the reply.

I tried the above sample after I had read the C# examples.

Yes. The TrainMinibatch() method can work with a dictionary perfectly. But all of the TestMinibatch() method's overloads don't receive a dictionary as the parameter.

Here are all the TestMinibatch() declaration:

public double TestMinibatch(UnorderedMapVariableMinibatchData arguments); public double TestMinibatch(UnorderedMapVariableValuePtr arguments, DeviceDescriptor computeDevice, bool distributed); public double TestMinibatch(UnorderedMapVariableValuePtr arguments, DeviceDescriptor computeDevice); public double TestMinibatch(UnorderedMapVariableValuePtr arguments); public double TestMinibatch(UnorderedMapVariableMinibatchData arguments, UnorderedMapVariableValuePtr outputsToFetch, DeviceDescriptor computeDevice); public double TestMinibatch(UnorderedMapVariableMinibatchData arguments, UnorderedMapVariableValuePtr outputsToFetch); public double TestMinibatch(UnorderedMapVariableValuePtr arguments, UnorderedMapVariableValuePtr outputsToFetch, DeviceDescriptor computeDevice, bool distributed); public double TestMinibatch(UnorderedMapVariableValuePtr arguments, UnorderedMapVariableValuePtr outputsToFetch, DeviceDescriptor computeDevice); public double TestMinibatch(UnorderedMapVariableValuePtr arguments, UnorderedMapVariableValuePtr outputsToFetch); public double TestMinibatch(UnorderedMapVariableMinibatchData arguments, DeviceDescriptor computeDevice, bool distributed); public double TestMinibatch(UnorderedMapVariableMinibatchData arguments, DeviceDescriptor computeDevice); public double TestMinibatch(UnorderedMapVariableMinibatchData arguments, UnorderedMapVariableValuePtr outputsToFetch, DeviceDescriptor computeDevice, bool distributed);`

Is there a workaround in calling the TestMinibatch method?

rikara commented 5 years ago

I do it like this ,and it works well for me :

void UnorderedMapVariableMinibatchData AsUnorderedMapVariableMinibatchData(IDictionary<Variable, MinibatchData> input) { UnorderedMapVariableMinibatchData data = new UnorderedMapVariableMinibatchData(); foreach (var kv in input) { data.Add(kv.Key, kv.Value); } return data; }

trainer.TestMinibatch(AsUnorderedMapVariableMinibatchData(inputDataMap), device);

viraptor commented 3 years ago

It's possible to do this by manually adding the entries, but in the latest version (mid 2021), there's still the argument disparity. It would be great if IDictionary was accepted.