<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Tensor Shapes on AIBussin — AI applications, systems and books</title><link>https://aibussin.com/tags/tensor-shapes/</link><description>Recent content in Tensor Shapes on AIBussin — AI applications, systems and books</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Sat, 29 Aug 2026 10:00:00 +0100</lastBuildDate><atom:link href="https://aibussin.com/tags/tensor-shapes/index.xml" rel="self" type="application/rss+xml"/><item><title>The Tensor: What Is Actually Flowing Through the Loop?</title><link>https://aibussin.com/books/pytorch-from-first-principles/02-chapter/</link><pubDate>Sat, 08 Aug 2026 12:45:00 +0100</pubDate><guid>https://aibussin.com/books/pytorch-from-first-principles/02-chapter/</guid><description>&lt;p&gt;The last program in Chapter 1 trained a two-parameter model on four examples. It worked, and we read it as a story about gradients: predict, measure, differentiate, step. But something in it went unexamined. &lt;code&gt;w&lt;/code&gt; was a single number and &lt;code&gt;x&lt;/code&gt; held four, and &lt;code&gt;w * x + b&lt;/code&gt; produced four predictions without anyone specifying how a scalar and a four-element vector should combine. PyTorch had a rule. We never stated it.&lt;/p&gt;</description></item><item><title>CNN Geometry: What Shape Reaches the Next Layer?</title><link>https://aibussin.com/books/pytorch-from-first-principles/08-chapter/</link><pubDate>Fri, 28 Aug 2026 10:15:00 +0100</pubDate><guid>https://aibussin.com/books/pytorch-from-first-principles/08-chapter/</guid><description>&lt;p&gt;Here is a batch of 32 RGB images and an ordinary first convolution.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;x &lt;span style="color:#f92672"&gt;=&lt;/span&gt; torch&lt;span style="color:#f92672"&gt;.&lt;/span&gt;randn(&lt;span style="color:#ae81ff"&gt;32&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;224&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;224&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;3&lt;/span&gt;)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;conv &lt;span style="color:#f92672"&gt;=&lt;/span&gt; nn&lt;span style="color:#f92672"&gt;.&lt;/span&gt;Conv2d(in_channels&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;3&lt;/span&gt;, out_channels&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;16&lt;/span&gt;, kernel_size&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;3&lt;/span&gt;)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;conv(x)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;RuntimeError: Given groups=1, weight of size [16, 3, 3, 3],&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;expected input[32, 224, 224, 3] to have 3 channels, but got 224 channels instead&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The images have three channels. PyTorch says there are 224. The message names a number that appears nowhere in the model definition, so the obvious reading is that the layer was declared wrong, and the obvious repair is to declare it right:&lt;/p&gt;</description></item><item><title>Attention: Which Position Is Comparing With Which?</title><link>https://aibussin.com/books/pytorch-from-first-principles/10-chapter/</link><pubDate>Sat, 29 Aug 2026 10:00:00 +0100</pubDate><guid>https://aibussin.com/books/pytorch-from-first-principles/10-chapter/</guid><description>&lt;p&gt;Here is a tensor of sequence representations and the line that splits it into heads.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;B, T, E, Nh &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;4&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;8&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;2&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Dh &lt;span style="color:#f92672"&gt;=&lt;/span&gt; E &lt;span style="color:#f92672"&gt;//&lt;/span&gt; Nh &lt;span style="color:#75715e"&gt;# 4&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;x &lt;span style="color:#f92672"&gt;=&lt;/span&gt; torch&lt;span style="color:#f92672"&gt;.&lt;/span&gt;arange(B &lt;span style="color:#f92672"&gt;*&lt;/span&gt; T &lt;span style="color:#f92672"&gt;*&lt;/span&gt; E)&lt;span style="color:#f92672"&gt;.&lt;/span&gt;reshape(B, T, E)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;heads &lt;span style="color:#f92672"&gt;=&lt;/span&gt; x&lt;span style="color:#f92672"&gt;.&lt;/span&gt;reshape(B, Nh, T, Dh)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;x: (1, 4, 8)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;heads: (1, 2, 4, 4)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That is exactly the shape multi-head attention wants: batch, heads, positions, head dimension. Nothing raised. Every shape assertion passes.&lt;/p&gt;&#10;&lt;p&gt;Now the same split written the other way:&lt;/p&gt;</description></item><item><title>PyTorch Tensor Shapes: Broadcasting, Reshape, View, Permute and the Errors That Waste Your Time</title><link>https://aibussin.com/post/pytorch-zero-to-hero-01/</link><pubDate>Sat, 08 Aug 2026 12:45:00 +0100</pubDate><guid>https://aibussin.com/post/pytorch-zero-to-hero-01/</guid><description>&lt;h2 id="pytorch-zero-to-hero--step-01"&gt;PyTorch: Zero to Hero — Step 01&lt;/h2&gt;&#10;&lt;p&gt;Most PyTorch bugs are not really &amp;ldquo;AI bugs&amp;rdquo;.&lt;/p&gt;&#10;&lt;p&gt;They are shape bugs.&lt;/p&gt;&#10;&lt;p&gt;You expected:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[batch, features]&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;but actually had:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;[batch, sequence, features]&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You expected two tensors to line up.&lt;/p&gt;&#10;&lt;p&gt;They broadcast instead.&lt;/p&gt;&#10;&lt;p&gt;You called &lt;code&gt;view()&lt;/code&gt; after &lt;code&gt;permute()&lt;/code&gt; and got a contiguity error.&lt;/p&gt;&#10;&lt;p&gt;You removed a dimension with &lt;code&gt;squeeze()&lt;/code&gt; and accidentally removed the batch dimension too.&lt;/p&gt;&#10;&lt;p&gt;Or you reached the familiar message:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;RuntimeError: The size of tensor a (...) must match the size of tensor b (...)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This article is about becoming dangerous enough with tensors that these errors stop being mysterious.&lt;/p&gt;</description></item></channel></rss>