Reshape, view, squeeze i unsqueeze
Zmieniać wymiary bez kopiowania danych
Reshape, view, squeeze i unsqueeze to bezpłatna lekcja Deep Learning Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Deep Learning Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Deep Learning Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. 🔧
Często zadawane pytania
Czy lekcja „Reshape, view, squeeze i unsqueeze” jest bezpłatna?
Tak — pełny tekst „Reshape, view, squeeze i unsqueeze” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Deep Learning Academy, przejdź na CoddyKit PRO. Kurs Deep Learning Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Reshape, view, squeeze i unsqueeze”?
Zmieniać wymiary bez kopiowania danych Ćwiczysz Deep Learning Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Deep Learning Academy?
Nie wymagamy żadnego doświadczenia. Deep Learning Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Reshape, view, squeeze i unsqueeze”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Deep Learning Academy?
Tak. Każda lekcja Deep Learning Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Kształty, dtype i indeksowanie
- Reshape, view, squeeze i unsqueeze
- Reguły broadcastingu, które oszczędzają pętle
- Tensory współpracują z NumPy