Skip to main content

example_linear

Function example_linear 

Source
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:

  1. Primary input tensor(s)
  2. Required parameters (sizes, dimensions, etc.)
  3. Optional parameters with defaults
  4. 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:

  1. Create a function context using function_context()
  2. Validate inputs using appropriate validate_* functions
  3. Use descriptive error messages with context
  4. 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]);