ループを減らすブロードキャストのルール
shapeの異なるテンソルを要素ごとに組み合わせます
「ループを減らすブロードキャストのルール」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Combine Without Matching Shapes
Broadcasting lets PyTorch stretch a smaller tensor to fit a bigger one, so you skip writing loops to repeat values.
Add a Scalar to Everything
The simplest broadcast: add one number to a whole tensor. PyTorch applies it to every element at once. ✨
x = torch.tensor([1, 2, 3])
print(x + 10) # tensor([11, 12, 13])Compare Shapes Right to Left
Broadcasting lines up dimensions from the right. It then checks each pair to decide if they can combine.
Rule One: Equal Sizes Match
Two dimensions are compatible when they are equal. Matching sizes line up one to one with no stretching needed.
Rule Two: A Size of 1 Stretches
If one dimension is 1, it expands to match the other. That single value is reused across the whole axis.
a = torch.tensor([[1], [2], [3]])
b = torch.tensor([10, 20])
print((a + b).shape) # torch.Size([3, 2])Missing Dimensions Count as 1
When one tensor has fewer dimensions, PyTorch pads it with leading ones. A vector can broadcast against a matrix this way.
m = torch.ones(2, 3)
v = torch.tensor([1, 2, 3])
print((m + v).shape) # torch.Size([2, 3])Add a Bias Across Rows
A classic use: add a row bias to every sample in a batch. One small vector reaches every row for free.
batch = torch.zeros(4, 3)
bias = torch.tensor([1.0, 2.0, 3.0])
print((batch + bias).shape) # torch.Size([4, 3])Normalize a Column at Once
Subtract a per-column mean with broadcasting and center your data in one line, no loop over rows required.
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
m = x.mean(dim=0)
print(x - m)When Shapes Don't Broadcast
If two dimensions differ and neither is 1, the broadcast fails and PyTorch raises a clear size error.
Add Dimensions to Steer Broadcasting
Use unsqueeze to insert a size-1 axis exactly where you need it. This guides broadcasting toward the shape you want.
col = torch.tensor([1, 2, 3]).unsqueeze(1)
print(col.shape) # torch.Size([3, 1])Why Broadcasting Is Fast
Broadcasting never copies the stretched values, it reuses them. That makes vectorized math far faster than Python loops. 🚀
Quick Check
Can you predict whether two shapes will broadcast?
Recap: Broadcasting Rules
You learned the two rules: dimensions match when equal or when one is 1. Master this and loops melt into clean, fast tensor math. 💪
AI チューターと学ぶ Python — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 30
- レッスン
- 120
よくある質問
「ループを減らすブロードキャストのルール」レッスンは無料ですか?
はい。「ループを減らすブロードキャストのルール」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。
「ループを減らすブロードキャストのルール」で何を学びますか?
shapeの異なるテンソルを要素ごとに組み合わせます ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Deep Learning Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「ループを減らすブロードキャストのルール」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDeep Learning Academyレッスンでコードを書いて実行できますか?
はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- shape、dtype、インデックス指定
- reshape、view、squeeze、unsqueeze
- ループを減らすブロードキャストのルール
- テンソルとNumPyの連携