Any operation similar to "reshape" available in numpy

Jul 16, 2015 at 8:54 AM
Edited Jul 16, 2015 at 10:06 AM
I was wondering if there is any function through which I could reshape a matrix in CNTK. Example: given a 18 x 1 matrix, I would want to convert it into 6 x 3 matrix.

I did check the documentation and did not find anything straight forward. I hope I did not miss anything. Any help in this regard would be greatly appreciated.

Jul 16, 2015 at 5:57 PM
yes. it's called Reshape. Bhaskar is adding document for it but it's straight forward if you search for ReshapeNode.
Marked as answer by mgautam on 7/16/2015 at 8:59 PM
Jul 17, 2015 at 12:12 AM
Edited Jul 17, 2015 at 4:59 AM
Thank you. I was running a very old version of the CNTK and the ReshapeNode was not available. I have obtained the latest version though.
Jul 17, 2015 at 5:27 AM
Dear Dongyu,
With the new CNTK, I am getting this below error:

cntk: DataReader/HTKMLFReader_linux/utterancesourcemulti.h:655: size_t msra::dbn::minibatchutterancesourcemulti::lazyrandomization(size_t): Assertion `randomizedchunks[i].empty() || (randomizedchunks[i].back().utteranceposend() == numutterances && randomizedchunks[i].back().globalte() == sweepts + _totalframes)' failed.

I tried running the CNTK with the my older ASR setups and it is still giving the same error. (I do not get this error with the older version of CNTK). The executables were generated from Makefile_kaldi.gpu. I hope I am not missing something very trivial.
Jul 17, 2015 at 6:47 AM
are you using the master branch or something else? This error seems to be strange.
Jul 17, 2015 at 8:01 AM
Yes. I have just downloaded today morning. I clicked on the "download" link on the source code panel.
Jul 17, 2015 at 8:06 AM
a fix is on the way.
Jul 23, 2015 at 4:55 AM
Edited Jul 23, 2015 at 4:55 AM
Dear Dongyu, Thank you. The issue has been fixed. However, I am tumbling upon another issue with the reshape node function:

I wanted to use the reshape node to convert a the below x to y.
As the function is meant for images, I have considered the channels to be 1.

x = Paramter(200,1)
y = Reshape(10,1,200,1,x) #10 rows (and 20 cols), 1 image width, 200 image height, 1 channel, x parameter

I hope I am doing right. I was constantly getting an error and so I tried by creating an Image input and verify. However, I am still getting a null ptr error. Below is the error I am getting:

cntk: MachineLearning/CNTK/SynchronousExecutionEngine.h:727: std::vector<void*> Microsoft::MSR::CNTK::SynchronousNodeEvaluator<ElemType>::EvaluateParameters(Microsoft::MSR::CNTK::NDLNode<ElemType>*, const wstring&, int, int, Microsoft::MSR::CNTK::NDLPass) [with ElemType = float; std::wstring = std::basic_string<wchar_t>]: Assertion `np != nullptr' failed.

I am not sure whether the parameters I am giving is incorrect or the node is not being created properly.

Thank you for taking your time.
Jul 23, 2015 at 10:12 AM
Looks like an issue with the ordering of the arguments. Can you please try the following and let me know if that fixes it?
y = Reshape(x, 10, imageWidth=1, imageHeight=200, imageChannels=1)
Marked as answer by mgautam on 7/23/2015 at 6:42 PM
Jul 24, 2015 at 2:42 AM
Edited Jul 27, 2015 at 11:02 AM
Dear Bhaskar,

Thank you for the update. I am getting some errors with the ReshapeNode. The actual script is as follows:

WT_SLICE=RowSlice(0,901120, BN_L3.DO) #BN_L3.DO - output of BN_L3 layer
SetInput(L1.*.T, 0, WT_RESHAPE)

L1 represents the first layer and its input is a 440 dimensions feature. I am adding some more layers to an existing trained DNN.

During validation the log file is as follows:

Validating --> WT_RESHAPE = Reshape(WT_SLICE[901120, 9], NumOfRows=2048, imageWidth=0, imageHeight=0, imageChannels=0)
L1.SBFF.BFF.FF.T = Times(WT_RESHAPE[2048, 3960], featNorm.xNorm[440, 9])
EXCEPTION occurred: The Matrix dimension in the Times operation does not match.

The parameter WT_RESHAPE is having a dimension of 2048 * 3960. I am expecting a dimension of 2048 * 440 (901120/2048 = 440). Is this because of the value 9 in WT_SLICE[901120, 9] (3960 = 440 * 9). Or I am doing something terribly wrong.
Jul 25, 2015 at 7:39 PM
Hi Gautam,

The image dimensions are optional parameters and therefore needs to be named arguments.
WT_RESHAPE=Reshape(WT_SLICE, 2048, imageWidth=1, imageHeight=901120, imageChannels=1) 
But this will also fail because the row size should be equal to the product of the image width, height and channels. Can you please specify the exact input and output matrix dimensions you are expecting?

Jul 26, 2015 at 6:47 AM
Edited Jul 26, 2015 at 7:14 AM
Hi Bhaskar,


I have a parameter WT_FLAT of size [901120 * 1]. I want to convert to WT_RESHAPE of size [2048 * 440]. Please note that WT_FLAT is not of image format. So I do not have any value for imageChannels.

Also after retrieving the WT_RESHAPE, I use it for further computation on an input feature (also not in image format and the vector dimension is 440). I hope this function is not limited to only image formats.


On a side note, based on your comment, I have tried the following command:


The above command is not giving any error in the reshape. However, the dimensions of the WT_RESHAPE does not match with that of the input feature and the following error is obtained:

L1.SBFF.BFF.FF.T = Times(WT_RESHAPE[901120 {W=440, H=2048, C=1}, 9], featNorm.xNorm[440, 9])EXCEPTION occurred: The Matrix dimension in the Times operation does not match.

The above error: Is it because the variables are not of Image format ?

Thank you for the help.

With Regards,
Gautam Mantena
Jul 27, 2015 at 10:11 AM
Hi Gautam,

In your case you can ignore the optional image dimension related arguments.
WT_RESHAPE=Reshape(WT_SLICE, 2048) #Assuming 2048 is the desired output row size.
Please let me know in case you have more issues related to this node.

Jul 27, 2015 at 11:08 AM
Hi Bhaskar,

That still does not solve the issue. It comes to one of my earlier problems as reported. It is the response to your first reply (time: Friday at 9.42 AM). WT_RESHAPE size is coming to be 2048 * 3960 (where 3960 = 440 * 9).

During validation, a value 9 is being included in the columns of the matrices, even though I declare them row matrices.

I am reposting the error again:

Validating --> WT_RESHAPE = Reshape(WT_SLICE[901120, 9], NumOfRows=2048, imageWidth=0, imageHeight=0, imageChannels=0)
L1.SBFF.BFF.FF.T = Times(WT_RESHAPE[2048, 3960], featNorm.xNorm[440, 9])
EXCEPTION occurred: The Matrix dimension in the Times operation does not match.
Jul 27, 2015 at 11:22 AM
That's because your input matrix is 901120 x 9 instead of 901120 x 1.

Just to make sure we are on the same page, the function of ReshapeNode is to transform a matrix with R1 rows and C1 columns to a new matrix R2 rows and C2 columns where R2 is provided as an argument (NumRows) and C2 is computed as (R1 * C1 / R2).

So if your input matrix is 901120 x 9 and your NumRows = 2048 then you will get an output matrix of dimensions 2048 x 3960.
And if your input matrix is 901120 x 1 and your NumRows = 2048 then you will get an output matrix of dimensions 2048 x 440.

Hope that helps.

Jul 27, 2015 at 12:03 PM
Hi Bhaskar,

Yes, I understand what you said. My issue is that I did not create a 901120 x 9 matrix. Following is a, example part of the model dump

featNorm.xStdDev=InvStdDev ( features ) [440,1] HasComputed=true

features=InputValue [440,9]

In the above line, my features are just 440 dimensions. Why is CNTK considering it as 440 * 9 instead of 440 * 1.

On another example consider the log dump of the following:

L2.SBFF.BFF.FF.P[2048, 9] = Plus(L2.SBFF.BFF.FF.T[2048, 9], L2.SBFF.BFF.B[2048, 1])

If you look at the above line 2 matrices are being added of different dimensions. The above example is taken from a working example and the model actually works. I never reported such as issue previously because I have assumed that 9 is being used for some internal process.
Jul 27, 2015 at 9:08 PM
What's your specified mini-batch size? The "9" most likely corresponds to the number of samples in the specific mini-batch.

Note that the last mini-batch (depending on your configuration) could return less than the requested size if not that many samples are available. For example if your requested mini-batch size is 1000 and you have (say) 4015 samples in your training data then the last mini-batch can be just 15 samples (depends of course on how the specific reader is implemented).
Jul 27, 2015 at 11:05 PM
I am using a mini batch of size 256 and I am using HTKMLF Reader.
Jul 27, 2015 at 11:47 PM
OK so this isn't related to the ReshapeNode. It's hard to comment without looking at the full validation message.

Dong - do you have any insights on why this might be happening?

Jul 28, 2015 at 6:23 AM
Also, I just remembered that the validation step (unlike at execution time) considers only one sample (not the whole minibatch). So some node before the ReshapeNode is generating the 9 columns. Hard to debug though without looking at the full validation stack.

Disclaimer: I am not familiar with HTKMLFReader. So may not be the best person to help you debug this.
Aug 1, 2015 at 8:24 PM
I think in your case each sample is 440 and you have 9 samples in each minibatch? If that's the case, the matrix multiplication does not work. you need to reformulate it so that even if it's a minibatch it can be computed as batches. The plus operation is different since it allows to add a vector to a matrix columnwise without changing the semantics. 165
Aug 2, 2015 at 12:25 AM
My mini batch size is 256.
Aug 2, 2015 at 9:54 AM
Dong - please correct me if I am wrong but at the time of the network validation CNTK assumes only one sample and not a full mini-batch*, right?


*Which makes sense because some readers can return partial mini-batches.
Aug 5, 2015 at 5:55 AM
CNTK will validate the network several times: when the model is to be saved, and when a computation is to be executed on a node. In the first case the mini-batch size is assumed to be 1 and in the actual computation stage it will be whatever specified by the user.