SlideShare a Scribd company logo
Pytorch入門(2)
Autograd
- ニューラルネットワークの中心となる機能
- tensorの計算過程を記憶
- 自動微分により勾配を算出することができる
Train
- 以下の値にてx=4,y=?を予測したい
x: 予測に使う値(説明変数)、y: 予測したい値(目的変数)、w: 重み
x y w
1 2 2
2 4 2
3 6 2
4
??
2
Train
- y = x * w
- 学習開始時はwの値を1とする
- 誤差(loss)は以下の通り
x y w loss
1 2 1 1
2 4 1 2
3 6 1 3
Train
- 誤差から勾配を算出しwに反映させる
- w = w - 学習率 * grad
- 勾配が正しければwの値が1->2へ近似していく
epoch: 学習回数、
epoch grad w
1 ?? 1
2 ?? 1.5
i ?? 2
grad
def loss_fn(x, y, w):
return (x * w - y) ** 2
- 損失関数を以下の関数とした場合
- 勾配は以下の式で求められる
requires_grad
x, y = [1,2,3], [2,4,6]
w = torch.tensor([1.], requires_grad=True)
loss = loss_fn(x[0], y[0], w)
loss.backward()
print(w.grad)
=> tensor([-2.])
- torch作成時にrequires_grad=Trueで追跡開始
- 損失関数の戻り値をbackwardすると勾配を算出
- 2*1*(1*1-2) = -2なので微分は合ってた(良かった)
train
x, y = [1,2,3], [2,4,6]
w = torch.tensor([1.], requires_grad=True)
for _x, _y in zip(x, y):
loss = loss_fn(_x, _y, w)
loss.backward()
w.data = w.item() - 0.01 * w.grad.data
# 勾配を0に初期化する
w.grad.data.zero_()
print( w.item() )
=> 1.260688066482544
- 上記を踏まえた実装は以下の通り
- 何回か学習させればOKっぽい
train
x, y = [1,2,3], [2,4,6]
w = torch.tensor([1.], requires_grad=True)
for epoch in range(1, 20):
for _x, _y in zip(x, y):
loss = loss_fn(_x, _y, w)
loss.backward()
w.data = w.item() - 0.01 * w.grad.data
# 勾配を0に初期化する
w.grad.data.zero_()
print( w.item() )
=> 1.9967811107635498
- 学習回数を20回にして見たところいい感じに
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57
train graph
- y軸がwのグラフは以下の通り
- いい感じに2に収束しているのがわかる
終わり

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Pytorch 02

  • 3. Train - 以下の値にてx=4,y=?を予測したい x: 予測に使う値(説明変数)、y: 予測したい値(目的変数)、w: 重み x y w 1 2 2 2 4 2 3 6 2 4 ?? 2
  • 4. Train - y = x * w - 学習開始時はwの値を1とする - 誤差(loss)は以下の通り x y w loss 1 2 1 1 2 4 1 2 3 6 1 3
  • 5. Train - 誤差から勾配を算出しwに反映させる - w = w - 学習率 * grad - 勾配が正しければwの値が1->2へ近似していく epoch: 学習回数、 epoch grad w 1 ?? 1 2 ?? 1.5 i ?? 2
  • 6. grad def loss_fn(x, y, w): return (x * w - y) ** 2 - 損失関数を以下の関数とした場合 - 勾配は以下の式で求められる
  • 7. requires_grad x, y = [1,2,3], [2,4,6] w = torch.tensor([1.], requires_grad=True) loss = loss_fn(x[0], y[0], w) loss.backward() print(w.grad) => tensor([-2.]) - torch作成時にrequires_grad=Trueで追跡開始 - 損失関数の戻り値をbackwardすると勾配を算出 - 2*1*(1*1-2) = -2なので微分は合ってた(良かった)
  • 8. train x, y = [1,2,3], [2,4,6] w = torch.tensor([1.], requires_grad=True) for _x, _y in zip(x, y): loss = loss_fn(_x, _y, w) loss.backward() w.data = w.item() - 0.01 * w.grad.data # 勾配を0に初期化する w.grad.data.zero_() print( w.item() ) => 1.260688066482544 - 上記を踏まえた実装は以下の通り - 何回か学習させればOKっぽい
  • 9. train x, y = [1,2,3], [2,4,6] w = torch.tensor([1.], requires_grad=True) for epoch in range(1, 20): for _x, _y in zip(x, y): loss = loss_fn(_x, _y, w) loss.backward() w.data = w.item() - 0.01 * w.grad.data # 勾配を0に初期化する w.grad.data.zero_() print( w.item() ) => 1.9967811107635498 - 学習回数を20回にして見たところいい感じに
  • 10. 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 train graph - y軸がwのグラフは以下の通り - いい感じに2に収束しているのがわかる