CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

C# API Doesn't Support Creating K-hot Sparse Value ?

Open MS-NLP opened this issue 8 years ago • 2 comments

I've a very simple CNTK model for binary classification: Input: a 10000-dim sparse vector where only k elements can be non-zero. (not sequence) Output: class label

I successfully trained the model using Python API, where the input vector is defined as a sparse vector.

# reader:
feature = C.io.StreamDef(field='X', shape=10000, is_sparse=True),
# variable definition:
input = C.input_variable(shape=10000, is_sparse=True, name="feature")

However, when I tried to use the C# API to evaluate the model, it seems that there is no method to create a simple k-hot sparse vector in Non-Sequence case. Could you please have a look?

MS-NLP avatar Jan 16 '18 03:01 MS-NLP

Big Problem - Yes!

System.ArgumentOutOfRangeException: 'Dense input data supplied for sparse input Variable 'Input...

using a known Sparse array:

Input = new List<List<float>>{ new float[] { 0,0,0,0,0,0,0,0,1 }.ToList() };

Even:

System.Numerics.Tensors.SparseTensor<float> Input = new System.Numerics.Tensors.SparseTensor<float>(new int[] { 9 }, true, 1);

Does not work!

Please, really need a fix for this!

GuntaButya avatar Apr 11 '19 22:04 GuntaButya

I have found a solution:

// The Model's Input Dimensions:
int inputDim = 38912;

// The Input:
int[] Input = new int[] { 7 }; //** = 7:1 The Index of the Input Array Vector or Tensor or what ever you want to call it.

// The Public Evaluate Method:
Variable XVariable = model.Arguments.Single();

// Creates a single Batch:
Value XValue = Value.CreateBatch<float>(inputDim, InputValue, device, false);

// Create Input Dictionary Pair:
Dictionary<Variable, Value> ModelInput = new Dictionary<Variable, Value>
{
    { XVariable, XValue }
};

// 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);

In my opinion this is totally NON-Intuitive, as the Sparse Vector is created in the CreateBatch Method, we do not parse any Vectors of any sort.

It works and I am happy its now working!

CNTK is AWESOME Thanks to all CNTK Engineers! However, PLEASE Document the Core!

P.S: I also posted this code here: https://github.com/microsoft/CNTK/issues/3641

GuntaButya avatar Jul 17 '19 23:07 GuntaButya