Foundations

Affine Projection

EasyRaw PyTorchAbout 10 min2 public examples plus private tests

Write and run your solution Sign in

Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.

Your task

Project the final feature axis of x using the supplied weight, then add bias if present. Return the projected tensor; all leading axes are unchanged.

Function signature

affine_projection(
    x: torch.Tensor,
    weight: torch.Tensor,
    bias: torch.Tensor | None = None,
) -> torch.Tensor

Worked example

Project two vectors

For the first vector, the dot products are 1+4=5 and 3-2=1. Adding the bias gives [5.5, 2]. For the second vector, the dot products are -1 and -3, so the result is [-0.5, -2]. Both leading rows are preserved in the (2, 2) output.

import torch

x = torch.tensor([[1.0, 2.0], [-1.0, 0.0]], dtype=torch.float64, requires_grad=True)
weight = torch.tensor([[1.0, 3.0], [2.0, -1.0]], dtype=torch.float64, requires_grad=True)
bias = torch.tensor([0.5, 1.0], dtype=torch.float64, requires_grad=True)

result = affine_projection(x=x, weight=weight, bias=bias)

Expected return

torch.tensor([[5.5, 2.0], [-0.5, -2.0]], dtype=torch.float64)

Parameters

ParameterShape and meaning
xtorch.Tensor[\ldots, \mathrm{Cin}]Feature vectors whose final axis is projected.
weighttorch.Tensor[\mathrm{Cin}, \mathrm{Cout}]Supplied input-channel-first projection matrix.
biastorch.Tensor | NoneDefault None[\mathrm{Cout}]Optional output-channel offset; omission means zero.

Return

torch.Tensor · [\ldots,C_{out}]

Return Projected vectors with all leading x axes preserved.

Required behavior

  • For x:[\ldots,C_{in}] and W:[C_{in},C_{out}], return y=xW+b. Weight is stored input-channel first, unlike the stored weight of torch.nn.Linear.
  • bias is optional and has shape (Cout,); omission means no addition. Support arbitrary leading shapes, including empty axes, and noncontiguous tensors.

Why this matters

A projection mixes a vector of features; a lookup selects a learned vector using an integer identity.

Starter code

You write the body of this function in the BioTorch workspace. Your code runs in an isolated PyTorch sandbox; Run checks the public examples and Submit grades the full test suite, including gradients.

import torch


def affine_projection(
    x: torch.Tensor,  # (..., Cin)
    weight: torch.Tensor,  # (Cin, Cout)
    bias: torch.Tensor | None = None,  # (Cout,)
) -> torch.Tensor:
    """Return a tensor with shape (...,C_{out})."""
    # Implement the operation. Inputs and test setup are supplied by BioTorch.
    raise NotImplementedError("Implement affine_projection")

Ready to implement it?

3 progressive hints, a solution walkthrough, and grading that checks values, shapes and gradients are waiting in the workspace.

Write and run your solution Sign in

Free account with email, Google or GitHub. Your code runs in an isolated PyTorch sandbox.

Related exercises

Sources and further reading

A primitive exercise, not an end-to-end reproduction of the named models. No affiliation or endorsement is implied.