เปลี่ยนรูปร่าง มุมมอง ลดมิติ และเพิ่มมิติ
เปลี่ยนมิติโดยไม่คัดลอกข้อมูล
เปลี่ยนรูปร่าง มุมมอง ลดมิติ และเพิ่มมิติ เป็นบทเรียน Deep Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Deep Learning Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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. 🔧
คำถามที่พบบ่อย
บทเรียน “เปลี่ยนรูปร่าง มุมมอง ลดมิติ และเพิ่มมิติ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “เปลี่ยนรูปร่าง มุมมอง ลดมิติ และเพิ่มมิติ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Deep Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “เปลี่ยนรูปร่าง มุมมอง ลดมิติ และเพิ่มมิติ”
เปลี่ยนมิติโดยไม่คัดลอกข้อมูล คุณปฏิบัติ Deep Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Deep Learning Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Deep Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “เปลี่ยนรูปร่าง มุมมอง ลดมิติ และเพิ่มมิติ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Deep Learning Academy นี้ได้ไหม
ได้ บทเรียน Deep Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- รูปร่าง ชนิดข้อมูล และการทำดัชนี
- เปลี่ยนรูปร่าง มุมมอง ลดมิติ และเพิ่มมิติ
- กฎการกระจายมิติที่ช่วยลดการวนซ้ำ
- เทนเซอร์สื่อสารกับ NumPy