CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

A bug in SqareError function?

Open KolyaIvankov opened this issue 9 years ago • 9 comments

Following @mortengryning in #1154 and @slaweks17 lines in issue #1094 .

Could there be a bug in the SquareError function?

I've used it in a variant of RegrSimple_CIFAR10, a simple regression model mentioned in #1154 , and got a rmse that disagreed completely with my observations when I've been evaluating the trained model from my C# project. Then, following @slaweks17, I have re-defined the square error function on my own. Since I have only 1 regression label, it was possible for me to simply write

	rmse = Sqrt ((regrLabels - ol) * (regrLabels - ol))

in the BrainScript network description. Now, with that I get the rmse I can finally trust: it's some 10 times as large as what I presumably get with SquareError, but at least it is just as big as what I see in my application.

It seems as if the actual age getting divided by 10, despite of the fact that I've changed regrLabels and labelDim to 1 at each and every instance they were not in the original file.

@mortengryning and @slaweks17 reported having analogous problems with the Python implementation. Both were "solved" by writing a new loss function (latter) or switching to the beta3 version (former).

KolyaIvankov avatar Dec 12 '16 15:12 KolyaIvankov

@KolyaIvankov, when you run RegrSimple_CIFAR10.cntk, does the square error function behave normally?

If it does, we need a small sample data set to replicate your observation. Would it be possible for you to share us code + data that we can debug with? Thanks!

cha-zhang avatar Dec 13 '16 23:12 cha-zhang

@cha-zhang Sorry in advance if I did something stupid.

UPD: I've been using Beta6 here.

So, in order to test the whole thing, I've changed the config file Regr_Simple_CIFAR10.CNTK, replacing sqerr = SquareError (regrLabels, ol) with sqerr = TransposeTimes(regrLabels - ol, regrLabels - ol) which seemingly must do the same. Actually it possibly does not, since the listings show me different patterns: with SquareError the last rows of validation read

Validating --> sqerr = SquareError (regrLabels, ol) : [3 x *2], [3 x *2] -> [1]
Validating --> rmse = ElementTimes (rmse.ElementTimesArgs[0], sqerr) regrLabels, ol) : [1 x 1], [1] -> [1]

while for the homemade square function I get

Validating --> sqerr.leftMatrix = Minus(regrLabels, ol) : [3 x *2], [3 x *2] -> [3 x *2]
Validating --> sqerr.rightMatrix = Minus(regrLabels, ol) : [3 x *2], [3 x *2] -> [3 x *2]
Validating --> sqerr = TransposeTimes (sqerr.leftMatrix, sqerr.rightMatrix) : [3 x *2], [3 x *2] -> [1 x *2]
Validating --> rmse.z = ElementTimes (rmse.z.ElementTimesArgs[0], sqerr) regrLabels, ol) : [1 x 1], [1 x *2] -> [1 x *2]
Validating --> rmse = Sqrt(rmse.z) : [1 x 1 x 2*] -> [1 x 1 x 2*]

I'm still not familiar with the notation, so this additional dimension (?) in the rmse somewhat bothers me.

Well, anyway, with this error the rmse on the train data has trouble passing the border 0.005 (tried on 2 to 7 generations), and on test data the rmse appears to be about 0.13. This seems to be immense, therefore I'd rather think I've made a mistake. Still, could someone with more experience please check this out independently or point out what am I doing wrong?

KolyaIvankov avatar Dec 14 '16 14:12 KolyaIvankov

We discovered a bug in TransposeTimes in the current master branch, which will be fixed soon.

The following code, however, seems to be equivalent to SquareError:

sq = (regrLabels - ol) .* (regrLabels - ol)
sqerr = SumElements(sq)

cha-zhang avatar Dec 14 '16 23:12 cha-zhang

I have tried the function you describe, and it indeed provides the same results as SquareError. Right now I'm having an incompatibility of my model file with the Beta5 NuGet package (it requires v16 whereas beta5 is v15). As soon as I get the newest package I'll be checking the model on the data hands-on and let you know.

Thanks!

KolyaIvankov avatar Dec 15 '16 09:12 KolyaIvankov

It turns out there is no bug in the SquareError function. However, in order to compute RMSE, one can not use SquareError because it's already accumulated across the minibatch axis as well.

The correct implementation would be:

        diff = regrLabels - ol
        sqerr = ReduceSum (diff.*diff, axis=1) 
        rmse =  Sqrt (sqerr / labelDim)

We will update the master soon. Thanks for the help!

cha-zhang avatar Dec 15 '16 17:12 cha-zhang

Hi,

I have tried several further models with the code snipped You have provided. The results looked better at first, but then when I've tried to train the model, everything went astray again.

Thus, for instance, if the data is bad and unfiltered, one would expect that a regression's equivalent of "no idea" would be to stick to the mean value. Yet whenever I evaluate my model, the estimate is always far below the actual mean. The rmse seems to be very different from the real value as well.

I will consider sending You the data for replication.

KolyaIvankov avatar Dec 19 '16 16:12 KolyaIvankov

Sounds good. Even if you have some synthetic data that can replicate the issue, we would love to chase down the issue.

The RMSE equation above, however, I believe is right. Maybe there's something else going on.

cha-zhang avatar Dec 19 '16 16:12 cha-zhang

Hi,

I did provide the replication code in https://github.com/Microsoft/CNTK/issues/1094

Regards,

Slawek

[https://avatars3.githubusercontent.com/u/8934797?v=3&s=400]https://github.com/Microsoft/CNTK/issues/1094

Regression does not work with Python APIs · Issue #1094 ...https://github.com/Microsoft/CNTK/issues/1094 github.com Hi, I have been trying unsuccessfully to run a simple vector-to-vector regression using Python APIs (beta 4 and 5). It appears to me that there is something wrong ...


From: Cha Zhang [email protected] Sent: Monday, December 19, 2016 08:29 To: Microsoft/CNTK Cc: slaweks17; Mention Subject: Re: [Microsoft/CNTK] A bug in SqareError function? (#1160)

Sounds good. Even if you have some synthetic data that can replicate the issue, we would love to chase down the issue.

The RMSE equation above, however, I believe is right. Maybe there's something else going on.

You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/Microsoft/CNTK/issues/1160#issuecomment-268009359, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AIhVjfZnKBNLYFw5i8_eFOOy3JShgAJAks5rJrDrgaJpZM4LKpUe.

slaweks17 avatar Dec 19 '16 18:12 slaweks17

Just replied that thread. Seems you set the learning rate too high?

cha-zhang avatar Dec 20 '16 00:12 cha-zhang