0Pricing
Deep Learning Academy · レッスン

nn.Moduleをサブクラス化する:__init__とforward

PyTorchモデルの標準的な骨組みを学びます

「nn.Moduleをサブクラス化する:__init__とforward」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Models Are Just Classes

In PyTorch, every model is a Python class. You build yours by subclassing nn.Module, the base that powers all networks.

Two Methods Run the Show

A model needs only two methods to work: __init__ sets up the layers, and forward describes how data flows through them.

Always Call super().__init__

The very first line inside __init__ must call super().__init__(). This wires your model into PyTorch and lets it track everything. 🔌

class Net(nn.Module):
    def __init__(self):
        super().__init__()

Define Layers in __init__

Inside __init__ you create your layers and store them as attributes. Saving them on self lets PyTorch register them automatically.

self.fc1 = nn.Linear(4, 8)
self.fc2 = nn.Linear(8, 2)

forward Is the Recipe

The forward method takes an input tensor and returns an output. It spells out the exact order your layers process the data.

def forward(self, x):
    x = self.fc1(x)
    return self.fc2(x)

Never Call forward Directly

You call the model like a function, not model.forward(x). Using model(x) runs hooks and bookkeeping that forward alone skips.

out = model(x)   # preferred
# not: out = model.forward(x)

Layers Become Parameters

Because layers live on self, their weights are collected as the model's parameters. The optimizer later updates exactly these.

Instantiate Then Use

Create the model once, then feed it tensors many times. Each call flows through the same learned weights.

model = Net()
prediction = model(sample_input)

Shapes Must Line Up

Each layer's output size must match the next layer's input size. Plan these dimensions as data travels through forward.

Why This Pattern Wins

Subclassing keeps setup and flow cleanly apart. The same simple skeleton scales from a tiny net to a giant one.

Flexible by Design

Inside forward you can branch, reshape, or reuse layers freely. This freedom is why custom nn.Module classes are so powerful. 💪

Quick Check

Think about which method defines how data moves through the network.

Recap

You build a model by subclassing nn.Module, defining layers in __init__ and the data flow in forward. Then just call model(x). 🎯

よくある質問

「nn.Moduleをサブクラス化する:__init__とforward」レッスンは無料ですか?

はい。「nn.Moduleをサブクラス化する:__init__とforward」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。

「nn.Moduleをサブクラス化する:__init__とforward」で何を学びますか?

PyTorchモデルの標準的な骨組みを学びます ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Deep Learning Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「nn.Moduleをサブクラス化する:__init__とforward」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このDeep Learning Academyレッスンでコードを書いて実行できますか?

はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. nn.Moduleをサブクラス化する:__init__とforward
  2. 線形レイヤーを積み重ねる
  3. 手早いモデルに使うnn.Sequential
  4. パラメーターとレイヤーのshapeを調べる
← Deep Learning Academyに戻る