Add linear_ layer for neural networks#3074
Merged
davisking merged 3 commits intodavisking:masterfrom May 3, 2025
Merged
Conversation
…des an optimized linear transformation for multi-dimensional inputs.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a new linear_ layer that provides an optimized linear transformation for multi-dimensional inputs while preserving spatial dimensions except for the feature one. The implementation supports both biased and unbiased modes and is integrated into the DNN API with corresponding test cases added.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| dlib/test/dnn.cpp | Added multiple tests exercising linear_ layer instantiations (e.g. linear_<1, LINEAR_NO_BIAS>) and a dedicated test_linear() function to validate outputs and gradients. |
| dlib/dnn/layers_abstract.h | Introduced the abstract definition for the new linear_ layer and the associated linear_bias_mode enum with clear documentation. |
| dlib/dnn/layers.h | Provided concrete implementations of the linear_ layer, including input/output mapping and wrapper aliases (linear and linear_no_bias). |
Comments suppressed due to low confidence (1)
dlib/test/dnn.cpp:3561
- [nitpick] Consider renaming 'xtmp' to a more descriptive name (like 'input_matrix') to improve code clarity in the test_linear() function.
matrix<float> xtmp(2, 4);
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Owner
|
Nice, thanks for the PR 😁 |
davisking
pushed a commit
to kSkip/dlib
that referenced
this pull request
May 24, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR adds a new
linear_layer that provides an optimized linear transformation for multi-dimensional inputs. The layer preserves the spatial dimensions of the input tensor while transforming the feature dimension.Features
This implementation is more efficient than reshaping inputs to use fc_ layers when working with multi-dimensional data.
Similar functionality exists in other major frameworks like PyTorch (
nn.Linear), which operates on the last dimension while preserving other dimensions.