Fix batch handling for Add::Gradient()
#3679
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.
The current implementation of the
Add
layer (which just adds a bias to each element) does not properly handle batch sizes greater than 1. This can be seen in the example in #3666.The
Gradient()
function expects theerror
matrix to be of sizeoutSize
xbatchSize
, and the gradient matrix itself is already sized to beweightSize
x1
. (For theAdd
layer, it is conveniently true thatweightSize == outSize
.)However, the existing implementation of
gradient = error
is only correct whenbatchSize = 1
. Much like the Linear layer, the bias term's gradient is computed as the sum across all input points in the batch.I then noticed that the
Add
layer is not tested, so I added a simple test for it at a batch size of 1 and also at a larger batch size. (Previous to this PR, the second test will fail.)