pub struct Conv<B: Backend, T: Float = f32> {
pub kernel: Array<B, T>,
pub bias: Option<Array<B, T>>,
pub strides: Vec<usize>,
pub padding: Padding,
pub input_dilation: Vec<usize>,
pub kernel_dilation: Vec<usize>,
pub feature_group_count: usize,
}Expand description
A convolution of channels-last inputs with an HWIO kernel, and a bias
(flax.linen.Conv).
The input has dimensions (*batch, spatial..., features): one batch
dimension is used as it is, several are flattened into one and restored,
and none is allowed (a batch of one is added and removed), as in Flax.
The configuration (Conv::strides, …) is not part of the weights.
§Examples
use ruxla_core::lax::Padding;
use ruxla_core::{Array, Native};
use ruxla_nn::Conv;
// One input and one output feature, a kernel of width 2.
let conv = Conv::<Native>::new(Array::from_vec(vec![1.0, 10.0], [2, 1, 1]), None)
.padding(Padding::Valid);
let y = conv.apply(&Array::from_vec(vec![1.0, 2.0, 3.0], [3, 1]));
assert_eq!(y.to_vec(), [21.0, 32.0]);Fields§
§kernel: Array<B, T>[*kernel_size, in_features / feature_group_count, features].
bias: Option<Array<B, T>>[features], if the layer has a bias (use_bias).
strides: Vec<usize>The step between windows in each spatial dimension.
padding: PaddingThe padding ('SAME' by default, as in Flax).
input_dilation: Vec<usize>The dilation of the input in each spatial dimension.
kernel_dilation: Vec<usize>The dilation of the kernel in each spatial dimension.
feature_group_count: usizeThe number of groups of features.
Implementations§
Source§impl<B: Backend, T: Float> Conv<B, T>
impl<B: Backend, T: Float> Conv<B, T>
Sourcepub fn new(kernel: Array<B, T>, bias: Option<Array<B, T>>) -> Self
pub fn new(kernel: Array<B, T>, bias: Option<Array<B, T>>) -> Self
A layer with Flax’s defaults: unit strides and dilations, 'SAME'
padding, and one group.
§Panics
Panics if the kernel has rank less than 3.
Sourcepub fn input_dilation(self, dilation: &[usize]) -> Self
pub fn input_dilation(self, dilation: &[usize]) -> Self
Sets the dilation of the input.
Sourcepub fn kernel_dilation(self, dilation: &[usize]) -> Self
pub fn kernel_dilation(self, dilation: &[usize]) -> Self
Sets the dilation of the kernel.
Sourcepub fn feature_group_count(self, groups: usize) -> Self
pub fn feature_group_count(self, groups: usize) -> Self
Sets the number of feature groups.
Source§impl<T: Float> Conv<Native, T>
impl<T: Float> Conv<Native, T>
Sourcepub fn load(weights: &Weights, prefix: &str) -> Result<Self, WeightsError>
pub fn load(weights: &Weights, prefix: &str) -> Result<Self, WeightsError>
Loads {prefix}/kernel and, if there is one, {prefix}/bias, such
as the weights of params/Conv_0 of a Flax model, with Flax’s
default configuration (see Conv::new; set the rest with
Conv::strides, …).
§Errors
Returns an error if the kernel is missing or a weight has another type, or if the shapes do not fit together.