tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

leCunNormal initializer

Open vmukhachev opened this issue 1 year ago • 5 comments

tf.moments(tf.initializers.leCunNormal().apply([1, 1, 1, 1000000], 'float32')).variance.print() prints something like: 0.7735087871551514

while it should be close to 1.0

The stddev passed to truncated normal distribution should be scaled by 1.0 / 0.87962566103423978 like it is done in https://github.com/keras-team/tf-keras/blob/v2.19.0/tf_keras/initializers/initializers.py#L662 . This is because truncated normal distribution has different variance to stdev relationship from regular normal distribution. The downscaling constant (0.87962566103423978 in this case) can be calculated like: sqrt(1 - 4 / sqrt(2 * pi) * exp(-0.5 * x**2) / erf(x / sqrt(2))), where x is the bound of the distribution over stddev (x = 2 in the code)

unfortunately the documentation is also ignoring that fact, so it is not clear what to change

vmukhachev avatar Oct 05 '25 19:10 vmukhachev

Hi @vmukhachev ,

Thanks for bringing the issue to us, and I am able to replicate the same.

Yes, it is true that the truncated normal distribution has a different variance when compared to the regular normal distribution.

I am escalating this to our eng team.

avmohansai avatar Nov 28 '25 07:11 avmohansai

Hi @vmukhachev, Thank you for bringing this to our attention and for providing a comprehensive overview. We appreciate the time you invested in creating this issue.

Currently, our internal development team is concentrated on other scheduled milestones, so we cannot prioritize this task immediately. However, we would love to see this move forward as a community-led effort.

If you’d like to submit a fix, please fork the repo and open a PR following the TensorFlow Contribution Guidelines, Tfjs Contributions and be sure to link this issue in your PR to avoid duplicate work, kindly leave a comment here if you plan to take this on.

Thank you again for your interest and time.

Varun-S10 avatar Jun 17 '26 09:06 Varun-S10

Hi @vmukhachev ,

Thank you for your patience, we have tried to reproduced this issue. As a workaround, you can manually use tf.initializers.varianceScaling instead of leCunNormal, but you must adjust the scale factor. Because TF.js takes the square root of the scale Math.sqrt(scale) to pass as stddev, we must square the variance correction constant.

Could you please try this and let us know if it works for you?

nileshau-afk avatar Aug 07 '26 04:08 nileshau-afk

that works, thanks

vmukhachev avatar Aug 08 '26 10:08 vmukhachev

Are you satisfied with the resolution of your issue? Yes No

google-ml-butler[bot] avatar Aug 08 '26 10:08 google-ml-butler[bot]