microsoft / CNTK

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

How to set input shape with C++ codes? #3240

Open fffupeng opened 6 years ago

fffupeng commented 6 years ago

Codes are as follows. When i use cntk c++ api. data vector is created from data point. Then, inputvar and outputvar can be get from model loaded. But i want to set my own shape like (NDShape({ 1,28,28 }). Saddly, it not right. Programs run with errors. By the way , my dnn model's input is free dimension.

` std::vector inputData(NDShape({ 1,28,28 }).TotalSize()); for (size_t i = 0; i < inputData.size(); i++) inputData[i] = inputdata_m[i];

// Create input value and input data map
ValuePtr inputVal = Value::CreateBatch(NDShape({ 1,28,28 }), inputData, device);
std::unordered_map<Variable, ValuePtr> inputDataMap = { {inputVar, inputVal } };

// Create output data map. Using null as Value to indicate using system allocated memory.
// Alternatively, create a Value object and add it to the data map.
std::unordered_map<Variable, ValuePtr> outputDataMap = { { outputVar, nullptr } };

// Start evaluation on the device
modelFunc->Evaluate(inputDataMap, outputDataMap, device);
printf("Evaluate done!\n");`

How can i fix input or output shape when my dnn model's input is free dimension?

ke1337 commented 6 years ago

Clone and replace the input_variable with one with fixed dimensions?

fffupeng commented 6 years ago

Does it mean i can change input vector's shape to set my input data shape? But the input vector's dimension is equal to the batch size. It will be so kind of you to show me some codes about how to set input shape.

ke1337 commented 6 years ago

Batch size is considered dynamic regardless of FreeDimension. You could have a model with fixed input shape, but eval with different batch size on each invocation. Note that in CNTK when we talking about variable shape, it's the sample shape not including sequence/batch axis which is called dynamic axes.

FreeDimension means the dimension in variable shape could change from input to input. The actual variable shape would be inferred at runtime (with some perf cost). So if you are using FreeDimension, there is no need to fix input/output variable shape.