shape、dtype、インデックス指定
テンソルのshapeを読み取り、スライスします
「shape、dtype、インデックス指定」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Tensors Are Just Numbers in a Grid
A tensor is a box of numbers arranged in rows, columns, or deeper. Everything in deep learning flows through them.
Shape Describes the Layout
The shape tells you how many values sit along each dimension. Read it like dimensions of a box: rows by columns. 📦
Check Shape with .shape
Ask any tensor for its .shape and PyTorch hands you the size of every dimension in order.
import torch
x = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(x.shape) # torch.Size([2, 3])Dimensions Count with .ndim
The number of dimensions is the tensor's rank. A scalar is 0, a vector is 1, a matrix is 2, and images go higher.
x = torch.tensor([[1, 2], [3, 4]])
print(x.ndim) # 2Every Tensor Has a Dtype
The dtype is the kind of number stored, like float32 or int64. It controls precision and which math is allowed.
x = torch.tensor([1.0, 2.0, 3.0])
print(x.dtype) # torch.float32Float32 Is the Default for Learning
Most networks train in float32 because gradients need decimals. Integers can't hold the tiny fractional updates.
Cast Between Dtypes
Change a tensor's type with .to() or .float(). This is how you match a model's expected precision.
x = torch.tensor([1, 2, 3])
y = x.to(torch.float32)
print(y.dtype) # torch.float32Indexing Grabs One Value
Indexing uses square brackets to pull out a single element by its position, counting from zero.
x = torch.tensor([10, 20, 30])
print(x[0]) # tensor(10)Two Indices for a Matrix
For a 2D tensor, give a row then a column. The order always follows the shape you saw earlier.
x = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(x[1, 2]) # tensor(6)Slicing Takes a Range
Use a colon to grab a whole slice. start:stop keeps everything up to but not including stop.
x = torch.tensor([10, 20, 30, 40])
print(x[1:3]) # tensor([20, 30])Slice Rows and Columns Together
A lone colon means take all of that dimension. This is how you select a whole column across every row.
x = torch.tensor([[1, 2], [3, 4]])
print(x[:, 0]) # tensor([1, 3])Quick Check
Time to test what shape and indexing really mean.
Recap: Shapes, Dtypes & Indexing
You can now read a tensor's shape, check its dtype, and slice into it. These three skills unlock every tensor operation ahead. 🎉
よくある質問
「shape、dtype、インデックス指定」レッスンは無料ですか?
はい。「shape、dtype、インデックス指定」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。
「shape、dtype、インデックス指定」で何を学びますか?
テンソルのshapeを読み取り、スライスします ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Deep Learning Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「shape、dtype、インデックス指定」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDeep Learning Academyレッスンでコードを書いて実行できますか?
はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- shape、dtype、インデックス指定
- reshape、view、squeeze、unsqueeze
- ループを減らすブロードキャストのルール
- テンソルとNumPyの連携