4. class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
# Linear(in_ch, out_ch) : in * w + b
self.linear = torch.nn.Linear(1, 1)
def forward(self, x):
x = self.linear(x)
return x
model
- 値を予測する機能(前回のforward関数)の実装例
- torch.nn.Moduleを継承したクラスを作成
- forwardメソッドに値を予測する機能を定義
5. x, y = torch.tensor([[1.],[2.],[3.]]), torch.tensor([[2.],[4.],[6.]])
model = Model()
pred = model(x[0])
print(pred)
print(model.linear.weight)
=> tensor([-1.5321], grad_fn=<AddBackward0>)
=> tensor([[-0.8516]], requires_grad=True)
model
- 定義したモデルの使用例
- 実体化したmodelに説明変数を渡すと予測値が戻る
- wの初期値はランダムらしい