microsoft / CNTK

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

FindAllWithName doesn't work in the C# API #2454

Open cesarsouza opened 7 years ago

cesarsouza commented 7 years ago

Hi there,

This is a direct translation of the Python code at https://www.cntk.ai/pythondocs/cntk.ops.functions.html#cntk.ops.functions.Function.find_all_with_name

var a = CNTKLib.InputVariable(NDShape.CreateNDShape(new[] { 1 }), CNTK.DataType.Double, name: "i");
var b = CNTKLib.InputVariable(NDShape.CreateNDShape(new[] { 1 }), CNTK.DataType.Double, name: "i");
var c = CNTKLib.Plus(a, b, name: "c");
var result = c.FindAllWithName("i");

In the C#/.NET API, results.Count is zero, while in the Python API, len(c.find_all_with_name('i')) is 2.

liqunfu commented 7 years ago

FindAllWithName C# API find all functions with the specified name. It is not the sample as python find_all_with_name. Here a and b are input variables. To find them, you may do this: var inputs = c.Inputs.Where(i => i.Name == "i");

cesarsouza commented 7 years ago

Hi @liqunfu

Many thanks for the answer! Indeed, using c.Inputs.Where instead of FindAllWithName does the trick and retrieves the variables named "i" in the example above.

However, wasn't FindAllWithName supposed to do the same? If not, why? Having two methods with almost the same name in the Python and C# APIs with different behaviors is quite confusing. Is there any documentation for the FindAllWithName method at this moment?

Regards, Cesar

liqunfu commented 7 years ago

2418 is about the same issue.

The C++ API behaves the same - returns only functions of the given name. It is mainly because C#/C++ are strong typed. To mix Functions with Variables is not desirable in C#. I am curious to know an use case where the python behavior is necessary in C#.

cesarsouza commented 7 years ago

Hi @liqunfu,

Thanks for the answer. It is not a matter of being necessary, but a mater of being consistent. If it is assumed that it should be the best for CNTK to have inconsistent behaviors across APIs, then that's also fine. However, those inconsistencies should be thorough mentioned across the documentation because it is very likely that there will be many users coming from Python to C# who might end up bumping into this issue (the fact that #2418 had been also been raised by another independent developer illustrates this).

Thanks again, Cesar

brianberns commented 6 years ago

@liqunfu: You asked for a use case. This would be very helpful for something like word2vec, where the desired embedding matrix is located inside a larger model. I'd like to be able to extract the matrix by name after training is complete. Since FindByName can't locate it, I have to store it a global variable when I create it in order to find it later.