0Pricing
Deep Learning Academy · درس

إعادة التشكيل وView وSqueeze وUnsqueeze

غيّر الأبعاد دون نسخ البيانات

إعادة التشكيل وView وSqueeze وUnsqueeze درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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. 🔧

الأسئلة الشائعة

هل درس «إعادة التشكيل وView وSqueeze وUnsqueeze» مجاني؟

نعم — نص درس «إعادة التشكيل وView وSqueeze وUnsqueeze» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.

ماذا ستتعلم في «إعادة التشكيل وView وSqueeze وUnsqueeze»؟

غيّر الأبعاد دون نسخ البيانات تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟

لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «إعادة التشكيل وView وSqueeze وUnsqueeze»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟

نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. الأشكال وأنواع البيانات والفهرسة
  2. إعادة التشكيل وView وSqueeze وUnsqueeze
  3. قواعد Broadcasting التي توفّر عليك الحلقات
  4. تتخاطب Tensors مع NumPy
← العودة إلى Deep Learning Academy