|
FlexNN 1
Fully connected neural network built from scratch with flexible n-layer design and multiple activations.
|
#include <Conv1D.hpp>
Public Member Functions | |
| Conv1D (Conv1DParams p, Activations::Activation act=Activations::Activation::ReLU, const Activations::ActivationParameters ¶ms=Activations::ActivationParameters{}) | |
| Construct from params and activation. | |
| Conv1D (int inChannels, int outChannels, int kernelSize, int stride=1, int padding=0, int dilation=1, Activations::Activation act=Activations::Activation::ReLU, const Activations::ActivationParameters ¶ms=Activations::ActivationParameters{}) | |
| Convenience ctor with explicit ints. | |
| LayerType | type () const noexcept |
| Activations::Activation | activation () const noexcept |
| const Activations::ActivationParameters & | activationParams () const noexcept |
| const Conv1DParams & | params () const noexcept |
| const Eigen::MatrixXd & | weights () const noexcept |
| const Eigen::VectorXd & | biases () const noexcept |
| void | setWeights (const Eigen::MatrixXd &W) |
| void | setBiases (const Eigen::VectorXd &b) |
| void | setActivationParameters (const Activations::ActivationParameters &p) |
| std::pair< Eigen::MatrixXd, Eigen::MatrixXd > | forward (const Eigen::MatrixXd &input) const |
Forward: Z[oc][ol] = b[oc] + Σic Σk W[oc][ic*K+k] * Xpad[ic][ol*stride - pad + k*dilation] | |
| Eigen::MatrixXd | backward (const Eigen::MatrixXd &nextW, const Eigen::MatrixXd &nextdZ, const Eigen::MatrixXd &currZ) const |
| Backward for hidden layers (old signature, for dense_test compatibility). | |
| Eigen::MatrixXd | backward (const Eigen::MatrixXd &upstream, const Eigen::MatrixXd &currZ) const |
| Backward with already-propagated upstream gradient. | |
| Eigen::MatrixXd | propagate (const Eigen::MatrixXd &dZ) const |
Propagate gradient to previous layer: dX = W^T * dZ via col2im. | |
| std::pair< Eigen::MatrixXd, Eigen::VectorXd > | grad (const Eigen::MatrixXd &dZ, const Eigen::MatrixXd &input) const |
| Compute gradients dW and db for SGD update. | |
| void | update (const Eigen::MatrixXd &dW, const Eigen::VectorXd &db, double lr) noexcept |
| void | updateWeights (const Eigen::MatrixXd &dW, const Eigen::VectorXd &db, double lr) noexcept |
|
explicit |
Construct from params and activation.
Initializes W and b with Random * 0.5 (same as Dense for v0.1).
| p | Conv1DParams with inChannels/outChannels/kernelSize/stride/pad/dilation |
| act | Activation (default ReLU) |
| FlexNN::Layers::Conv1D::Conv1D | ( | int | inChannels, |
| int | outChannels, | ||
| int | kernelSize, | ||
| int | stride = 1, |
||
| int | padding = 0, |
||
| int | dilation = 1, |
||
| Activations::Activation | act = Activations::Activation::ReLU, |
||
| const Activations::ActivationParameters & | params = Activations::ActivationParameters{} |
||
| ) |
Convenience ctor with explicit ints.
|
inlinenoexcept |
|
inlinenoexcept |
| Eigen::MatrixXd FlexNN::Layers::Conv1D::backward | ( | const Eigen::MatrixXd & | nextW, |
| const Eigen::MatrixXd & | nextdZ, | ||
| const Eigen::MatrixXd & | currZ | ||
| ) | const |
Backward for hidden layers (old signature, for dense_test compatibility).
Computes dA = W_next^T * dZ_next then ‘dZ = f’(Z) ⊙ dA. Kept forDense-style testing; new code should usebackward(upstream, currZ)`.
| Eigen::MatrixXd FlexNN::Layers::Conv1D::backward | ( | const Eigen::MatrixXd & | upstream, |
| const Eigen::MatrixXd & | currZ | ||
| ) | const |
Backward with already-propagated upstream gradient.
| upstream | Gradient w.r.t. A of this layer (already W_next^T*dZ_next or propagate result) |
| currZ | Pre-activation Z of this layer from forward |
|
inlinenoexcept |
| std::pair< Eigen::MatrixXd, Eigen::MatrixXd > FlexNN::Layers::Conv1D::forward | ( | const Eigen::MatrixXd & | input | ) | const |
Forward: Z[oc][ol] = b[oc] + Σic Σk W[oc][ic*K+k] * Xpad[ic][ol*stride - pad + k*dilation]
| input | Flattened input [C_in*L_in × batch] (each column is a sample) |
[C_out*L_out × batch] | std::pair< Eigen::MatrixXd, Eigen::VectorXd > FlexNN::Layers::Conv1D::grad | ( | const Eigen::MatrixXd & | dZ, |
| const Eigen::MatrixXd & | input | ||
| ) | const |
Compute gradients dW and db for SGD update.
Uses im2col: col [C_in*K × L_out] per sample, dW = Σ_n dZ_n * col_n^T / batch, db = rowMean over (batch*L_out) per channel (mean over batch and spatial).
| dZ | Gradient w.r.t. Z [C_out*L_out × batch] |
| input | Original input to forward [C_in*L_in × batch] |
[outChannels × inChannels*K], db is [outChannels]
|
inlinenoexcept |
| Eigen::MatrixXd FlexNN::Layers::Conv1D::propagate | ( | const Eigen::MatrixXd & | dZ | ) | const |
Propagate gradient to previous layer: dX = W^T * dZ via col2im.
For Conv1D, this is the transpose convolution (col2im). Input dZ is [C_out*L_out × batch], output dX is [C_in*L_in × batch] where L_in is derived from dZ's implied L_out and params. The caller must know L_in — we derive it from dZ rows and params assuming the forward L_out formula. For the common case where Conv1D is followed by Dense, L_in is not needed because Dense's dZ is already flattened; this method is used when Conv1D is the next layer for a previous Dense — the Dense's upstream is propagate(dZ_next) from Conv1D.
| dZ | Gradient w.r.t. Z of this Conv1D layer [C_out*L_out × batch] |
[C_in*L_in × batch] (derived)
|
inline |
|
inline |
|
inline |
|
inlinenoexcept |
|
noexcept |
|
inlinenoexcept |
|
inlinenoexcept |