Typorithm · easy

PyTorch: tensors, shapes, dtypes and explicit CPU execution

pythontorchindependent-practice
Saved on this device
Full lesson · 4 learning steps Open any exercise for focused practice
1

Step 1 of 4 · intro card

PyTorch: tensors, shapes, dtypes and explicit CPU execution

0%

Concept and contract

Read a tensor’s structural contract before writing a model.

A tensor combines values with shape, dtype and device. Shape defines the axes an operation can interpret, dtype affects arithmetic and supported kernels, and device determines where the work runs.

A Python list does not carry all those contracts. Start by naming each dimension’s meaning instead of treating every nested sequence as an interchangeable numeric container.

Mechanism and worked example

The helper constructs a float32 tensor on CPU from owned input values and requires exactly two dimensions.

The helper constructs a float32 tensor on CPU from owned input values and requires exactly two dimensions. It reports observed shape, dtype, device and sum.

Explicit CPU placement keeps this lab independent of GPU hardware and cloud spending. The bootstrap limits CPU threads and seeds the local process; it does not download data, pretrained weights or external models.

Complete worked example

python

from bootstrap_lab import torch_setup
torch = torch_setup()

def describe(values):
    tensor = torch.tensor(values, dtype=torch.float32, device='cpu')
    if tensor.ndim != 2:
        raise ValueError('matrix required')
    return {'shape': list(tensor.shape), 'dtype': str(tensor.dtype),
            'device': tensor.device.type, 'total': tensor.sum().item()}
Reasoning
The helper constructs a float32 tensor on CPU from owned input values and requires exactly two dimensions. It reports observed shape, dtype, device and sum.

Counterexamples and common mistakes

The counterexample constructs an integer tensor, which may preserve simple sums but violates the floating-point contract required by many learning operations.

The counterexample constructs an integer tensor, which may preserve simple sums but violates the floating-point contract required by many learning operations. A passing numeric total alone therefore misses the defect.

Empty and ragged input shapes also need an intentional policy. Do not move data automatically to a detected accelerator before understanding device compatibility and memory constraints.

Counterexample: a runnable framework mistake

python

from bootstrap_lab import torch_setup
torch = torch_setup()

def describe(values):
    tensor = torch.tensor(values, dtype=torch.int64, device='cpu')
    if tensor.ndim != 2:
        raise ValueError('matrix required')
    return {'shape': list(tensor.shape), 'dtype': str(tensor.dtype),
            'device': tensor.device.type, 'total': tensor.sum().item()}
Reasoning
The counterexample constructs an integer tensor, which may preserve simple sums but violates the floating-point contract required by many learning operations. A passing numeric total alone therefore misses the defect.

Independent practice: one bounded framework change

Add the arithmetic mean to the result, but reject a matrix with zero elements before computing it.

Add the arithmetic mean to the result, but reject a matrix with zero elements before computing it. Preserve the existing two-dimensional check and explicit float32 CPU policy.

This introduces one reduction and one boundary case. Do not silently return NaN for an empty collection, and do not change the expected dtype merely to simplify a displayed number.

Step 1: Add a finite mean and reject empty matrices. Step 2: Check numel after the dimensionality check.

Practice starter: solve before reading the solution

python

from bootstrap_lab import torch_setup
torch = torch_setup()

def describe(values):
    # Remove this placeholder, then adapt the retained baseline:
    # Add a finite mean and reject empty matrices.
    raise NotImplementedError
    tensor = torch.tensor(values, dtype=torch.float32, device='cpu')
    if tensor.ndim != 2:
        raise ValueError('matrix required')
    return {'shape': list(tensor.shape), 'dtype': str(tensor.dtype),
            'device': tensor.device.type, 'total': tensor.sum().item()}
Reasoning
Add a finite mean and reject empty matrices. Check numel after the dimensionality check.
  • Hint 1: Use tensor.mean().item() for the observed mean.
  • Hint 2: Retain shape, dtype and CPU fields.

Solution, tests and trade-offs

Fixtures compare rectangular matrices, negative values and rejection of a one-dimensional input.

Fixtures compare rectangular matrices, negative values and rejection of a one-dimensional input. Practice adds an empty two-dimensional matrix case and verifies the mean.

Floating results use a small finite tolerance rather than string equality; shape and dtype remain exact contracts. These are deterministic tensor checks, not evidence about the quality of a trained model or performance on a GPU.

Practice solution and acceptance checks

python

from bootstrap_lab import torch_setup
torch = torch_setup()

def describe(values):
    tensor = torch.tensor(values, dtype=torch.float32, device='cpu')
    if tensor.ndim != 2:
        raise ValueError('matrix required')
    if tensor.numel() == 0:
        raise ValueError('nonempty matrix required')
    return {'mean': tensor.mean().item(), 'shape': list(tensor.shape), 'dtype': str(tensor.dtype),
            'device': tensor.device.type, 'total': tensor.sum().item()}
Reasoning
Fixtures compare rectangular matrices, negative values and rejection of a one-dimensional input. Practice adds an empty two-dimensional matrix case and verifies the mean.

Interview reasoning and next step

Later lessons build batches, model parameters and losses on top of these same structural decisions.

Later lessons build batches, model parameters and losses on top of these same structural decisions. Write down expected shapes beside each intermediate operation and inspect them when debugging.

A model can run without an exception while interpreting the wrong axis. Clear shape, dtype and device contracts reduce that class of silent error before training complexity is introduced.

Optional stretch — after the core task works: Add a rectangular-shape validation policy for external data.

Computer practice: https://applydjinn.com/typorithm/python-frameworks-lab-20261105.zip. Install the pinned dependencies in an isolated environment, run verify.py --track torch, then edit the retained exercise baseline and check it with --practice pt-tensors. The lab uses owned in-memory databases, local framework clients and tiny CPU tensors. It never reads the working ApplyDjinn database, contacts production feeds or downloads models.

Read a tensor’s structural contract before writing a model.