Recommended 2013.07.15 はじパタlt scikit-learnで始める機械学習
[第2版]Python機械学習プログラミング 第16章
[第2版]Python機械学習プログラミング 第14章
[第2版]Python機械学習プログラミング 第8章
[第2版]Python機械学習プログラミング 第13章
[第2版]Python機械学習プログラミング 第9章
「深層学習」勉強会LT資料 "Chainer使ってみた"
Chainer/CuPy v5 and Future (Japanese)
ディープラーニングフレームワーク とChainerの実装
Pythonによる機械学習入門 ~Deep Learningに挑戦~
[第2版]Python機械学習プログラミング 第15章
深層学習フレームワークChainerの紹介とFPGAへの期待
PythonによるDeep Learningの実装
Tokyo.R 41 サポートベクターマシンで眼鏡っ娘分類システム構築
[DSO] Machine Learning Seminar Vol.1 Chapter 1 and 2
Qlik Tips 20220315 Null値の課題と対策
Chainerチュートリアル -v1.5向け- ViEW2015
[DSO] Machine Learning Seminar Vol.2 Chapter 3
Jupyter NotebookとChainerで楽々Deep Learning
Few shot object detection via feature reweighting
[DL輪読会]“Submodular Field Grammars Representation” and “Deep Submodular Functi...
DTrace for biginners part(2)
More Related Content 2013.07.15 はじパタlt scikit-learnで始める機械学習
[第2版]Python機械学習プログラミング 第16章
[第2版]Python機械学習プログラミング 第14章
[第2版]Python機械学習プログラミング 第8章
[第2版]Python機械学習プログラミング 第13章
[第2版]Python機械学習プログラミング 第9章
What's hot (20) 「深層学習」勉強会LT資料 "Chainer使ってみた"
Chainer/CuPy v5 and Future (Japanese)
ディープラーニングフレームワーク とChainerの実装
Pythonによる機械学習入門 ~Deep Learningに挑戦~
[第2版]Python機械学習プログラミング 第15章
深層学習フレームワークChainerの紹介とFPGAへの期待
PythonによるDeep Learningの実装
Tokyo.R 41 サポートベクターマシンで眼鏡っ娘分類システム構築
[DSO] Machine Learning Seminar Vol.1 Chapter 1 and 2
Qlik Tips 20220315 Null値の課題と対策
Chainerチュートリアル -v1.5向け- ViEW2015
[DSO] Machine Learning Seminar Vol.2 Chapter 3
Jupyter NotebookとChainerで楽々Deep Learning
Few shot object detection via feature reweighting
[DL輪読会]“Submodular Field Grammars Representation” and “Deep Submodular Functi...
DTrace for biginners part(2)
Similar to Pytorch 03 (20)
Why Reactive Matters #ScalaMatsuri
【LT版】Elixir入門「第7回:Python/KerasをElixirから繋いでアレコレする」
Opencv object detection_takmin
Pytorch 035. 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の初期値はランダムらしい
6. criterion = torch.nn.MSELoss()
pred = model(x[0])
loss = criterion(pred, y[0])
print(loss)
=> tensor(12.4759, grad_fn=<MseLossBackward>)
criterion
- 損失関数(前回のloss_fn)を定義
- 前回の実装と近いMeanSquadErrorを利用する
- 利用方法は以下の通り
7. pred = model(x[0])
loss = criterion(pred, y[0])
loss.backward()
print(loss)
print(model.linear.weight.grad)
=> loss: tensor(3.1791, grad_fn=<MseLossBackward>)
=> grad: tensor([[-3.5660]])
grad
- 勾配の算出はcriterionの戻り値をbackward()する
- 重み、勾配はモデル内に存在し以下で出力できる
- 今回の勾配は以下の通り
8. optimizer
- 勾配を重みへ反映する処理
- 今回は確立的勾配降下法を利用
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
pred = model(x[0])
loss = criterion(pred, y[0])
loss.backward()
print(model.linear.weight.item(), model.linear.weight.grad.item())
optimizer.step() #=> 勾配を重みに反映
print(model.linear.weight)
optimizer.zero_grad() #=> 勾配を0にセット
print(model.linear.weight.grad)
=> 0.9048234224319458 -2.2820169925689697
=> tensor([[0.9276]], requires_grad=True)
=> tensor([[0.]])
9. まとめ
import torch
x, y = torch.tensor([ [1.], [2.], [3.] ]), torch.tensor([ [2.], [4.], [6.] ])
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear = torch.nn.Linear(1, 1)
def forward(self, x):
x = self.linear(x)
return x
model = Model()
- 以上を踏まえた実装例(前半)
10. まとめ
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
criterion = torch.nn.MSELoss()
for epoch in range(300):
for _x, _y in zip(x, y):
pred = model(_x)
loss = criterion(pred, _y)
loss.backward()
optimizer.step()
optimizer.zero_grad()
pred = model(torch.tensor([4.]))
print(pred)
=> tensor([7.9167], grad_fn=<AddBackward0>)
- 以上を踏まえた実装例(後半)