Reshape, View, Squeeze und Unsqueeze
Ändern Sie Dimensionen, ohne Daten zu kopieren.
Reshape, View, Squeeze und Unsqueeze ist eine kostenlose Deep Learning Academy-Lektion auf CoddyKit. Dies ist Lektion 2 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 Deep Learning Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Same Data, Different Shape
Often the numbers are right but the layout is wrong. Reshaping rearranges a tensor's dimensions without changing the values inside.
reshape Picks a New Layout
Call reshape with the dimensions you want. The total number of elements must stay the same.
x = torch.arange(6)
y = x.reshape(2, 3)
print(y.shape) # torch.Size([2, 3])Let -1 Infer a Dimension
Pass -1 for one dimension and PyTorch computes it for you from the total count. Handy when you only know the rest.
x = torch.arange(6)
y = x.reshape(-1, 2)
print(y.shape) # torch.Size([3, 2])view Shares the Same Memory
view reshapes without copying, so it is fast. It needs the data to be laid out contiguously in memory.
x = torch.arange(6)
y = x.view(3, 2)
print(y.shape) # torch.Size([3, 2])reshape Is the Safer Default
When in doubt, reach for reshape. It works even on non-contiguous tensors by copying only if it must.
Squeeze Removes Size-1 Dims
squeeze strips out any dimension of length 1. It cleans up shapes like (1, 5) down to a simple (5).
x = torch.zeros(1, 5)
y = x.squeeze()
print(y.shape) # torch.Size([5])Squeeze a Specific Dimension
Give squeeze an index to remove only that dimension. Safer when other size-1 dims should stay put.
x = torch.zeros(1, 5, 1)
y = x.squeeze(0)
print(y.shape) # torch.Size([5, 1])Unsqueeze Adds a Dimension
unsqueeze inserts a new size-1 dimension at the position you choose. It is the exact opposite of squeeze.
x = torch.tensor([1, 2, 3])
y = x.unsqueeze(0)
print(y.shape) # torch.Size([1, 3])Why You Add a Batch Dimension
Models expect a batch dimension up front. unsqueeze(0) turns one sample into a batch of one so the model accepts it.
sample = torch.randn(3)
batch = sample.unsqueeze(0)
print(batch.shape) # torch.Size([1, 3])Flatten Down to One Line
flatten collapses every dimension into a single long vector. It is common right before a final linear layer.
x = torch.zeros(2, 3)
y = x.flatten()
print(y.shape) # torch.Size([6])Element Count Never Changes
Every reshape trick keeps the same total number of values. If the counts don't match, PyTorch raises a shape error.
Quick Check
Let's see if you can predict the shape changes.
Recap: Reshape, View, Squeeze & Unsqueeze
You can now bend tensors into any layout: reshape for flexibility, view for speed, and squeeze or unsqueeze to drop and add dimensions. 🔧
Häufig gestellte Fragen
Ist die Lektion „Reshape, View, Squeeze und Unsqueeze“ kostenlos?
Ja — der vollständige Text von „Reshape, View, Squeeze und Unsqueeze“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Deep Learning Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Reshape, View, Squeeze und Unsqueeze“?
Ändern Sie Dimensionen, ohne Daten zu kopieren. Du übst Deep Learning 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 Deep Learning Academy zu starten?
Keine Vorkenntnisse erforderlich. Deep Learning 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 2 von 4.
Wie lange dauert die Lektion „Reshape, View, Squeeze und Unsqueeze“?
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 Deep Learning Academy-Lektion Code schreiben und ausführen?
Ja. Jede Deep Learning 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
- Shapes, Dtypes und Indexierung
- Reshape, View, Squeeze und Unsqueeze
- Broadcasting-Regeln, die Schleifen sparen
- Tensoren sprechen mit NumPy