Einen Tensor in Mojo modellieren
Shape, Strides und Elementlayout.
Einen Tensor in Mojo modellieren ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
What Is a Tensor?
A tensor is just an n-dimensional grid of numbers. A vector is 1D, a matrix is 2D, and an image batch is often 4D.
Flat Memory Underneath
However many dimensions you imagine, a tensor really lives as one flat buffer of values in memory, laid out end to end.
var data = List[Float32](0, 1, 2, 3, 4, 5)Shape Describes the Grid
The shape is a tuple of sizes, one per dimension. A shape of (2, 3) means two rows of three columns.
var shape = (2, 3) # 2 rows, 3 colsIndices Pick an Element
You name an element with one index per dimension. In a (2, 3) tensor, (1, 2) is the last column of the second row.
Strides Map to Memory
A stride tells you how many flat slots to jump to move one step along a dimension. Strides turn indices into a single offset.
var strides = (3, 1) # row jump 3, col jump 1The Offset Formula
Flatten any index by a dot product: offset equals the sum of each index times its stride. That offset reaches the right slot.
offset = i * strides[0] + j * strides[1]Row-Major Layout
Mojo numeric code usually stores rows contiguously, called row-major. The last dimension has stride 1, so columns sit side by side.
Why Strides Are Powerful
With clever strides you can view, slice, or transpose a tensor without copying data. Only the strides change, not the buffer.
Element Type Matters
Every tensor has a fixed dtype, like Float32 or Int64. A uniform type lets Mojo pack values tightly and compute fast.
alias dtype = DType.float32A Minimal Tensor Struct
You can model a tensor as a struct that bundles a data pointer with its shape and strides, keeping layout info in one place.
struct Tensor:
var data: UnsafePointer[Float32]
var shape: (Int, Int)
var strides: (Int, Int)Contiguous vs Strided
A contiguous tensor has no gaps, so a linear scan is cheap. Strided views may skip around, which can slow memory access.
Quick Check
You have a (2, 3) row-major tensor. How do you reach element (1, 2)?
Recap
A tensor is a flat buffer plus shape and strides; the offset formula maps indices to memory, and row-major keeps columns contiguous. 🧮
Häufig gestellte Fragen
Ist die Lektion „Einen Tensor in Mojo modellieren“ kostenlos?
Ja — der vollständige Text von „Einen Tensor in Mojo modellieren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Einen Tensor in Mojo modellieren“?
Shape, Strides und Elementlayout. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Mojo Academy zu starten?
Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Einen Tensor in Mojo modellieren“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?
Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Einen Tensor in Mojo modellieren
- Matmul Schritt für Schritt erstellen
- Das Skalarprodukt optimieren
- Numerische Korrektheit überprüfen