计划执行与检查节点
executing-plans
按既定实施计划完成工作,保持过程检查和反馈。
tensor-operations【效率与协作】Core einops tensor operations for rearrange, reduce, repeat, parse_shape, asnumpy, shape recipes, and troubleshooting.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Use this sub-skill when a task asks for everyday tensor shape work with
einops.rearrange, einops.reduce, einops.repeat, einops.parse_shape, or
einops.asnumpy: readable replacements for reshape/view/permute/transpose,
shape-checked refactors, pooling/reduction, broadcasting/repetition, stack or
concatenate from lists, ellipsis patterns, and pattern debugging.
Stay here for:
rearrange, reduce, repeat, parse_shape, and
asnumpy.(c h w), decomposed axes
like (h h2), singleton axes 1/(), anonymous numeric axes in allowed
contexts, and ellipsis ....min, max, sum, mean, prod, any, all, plus
callable reductions with signature f(tensor, axes_tuple) -> tensor.Route elsewhere:
einsum, pack, and unpack: use
named-einsum-and-packing.EinMix, compilation, and
deep framework integration: use
framework-integrations.repo-development.Import the needed functions:
from einops import rearrange, reduce, repeat, parse_shape, asnumpy
Write patterns as "left axes -> right axes".
b c h w -> b (c h w) or
b (patch c) h w -> patch b c h w.(a b), neighboring linear
positions differ first in the rightmost axis b.reduce.repeat or as singleton 1/() axes.Supply axes_lengths whenever einops cannot infer a factor or must check a
contract:
y = rearrange(x, "b c (h h2) (w w2) -> b h w (c h2 w2)", h2=2, w2=2)
Preserve original shape facts with parse_shape when undoing a flatten or
refactoring code that previously relied on comments:
shape = parse_shape(x, "b c h w")
flat = rearrange(x, "b c h w -> (b h w) c")
restored = rearrange(flat, "(b h w) c -> b c h w", **shape)
Validate with explicit shapes and a tiny data check before replacing a framework-native snippet. For copyable assertions, run the bundled smoke script below.
references/api-reference.md: verified
signatures, return behavior, pattern grammar, reduction names, callable
reductions, parse_shape, asnumpy, and API boundaries.references/pattern-recipes.md: copyable
recipes for flatten/unflatten, pooling, stack/concat, space-depth transforms,
image/video conventions, ellipsis, and reduce/repeat interplay.references/troubleshooting.md: symptom →
cause → recovery guidance for common einops errors and pattern pitfalls.scripts/shape_recipe_smoke.py: deterministic
NumPy smoke script adapted from public example/test recipes; runs core shape
recipes and assertion-backed failure checks.# NCHW -> NHWC
x_nhwc = rearrange(x_nchw, "b c h w -> b h w c")
# Per-example flatten with explicit semantic axes
features = rearrange(x_nchw, "b c h w -> b (c h w)")
Add fixed lengths when the refactor is meant to assert an interface:
features = rearrange(x, "b c h w -> b (c h w)", c=256, h=19, w=19)
pooled = reduce(x, "b c (h h2) (w w2) -> b c h w", "max", h2=2, w2=2)
centered = x - reduce(x, "b c h w -> b c 1 1", "mean")
For integer tensors, avoid "mean" until the tensor is floating-point; see the
troubleshooting reference.
rgb = repeat(gray, "h w -> h w c", c=3)
upsampled = repeat(lowres, "b h w c -> b (h h2) (w w2) c", h2=2, w2=2)
Every named axis introduced by repeat must have a supplied size or be written
as an anonymous numeric axis such as 5.
stacked = rearrange(list_of_images, "b h w c -> b c h w")
wide_strip = rearrange(list_of_images, "b h w c -> h (b w) c")
A list input is stacked on a new zeroth axis before the pattern is interpreted;
name that axis explicitly (b above).
moved = rearrange(x, "batch ... channels -> batch channels ...")
summary = reduce(x, "batch ... channels -> batch channels", "mean")
repeated = repeat(x, "... channels -> ... channels copies", copies=4)
Use ellipsis when the rank between known outer axes can vary. Do not place ellipsis inside parentheses on the left side.
For any nontrivial transformation:
Write a one-line shape expectation beside the operation.
If decomposing axes, assert divisibility before calling einops when the recovery path is user-facing.
Compare one or two sentinel values after rearrangement to confirm axis order.
For refactors, run:
python sub-skills/tensor-operations/scripts/shape_recipe_smoke.py --case all
from any directory after installing einops and numpy.
This sub-skill distills public evidence from README.md, einops/einops.py,
einops/parsing.py, einops/tests/test_ops.py,
einops/tests/test_examples.py, einops/tests/test_other.py, and the headings
and example patterns from docs/1-einops-basics.ipynb and
docs/2-einops-for-deep-learning.ipynb. Installed package inspection verified
core function availability and signatures for the 0.9.0 development runtime.