pub fn example_linear(
input: &Tensor,
weight: &Tensor,
bias: Option<&Tensor>,
) -> Result<Tensor>Expand description
Standard parameter ordering for functional operations
All functions in torsh-functional follow this parameter ordering convention:
- Primary input tensor(s)
- Required parameters (sizes, dimensions, etc.)
- Optional parameters with defaults
- Configuration flags (inplace, etc.)
Example: conv2d(input, weight, bias, stride, padding, dilation, groups)
Standard documentation template for functions
All functions should include:
- Brief description of what the function does
- Mathematical formula (if applicable)
- Parameter descriptions with types and constraints
- Return value description
- Error conditions
- Usage example
- References to related functions Standard error handling patterns
All functions should:
- Create a function context using
function_context() - Validate inputs using appropriate
validate_*functions - Use descriptive error messages with context
- Chain errors properly with
.map_err()Example function demonstrating all API conventions
Linear transformation with bias: y = xW^T + b
Formula: linear(x, W, b) = xW^T + b
§Parameters
input- Input tensor of shape (N, *, in_features)weight- Weight matrix of shape (out_features, in_features)bias- Optional bias vector of shape (out_features,)
§Returns
Output tensor of shape (N, *, out_features)
§Errors
Returns error if:
- Input tensor is empty
- Weight/bias shapes are incompatible with input
- Matrix multiplication fails
§Example
use torsh_functional::api_patterns::example_linear;
use torsh_functional::random_ops::randn;
let input = randn(&[2, 3], None, None, None).unwrap();
let weight = randn(&[4, 3], None, None, None).unwrap();
let bias = Some(randn(&[4], None, None, None).unwrap());
let output = example_linear(&input, &weight, bias.as_ref()).unwrap();
assert_eq!(output.shape().dims(), &[2, 4]);