This repository was archived by the owner on Jul 1, 2023. It is now read-only.
use .moments() in LayerNorm and BatchNorm layers #384
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Mean and variance in the layers are now calculated using
Tensor.moments()
. I also added tests for both BatchNorm and LayerNorm layers.The tests turned up a flaw in how the shape of the
scale
andoffset
which were always of shape[featureCount]
irrespective of the input shape or axis for normalisation. That shape leads to incorrect broadcasting when the axis being normalized along is not the last axis.I have fixed this by always reshaping
scale
andoffset
before they are used. This seems hacky in that I get the shapes from the calculatedmean
andvariance
. Without the input shape being known at initialization time though I couldn't see a better way to do this.I think the axis argument is probably there to be consistent with Keras but most of the Swift api layers assume inputs and activations are NHWC. So requiring NHWC, eliminating the axis argument, and the setting the correct shapes in
init()
would be another option.