渲染zh-Hant

tensor-shape-generic

Translation status: 繁體中文 reader-locale proof. Term names and code fences follow the zh-Hant pack; supporting prose may still be English.

Shape-generic tensor functions: a 尺寸 shape param binds implicitly at call sites.

Syntax: 函式 name<T, 尺寸 Figura>(張量<T, Figura> …) → 張量<T, Figura>

Category#

type

Examples#

radix/corpus/tensor/generic-shape.fab (canonical · concept)#

Shape-generic tensor functions: a 尺寸 shape param binds implicitly at call sites.

# =============================================================================
# tensor-shape-generic — shape-generic 張量 functions (尺寸 Figura)
# =============================================================================
#
# What this teaches:
#   • A `尺寸 Figura` shape param stands for the whole 張量 shape; the
#     shape is rank-agnostic, so one signature covers `[4]`, `[2, 2]`, `[1, 2]`,
#     and `[]` witnesses.
#   • The typechecker binds the shape param implicitly at call sites from the
#     argument shapes or the expected type — no explicit figura arguments.
#   • Fully static bodies compose elementwise ops (`reple`, `multiplica`,
#     `subtrahe`) whose results keep the same shape symbolically.
#
# Common mistakes:
#   • Spelling the shape as a rank-1 tuple `[Figura]` — that only matches
#     rank-1 witnesses; use the bare `Figura` for rank-agnostic shapes.
#   • Expecting a shape param bound by neither arguments nor the expected type
#     to guess: it errors (SEM014) instead.
#
# See also: 張量, 尺寸, 函式, 向量
# =============================================================================

# One generic `sgd_step` replaces the concrete [4]/[2,2]/[1,2]/[] overloads:
# the param/grad pair shares `Figura`, so every elementwise step proves the
# same-shape result.

函式 sgd_step<尺寸 Figura>(張量<f32, Figura> param, 張量<f32, Figura> grad, f32 lr) → 張量<f32, Figura> {
    定值 張量<f32, Figura> lr_fill ← param.填充(lr)
    定值 張量<f32, Figura> scaled ← grad.乘以(lr_fill)
    傳回 param.減去(scaled)
}

註記 "shape-generic sgd_step parata"

Expected output:

shape-generic sgd_step parata