Regression Network with Multiple Outputs and UCI Fast Reader
Hi, I am unable to get a regression network with multiple outputs to work using UCIFastReader. Is this possible?
If I define a network with 2 output nodes like: SimpleNetworkBuilder = [ layerSizes = 12:2 ...
and a UCIFastReader reader labels section as: labels = [ labelType = "regression" dim = 2 start = 12 labelMappingFile = "$DataDir$/label-mapping.txt" ]
...where label-mapping.txt is an empty file.
When I run the train command it ends with the following: EXCEPTION occurred: NotifyFunctionValuesMBSizeModified: labels InputValue operation had its row dimension 2 changed by the reader to 1.
If I change config to a network with one output node and change the reader section to dim = 1, then it works, so I think this somehow has to do with not being able to use the UCIFastReader for regression with multiple outputs, if that is the case - is there a way to do this with some different reader?
I have pushed a change at branch dongyu/UCIFastReaderFix. can you check whether it addressed your problem?
Be noted that in the regression mode you don't need the label mapping file and it has no effect if it's there.
Thanks for the change.
I've tried the branch, the error is gone, but it does not work as I expect; on a simple test case with 1 input and 2 outputs, when I write out the predictions both outputs have very similar values while I expect the two outputs to be different significantly. It is as if both outputs have actually been trained on the same column of data rather than two different columns.
As for the labelMappingFile and regression, I get the following exception if I do not specify the labelMappingFile value for the reader: EXCEPTION occurred: configparameters: required parameter missing: SimpleWithOneAmir.config:SimpleWithOne_Demo_Train:reader:labels:labelMappingFile
can you send me your setup to reproduce your problem? at least for the label mapping it should work.
Thanks, Dong Yu (俞栋)Sent from my smart phone. Please forgive my typos.
Thanks for the change.
I've tried the branch, the error is gone, but it does not work as I expect; on a simple test case with 1 input and 2 outputs, when I write out the predictions both outputs have very similar values while I expect the two outputs to be different significantly. It is as if both outputs have actually been trained on the same column of data rather than two different columns.
As for the labelMappingFile and regression, I get the following exception if I do not specify the labelMappingFile value for the reader: EXCEPTION occurred: configparameters: required parameter missing: SimpleWithOneAmir.config:SimpleWithOne_Demo_Train:reader:labels:labelMappingFile
Reply to this email directly or view it on GitHub: https://github.com/Microsoft/CNTK/issues/97#issuecomment-185895288
I ran your setup and did not observe the labelmappingfile issue. I tried removing lablemappingfile and labelDim (labelDim is only valid if you have categorical label in which case you can only use one column as the label in the csv file).
I did notice a bug when copying data from the parser to input nodes when there are multiple regression label columns. This is now fixed. Can you please try dongyu/UCIFastReaderFix again?
Tried the fix, works properly on my test case.
Also, as you mentioned, works fine if I do not specify lableMappingFile and labelDim.
Thank you.
Hi, I don't have the possibility to build the corresponding branch atm. Considering I'm also trying to perform a multidimensional regression and have the same "dimension x changed by the reader" exception, should I assume that won't work until next release? Any other option or a possibility to get my hands on a Build? Thanks in advance
The fix was done 22 days ago. This is likely in the latest binary already. @dongyu888, could you confirm?
Actually I don’t know. Alexey, do you know?
well the latest Binary is older than the fix is that right? Anyway, thanks for your quick answer. Maybe you can review my setup: I'm trying to make a variation of that model in TF to evaluate Board Go positions for end game territories.
I'm in c# so I wrote an alternate script to produce my own data. My training tile has game records with BoardSize = 19*19 = 361 columns for end game territories label vector ( values -1,0,1) and another 361 feature vector for mid game position ( values -1,0,1) for so my reader's:
features = [
dim = 361
start = 361
]
labels = [
dim = 361
start = 0
# labelDim = 3
labeltype = regression
labelMappingFile = "$DataDir$/labelsmap.txt"
]
If I comment the last line I get "parameter missing (...) labelmappingFile"
If I don't I get
EXCEPTION occurred: NotifyFunctionValuesMBSizeModified: features InputValue operation had its row dimension 784 changed by the reader to 361.
Now I'm also not very sure with my arrangement of convoluted layers and how to best re-project into the original label with an image dimension while preserving locality. Of course, knowing if there's a bug around would help to start with, but if you've got advises on how to handle that part it would be very nice. Will I get the same error if I get my dimensions wrong?
I started from the mnist example, with:
labels = ImageInput(imageW, imageH, 1, tag = label, imageLayout=$imageLayout$)
I commented pooling to start with, and I'll add maps when I get this first one right, now I suppose the end layers should look like :
h1Dim = 361
# DNNSigmoidLayer and DNNLayer are defined in Macros.ndl
# h1 = DNNSigmoidLayer(512, h1Dim, pool2, 1)
h1 = DNNSigmoidLayer(zzz, h1Dim, conv2_act, 1)
ol = DNNLayer(h1Dim, 361, h1, 1)
ce = CrossEntropyWithSoftmax(labels, ol)
err = ErrorPrediction(labels, ol)
Is that right? To get zzz, should I apply eq. (2.156) from the book and the output layer of the successive convoluted layers? Also, what should h1Dim be? Having comments about that computation in the code would be nice. Would you walk me to the appropriate ndl ?
Thanks in advance
You may need to build the current master branch.
My development environment is a virtual machine (no GPU) with only Vs 2015 installed and I wasn't able to build from source I'm sorry (many error messages starting from the Math project). Would you help me out with that?
You may not need it (yet). The error in NotifyFunctionValuesMBSizeModified() does actually not indicate a problem with your tensor dimensions. Rather, this error is triggered when an InputValue node (called "features" in your case) was declared and used by the network, but not filled by the reader. I will refine that error message.
So we must first find out why the reader is not filling the "features." First, make sure that your reader definition contains sections for both 'labels' and 'features', e.g. like 01_OneHidden.cntk. These section names are matched with node names.
Next, I notice that you are reading the images into 'labels' instead. You should read them into 'features'. Please try to change your "labels=" into "features=", and also change the tag to "features" (or alternatively say FeatureNodes=(features)".
For training, you still need labels. Please try to define an InputValue named 'labels', like the MNIST sample 01_OneHidden.ndl (in the section called ndlMnistMacros). Please don't, however, copy the definition of "features" from there, keep using your InputImage.
The second argument to DNNLayer is the label dimension. For MNIST, that should be 10, not 361. Sorry, the layers take their dimension parameters in the order "inputDim, outputDim", which is reverse to the matrix dimension (rows=outputDim, cols=inputDim). It confuses me too sometimes.
Your NDL seems fine otherwise. To set zzz, I would go the simple route, let CNTK give you an error message and get it from there. I.e., leave it at 512 (or anything), run CNTK, get it to the point where it fails due to a dimension mismatch in a matrix product (:Times operation") in this layer (the error message will tell you a node name that will indicate whether it is indeed the node in this DNNSigmoidLayer that is failing). The error message will also tell you the tensor dimensions of both inputs of the Times node, something like [N x 512] * [W x H x C x *] --> [N x *]. The values for (W,H,C) that are shown are the actual dimension of the data that the weight matrix of the DNNSigmoidLayer is applied to, and you need to modify zzz to match that.
I notice one error. The OneHidden sample may be outdated, as the weight matrix really must be a tensor (we recently changed that). So you will need to change the DNNSigmoidLayer to a DNNImageSigmoidLayer, which is defined in the Macros.ndl file in the same folder with this signature:
DNNImageSigmoidLayer(inW, inH, inC, outDim, x, parmScale) = ...
You need to set inW/H/C to match the dimensions of the second argument in the error message you will get for the Times operation. (Again, the parameter order is even more confusing, since W,H,C are inputs. The tensor dimension will be [outDim x inW x inH x inC].)
(I will also track down why this was not caught in automated tests.)
h1Dim is a design question. The MNIST sample 01_OneHidden uses 200. In the end, you will use larger training sets, so you would want to increase this and also the depth.
If you get errors related to convolution engines (a typical error), the imageLayout must be set correctly, that is, run on GPU (deviceId>=0) and set imageLayout="cudnn" in all convolution-related nodes. A way to know which nodes must have that parameter is to search through the example NDL files in the MNIST folder for the string 'imageLayout'.
Since this is your first setup, expect to run into more errors. Don't hesitate to ask (maybe open a new Issue if the topic changes).
Yikes, re-reading your mail, I realize the error indicates something slightly different.
row dimension 784 changed by the reader to 361
means the reader thinks the features node has dimension 361 (you indeed say so in your reader definition), while the features node itself is declared to be 784-dimensional.
But you declared your labels to be 19 x 19 (I presume that is correct in your scenario). Did you also declare the features to be 19 x 19? 784 is the value I see in the MNIST sample (=28 x 28). Please also adapt the definition of the features node. It is not enough to adapt it in the reader. Rather, reader and node dimensions must match.
Thanks a lot for your thorough explanation. I'm not there yet, but I somehow managed to move on a bit I believe. Several points then:
- I was able to build the latest source (Debug_CPUOnly) on a different environment, though the resulting binaries wouldn't work once copied to my development VM environment. When trying on the new environment, I did not get the error mentioning the missing labelmappingFile with that line commented in the reader section, so there seems to be a significant difference between release binaries and the build.
- As per your advices, I have updated the macro.ndl file from the latest MNIST model source version and turned the DNNSigmoidLayer from the last layer to a DNNImageSigmoidLayer
- From your 2nd message, I think you got that both my features and my labels are 1 channel maps with the size as the board 19 x 19 = 361, so the training file has rows with 361 *2 = 722 columns, and
the features definition, which I ommited in my initial message, is also
features = ImageInput(imageW, imageH, 1, tag = feature, imageLayout=$imageLayout$) - Somehow, the NotifyFunctionValuesMBSizeModified did vanish on both environment, I got to Times "operation error" to fix the product dimension as per your advice, which I did with h1 = DNNImageSigmoidLayer(11, 11, cMap2, h1Dim, conv2_act, 1)
I suppose that means that both convolutions with 5*5 kernels without stride applied from the original MNIST convolution example reduced the dimension by 5-1=4 each, which is consistent with eq. (2.156). On that note, the original article mentions an addition feature
final plane is all 1's so the convolutions can detect the edge of the board
I noticed the "zeroPadding" parameter in the convolution function, would I be able to keep the 19*19 dimension using the board detection plane trick, with that parameter?
Anyway I'm not there yet, and just trying to get something with 1 feature plane and similar 1 label plane for now.
So here is where I'm stuck now.
In my dev environment with the release binaries, cntk crashes with:
(...)
command: train test
precision = float
CNTKModelPath: ../Output/Models/02_Convolution
CNTKCommandTrainInfo: train : 5
CNTKCommandTrainInfo: CNTKNoMoreCommands_Total : 5
CNTKCommandTrainBegin: train
NDLBuilder Using CPU
Reading UCI file ../Data/Train-1F.txt
[CALL STACK]
>00007FF7FB00C453 (SymFromAddr error : Tentative dÔÇÖacc├¿s ├á une adresse n
on valide.)
(...)
-00007FF7FB043768 (SymFromAddr error : Tentative dÔÇÖacc├¿s ├á une adresse n
on valide.)
EXCEPTION occurred: ImageParameter should have 3 parameters [width, height, numC
hannels].
(...)
On the new environment with the source build, the same training file can be read properly it seems, and the model validated, but I've got a crash a bit later with:
Starting Epoch 1: learning rate per sample = 0.005000 effective momentum = 0.000000 momentum as time constant = 0.0 samples
UCIFastReader: Starting at epoch 0, counting lines to determine record count...
117 records found.
starting epoch 0 at record count 0, and file position 0
already there from last epoch
Starting minibatch loop.
About to throw exception 'Features matrix not found in config file, there should be a section 'features=[...]' in the configuration file.'
EXCEPTION occurred: Features matrix not found in config file, there should be a section 'features=[...]' in the configuration file.
[CALL STACK]
> Microsoft::MSR::CNTK::UCIFastReader<float>:: GetMinibatchImpl
- Microsoft::MSR::CNTK::UCIFastReader<float>:: GetMinibatch
- Microsoft::MSR::CNTK::DataReader:: GetMinibatch
- Microsoft::MSR::CNTK::DataReaderHelpers::GetMinibatchIntoNetwork<float>
- Microsoft::MSR::CNTK::SGD<float>:: TrainOneEpoch
- Microsoft::MSR::CNTK::SGD<float>:: TrainOrAdaptModel
- Microsoft::MSR::CNTK::SGD<float>:: Train
- DoTrain<Microsoft::MSR::CNTK::ConfigParameters,float>
- DoCommands<float>
- wmainOldCNTKConfig
- wmain1
- wmain
- __tmainCRTStartup
- wmainCRTStartup
- BaseThreadInitThunk
- RtlUserThreadStart
I'm not sure what I did wrong then, considering all we've said.
Thanks in advance for any follow up. And of course if you're up for it, I can generate and send you training files together with my configuration.
Glad to see we are quite a bit further!
I think the crash in UCIFastReader was a known out-of-bounds access in the randomization code in case the specified epochSize parameter is larger than the actual training set. The latest code has a workaround, but that might cause slightly suboptimal randomization. So even if it passes now, please could you ensure that your SGD epochSize parameter matches the training set.
As for the frame of '1', did I understand right, you want to add a second 19 x 19 plane (like a color channel) with 1s in the boundary positions, and ask whether the use of zero padding will retain the 19 x 19 dimension? Yes, it will.
Lastly, that error string about the missing "Features matrix" is wrong (just fixed it, but not yet in master). If I read the code right, it rather indicates that your reader config is fine but there is no feature node named "features" in your network. A feature node is one tagged as "feature", either by saying InputValue(..., tag = "feature", ..) or saying something like this in the NDL:
FeatureNodes = (features)
LabelNodes = (labels)
Could you check whether your "features" node matches the name used in the reader config and is tagged as a feature?
Thanks again for your renewed help.
I'm yet a little bit ahead, I was using featScaled= features (without parenthesis), taping directly into features seemed to do the trick
- I regenerated a slightly larger dataset with a little more than 450 pro games for training and 150 for testing. I have set the epoch size to 450 and epoch 1 runs on the build env (not on the dev environment with release binaries and the same reader error), but with 100% error, and I get a crash at the beginning of epoch 2:

Here is what I have in Vs when attaching to the process before hitting retry:

For reference, here are my training and test datasets
And here both cntk and ndl models.
02_Convolution.cntk.txt 02_Convolution.ndl.txt
- Now I didn't think past the dimensions at this point, and I suspect the EvalErrPerSample = 1 means there is still something wrong with the end layers, which may lead to the crash. Could it come from the -1/0/1 values?
- Finally about the edge thing. 0 stands for an empty cell on the board, so I suppose that zero padding, while retaining original size, will lead to implementing an edge less board a bit akin to torus Go, which really is a different game. I suppose adding a map with 1s covering the board serves marking the edges as walls rather than hidden empty cells.
Also, on that last remark about exploiting the board geometry: ideally, on top of making sure the edges are properly accounted for, I'd want to make sure:
- Symmetry invariance by 90° rotation is accounted for: would that involve generating symmetric samples in the dataset, or replicating features into additional maps with such rotations applied?
- Information is propagated along vertical and horizontal connections only (Manhattan distance). Unlike in the game of life, there are not diagonal connections. I suppose that would mean removing kernel corners in convolution layers with 0 values. Is there any way to enforce such a mask during training?
Finally in the conclusion of the original example, the author mentions that implementing connectedness would be interesting: indeed, chains of connected stones of the same color (along vertical and horizontal lines) share the same status, which it seems was not properly learnt by the yet rather successful end model. I suppose having some kind of "chain pooling" as suggested would be nice indeed, what would be a good way to go about it.
BTW, if I get anywhere with that model, I'll be happy to contribute the final setup for instance if you want to introduce a Games examples section.
Let me first reply to your last post. The ImageReader has abilities to crop and scale, but I don't think it can rotate. UCIFastReader has no such facilities. You would have to generate the data yourself.
As for filtering with masked corners, I think you should be able to use a convolution filter that is an element-wise product of a learnable parameter and a constant tensor, something like
maskedKernel = ElementTimes (kernel, mask)
out = Convolution (maskedKernel, ...).
To do that, you would have to:
- create such a mask as a file
- load it into a ImageParameter (..., init="fromFile", initFromFilePath="PATH_OF_THAT_FILE", learningRateMultiplier=0), where the last option makes it a constant (the predefined Constant() only works for scalars)
How to create the mask file: Let me assume that the mask has dimensions [W x H x C] but is identical for all C (if not, then it is a bit more complex). In that case, set C=1 and let ElementTimes broadcast it for you. Then you can just print out your W x H mask to an ASCII file. E.g. for a 3 x 3 filter with suppressed corners, you'd need 3 lines, one per matrix row, each with 3 values separated by whitespace:
0 1 0
1 1 1
0 1 0
Lastly, if you use Linux, you should be able to avoid writing a separate file by using a Perl-like inline pipeline command like this (I might have gotten the escaping wrong):
initFromFilePath="echo -e '0 1 0\n1 1 1\n0 1 0' |"
Under Windows, this works with Cygwin.
As for the UCIFastReader crash, I noticed that your training set seems to have 458 samples, not 450. Please try to set epochSize to 458. Sorry, trying to work around that bug.