SlideShare a Scribd company logo
PytorchのDataLoader
- torchtextのソースコードを読んでみた-
20170904 松尾研 曽根岡
1
PytorchのDataLoader周り
2
実装手順
• DataLoader
• モデル作成
• 損失関数
• 訓練
• ハイパーパラメータチューニング
3
実装手順
• DataLoader ← 当たり前だけど最初にやるべき
• モデル作成
• 損失関数
• 訓練
• ハイパーパラメータチューニング
4
DataLoader周り実装手順
1.DataSetの作成

DataSetのサブクラスでラップする
2.Dataの前処理

Transformで前処理を定義する
3.DataLoader

DataLoaderでDatasetをバッチで取り出せるようにする
5
1. DataSet
• torch.utils.data.Dataset :ただの抽象クラス

(getとlenをoverrideして使う)
6
https://github.com/pytorch/pytorch/blob/master/torch/utils/data/dataset.py
1. DataSet例:FaceLandmarks
7http://pytorch.org/tutorials/beginner/data_loading_tutorial.html
1. DataSet例:FaceLandmarks
8http://pytorch.org/tutorials/beginner/data_loading_tutorial.html
← getで取るときに変換
2. Transform
TransformオブジェクトをDatasetに渡すことで前処理を行う
9
2. Transform
TransformオブジェクトをDatasetに渡すことで前処理を行う
10
← callを定義する (initも書く)
2.Transforms
復数の前処理:torchvision.transforms.Compose
11
(余談)2.Transforms
復数の前処理:torchvision.transforms.Compose
12
ただfor文回すだけ。なぜtorchvisionなのか不明
2. Transform
• torchvision.transforms内に画像系はすでにある
- Scale:大きさ変更
- CenterCrop:真ん中でクロッピング
- RandomCrop:ランダムにクロッピング
- RandomHorizontalFlip:ある確率でFlip
- Normalize:正規化
- ToTensor:PIL.ImageやnumpyをTensor化
13http://pytorch.org/docs/master/torchvision/transforms.html
3. DataLoader
Datasetを渡すことでミニバッチを返すIterableなオブジェクトに
14
マルチスレッドとかいろいろやってくれている優秀なやつ
余談
• torchvisionのLoaderはpytorch以外でも使える
- torchのモデルに依存せずnumpy等で返してくれる
- torch.util.data.Datasetだけは使う
• 他のloaderってどうなのだろう?
15
ここまでは
チュートリアルとDocでわかること

(ソースコードも少し)
16
[WIP]torchtextを読み解いてみた
2017/9/4時点
17
torch text
• Pytorchグループが作るテキスト処理用のDataLoader

https://github.com/pytorch/text
• torchvisionやpytorchのdataloaderとは別物感
- 複雑な構造
- ドキュメントが薄い
- pipで入らない(git cloneしてdirectory取ってきた
18
結構つらいので、他にいいのがあればそれでもいいかも
利用できるデータセット
• ポジネガ分析:SST, IMDbb
• 質問分類:TREC
• 自然言語推論:SNLI
• 原簿モデリング:Wikitext-2
19
ただデータを欲しい時
20
イテレータの作成
イテレータを回すとき
DataSet構造
21
Dataset
Example Field
Vocabfieldの名前属性に
前処理済みのデータ
Preprocess
itos stoi len vectors
DataSet構造
22
Dataset
Example Field
Vocabfieldの名前属性に
前処理済みのデータ
Preprocess
itos stoi len vectors
Dataset
• initでself.examples , self.fieldsを保持
• splits:train, dev, validをそれぞれのpathから返す
• get, getattr, iter, len:exampleの値を返す
• サブクラス:TabularDataset, ZipDataset
23
DataSet構造
24
Dataset
Example Field
Vocabfieldの名前属性に
前処理済みのデータ
Preprocess
itos stoi len vectors
Example
• 前処理後のデータ群
- TextFieldが持つpreprocessを使って前処理
- Datasetのfields(辞書)のkeyでアクセス可
25
DataSet構造
26
Dataset
Example Field
Vocabfieldの名前属性に
前処理済みのデータ
Preprocess
itos stoi len vectors
Field
• データの種類ごとの前処理とVocabを保持
• .preprocess(data):設定した前処理を行う
- tokenize引数で前処理を渡す
• .build_vocab(data):引数のデータからvocab作成
• .pad(minibatch):paddingされたデータを作成
• .numericalize(data):Variableに変換
27
DataSet構造
28
Dataset
Example Field
Vocabfieldの名前属性に
前処理済みのデータ
Preprocess
itos stoi len vectors
Vocab
• テキスト関係の処理
• init:データから下記を作成
- freqs:単語のカウンター
- itos:indexから文字のリスト
- stoi:文字からindexのリスト
- wordvectors:埋め込み行列(Tensor型
• wv_typeを引数で渡すとgloveとかを取る
29
Iterator周り
• Datasetを引数にミニバッチを返すiterator
- pytorchのでデフォルトと異なる
- Padding最小にするBucketIteratorなど、

テキスト周り用に少しカスタマイズされている
• Batchオブジェクトを返す
30

More Related Content

What's hot (20)

PPTX
データサイエンティスト向け性能問題対応の基礎
Tetsutaro Watanabe
 
PDF
Active Learning 入門
Shuyo Nakatani
 
PDF
音声認識と深層学習
Preferred Networks
 
PPTX
モデル高速化百選
Yusuke Uchida
 
PDF
【DL輪読会】DINOv2: Learning Robust Visual Features without Supervision
Deep Learning JP
 
PDF
(修正)機械学習デザインパターン(ML Design Patterns)の解説
Hironori Washizaki
 
PDF
【メタサーベイ】数式ドリブン教師あり学習
cvpaper. challenge
 
PDF
自己教師学習(Self-Supervised Learning)
cvpaper. challenge
 
PPTX
未出現事象の出現確率
Hiroshi Nakagawa
 
PDF
[DL輪読会]Reward Augmented Maximum Likelihood for Neural Structured Prediction
Deep Learning JP
 
PDF
形態素解析の過去・現在・未来
Preferred Networks
 
PPTX
DockerコンテナでGitを使う
Kazuhiro Suga
 
PPTX
論文の図表レイアウト例
Sunao Hara
 
PDF
【メタサーベイ】基盤モデル / Foundation Models
cvpaper. challenge
 
PDF
時系列分析による異常検知入門
Yohei Sato
 
PDF
トピックモデルの評価指標 Perplexity とは何なのか?
hoxo_m
 
PDF
多人数不完全情報ゲームにおけるAI ~ポーカーと麻雀を例として~
Kenshi Abe
 
PDF
Hyperoptとその周辺について
Keisuke Hosaka
 
PDF
強化学習と逆強化学習を組み合わせた模倣学習
Eiji Uchibe
 
PDF
【DL輪読会】Aspect-based Analysis of Advertising Appeals for Search Engine Advert...
Deep Learning JP
 
データサイエンティスト向け性能問題対応の基礎
Tetsutaro Watanabe
 
Active Learning 入門
Shuyo Nakatani
 
音声認識と深層学習
Preferred Networks
 
モデル高速化百選
Yusuke Uchida
 
【DL輪読会】DINOv2: Learning Robust Visual Features without Supervision
Deep Learning JP
 
(修正)機械学習デザインパターン(ML Design Patterns)の解説
Hironori Washizaki
 
【メタサーベイ】数式ドリブン教師あり学習
cvpaper. challenge
 
自己教師学習(Self-Supervised Learning)
cvpaper. challenge
 
未出現事象の出現確率
Hiroshi Nakagawa
 
[DL輪読会]Reward Augmented Maximum Likelihood for Neural Structured Prediction
Deep Learning JP
 
形態素解析の過去・現在・未来
Preferred Networks
 
DockerコンテナでGitを使う
Kazuhiro Suga
 
論文の図表レイアウト例
Sunao Hara
 
【メタサーベイ】基盤モデル / Foundation Models
cvpaper. challenge
 
時系列分析による異常検知入門
Yohei Sato
 
トピックモデルの評価指標 Perplexity とは何なのか?
hoxo_m
 
多人数不完全情報ゲームにおけるAI ~ポーカーと麻雀を例として~
Kenshi Abe
 
Hyperoptとその周辺について
Keisuke Hosaka
 
強化学習と逆強化学習を組み合わせた模倣学習
Eiji Uchibe
 
【DL輪読会】Aspect-based Analysis of Advertising Appeals for Search Engine Advert...
Deep Learning JP
 

Viewers also liked (16)

PPTX
[DL輪読会]Learning by Association - A versatile semi-supervised training method ...
Deep Learning JP
 
PDF
もしその単語がなかったら
Hiroshi Nakagawa
 
PDF
PoisoningAttackSVM (ICMLreading2012)
Hidekazu Oiwa
 
PPTX
20171024NL研報告スライド
Masatoshi TSUCHIYA
 
PDF
深層学習の判断根拠を理解するための 研究とその意義 @PRMU 2017熊本
Takahiro Kubo
 
PDF
Dragon: A Distributed Object Storage at Yahoo! JAPAN (WebDB Forum 2017)
Yahoo!デベロッパーネットワーク
 
PDF
StanとRでベイズ統計モデリング読書会 Chapter 7(7.6-7.9) 回帰分析の悩みどころ ~統計の力で歌うまになりたい~
nocchi_airport
 
PDF
スキルチェックリスト 2017年版
The Japan DataScientist Society
 
PPTX
多項式あてはめで眺めるベイズ推定 ~今日からきみもベイジアン~
tanutarou
 
PDF
新規事業・起業を妨げる「ビジネスモデル症候群」とは
Lean Startup Japan LLC
 
PDF
確率的プログラミングライブラリEdward
Yuta Kashino
 
PDF
AWS Black Belt - AWS Glue
Amazon Web Services Japan
 
PDF
すべてを自動化せよ! 〜生産性向上チームの挑戦〜
Jumpei Miyata
 
PDF
Tokyo webmining 2017-10-28
Kimikazu Kato
 
PDF
(DL hacks輪読)Bayesian Neural Network
Masahiro Suzuki
 
PPTX
本当は恐ろしい分散システムの話
Kumazaki Hiroki
 
[DL輪読会]Learning by Association - A versatile semi-supervised training method ...
Deep Learning JP
 
もしその単語がなかったら
Hiroshi Nakagawa
 
PoisoningAttackSVM (ICMLreading2012)
Hidekazu Oiwa
 
20171024NL研報告スライド
Masatoshi TSUCHIYA
 
深層学習の判断根拠を理解するための 研究とその意義 @PRMU 2017熊本
Takahiro Kubo
 
Dragon: A Distributed Object Storage at Yahoo! JAPAN (WebDB Forum 2017)
Yahoo!デベロッパーネットワーク
 
StanとRでベイズ統計モデリング読書会 Chapter 7(7.6-7.9) 回帰分析の悩みどころ ~統計の力で歌うまになりたい~
nocchi_airport
 
スキルチェックリスト 2017年版
The Japan DataScientist Society
 
多項式あてはめで眺めるベイズ推定 ~今日からきみもベイジアン~
tanutarou
 
新規事業・起業を妨げる「ビジネスモデル症候群」とは
Lean Startup Japan LLC
 
確率的プログラミングライブラリEdward
Yuta Kashino
 
AWS Black Belt - AWS Glue
Amazon Web Services Japan
 
すべてを自動化せよ! 〜生産性向上チームの挑戦〜
Jumpei Miyata
 
Tokyo webmining 2017-10-28
Kimikazu Kato
 
(DL hacks輪読)Bayesian Neural Network
Masahiro Suzuki
 
本当は恐ろしい分散システムの話
Kumazaki Hiroki
 

More from Deep Learning JP (20)

PPTX
【DL輪読会】AdaptDiffuser: Diffusion Models as Adaptive Self-evolving Planners
Deep Learning JP
 
PPTX
【DL輪読会】事前学習用データセットについて
Deep Learning JP
 
PPTX
【DL輪読会】 "Learning to render novel views from wide-baseline stereo pairs." CVP...
Deep Learning JP
 
PPTX
【DL輪読会】Zero-Shot Dual-Lens Super-Resolution
Deep Learning JP
 
PPTX
【DL輪読会】BloombergGPT: A Large Language Model for Finance arxiv
Deep Learning JP
 
PPTX
【DL輪読会】マルチモーダル LLM
Deep Learning JP
 
PDF
【 DL輪読会】ToolLLM: Facilitating Large Language Models to Master 16000+ Real-wo...
Deep Learning JP
 
PPTX
【DL輪読会】AnyLoc: Towards Universal Visual Place Recognition
Deep Learning JP
 
PDF
【DL輪読会】Can Neural Network Memorization Be Localized?
Deep Learning JP
 
PPTX
【DL輪読会】Hopfield network 関連研究について
Deep Learning JP
 
PPTX
【DL輪読会】SimPer: Simple self-supervised learning of periodic targets( ICLR 2023 )
Deep Learning JP
 
PDF
【DL輪読会】RLCD: Reinforcement Learning from Contrast Distillation for Language M...
Deep Learning JP
 
PDF
【DL輪読会】"Secrets of RLHF in Large Language Models Part I: PPO"
Deep Learning JP
 
PPTX
【DL輪読会】"Language Instructed Reinforcement Learning for Human-AI Coordination "
Deep Learning JP
 
PPTX
【DL輪読会】Llama 2: Open Foundation and Fine-Tuned Chat Models
Deep Learning JP
 
PDF
【DL輪読会】"Learning Fine-Grained Bimanual Manipulation with Low-Cost Hardware"
Deep Learning JP
 
PPTX
【DL輪読会】Parameter is Not All You Need:Starting from Non-Parametric Networks fo...
Deep Learning JP
 
PDF
【DL輪読会】Drag Your GAN: Interactive Point-based Manipulation on the Generative ...
Deep Learning JP
 
PDF
【DL輪読会】Self-Supervised Learning from Images with a Joint-Embedding Predictive...
Deep Learning JP
 
PPTX
【DL輪読会】Towards Understanding Ensemble, Knowledge Distillation and Self-Distil...
Deep Learning JP
 
【DL輪読会】AdaptDiffuser: Diffusion Models as Adaptive Self-evolving Planners
Deep Learning JP
 
【DL輪読会】事前学習用データセットについて
Deep Learning JP
 
【DL輪読会】 "Learning to render novel views from wide-baseline stereo pairs." CVP...
Deep Learning JP
 
【DL輪読会】Zero-Shot Dual-Lens Super-Resolution
Deep Learning JP
 
【DL輪読会】BloombergGPT: A Large Language Model for Finance arxiv
Deep Learning JP
 
【DL輪読会】マルチモーダル LLM
Deep Learning JP
 
【 DL輪読会】ToolLLM: Facilitating Large Language Models to Master 16000+ Real-wo...
Deep Learning JP
 
【DL輪読会】AnyLoc: Towards Universal Visual Place Recognition
Deep Learning JP
 
【DL輪読会】Can Neural Network Memorization Be Localized?
Deep Learning JP
 
【DL輪読会】Hopfield network 関連研究について
Deep Learning JP
 
【DL輪読会】SimPer: Simple self-supervised learning of periodic targets( ICLR 2023 )
Deep Learning JP
 
【DL輪読会】RLCD: Reinforcement Learning from Contrast Distillation for Language M...
Deep Learning JP
 
【DL輪読会】"Secrets of RLHF in Large Language Models Part I: PPO"
Deep Learning JP
 
【DL輪読会】"Language Instructed Reinforcement Learning for Human-AI Coordination "
Deep Learning JP
 
【DL輪読会】Llama 2: Open Foundation and Fine-Tuned Chat Models
Deep Learning JP
 
【DL輪読会】"Learning Fine-Grained Bimanual Manipulation with Low-Cost Hardware"
Deep Learning JP
 
【DL輪読会】Parameter is Not All You Need:Starting from Non-Parametric Networks fo...
Deep Learning JP
 
【DL輪読会】Drag Your GAN: Interactive Point-based Manipulation on the Generative ...
Deep Learning JP
 
【DL輪読会】Self-Supervised Learning from Images with a Joint-Embedding Predictive...
Deep Learning JP
 
【DL輪読会】Towards Understanding Ensemble, Knowledge Distillation and Self-Distil...
Deep Learning JP
 

Recently uploaded (8)

PDF
Hyperledger Fabric最新v3.x系での機能強化、変更点にキャッチアップ!
LFDT Tokyo Meetup
 
PDF
プライバシ保護のためのインターネットアーキテクチャの進化 (2025-07-11)
Jun Kurihara
 
PDF
20250710_Devinで切り拓くDB革命_〜価値創出に集中せよ〜.pdf
Masaki Yamakawa
 
PDF
PostgreSQL18新機能紹介(db tech showcase 2025 発表資料)
NTT DATA Technology & Innovation
 
PDF
20250711_日本IBM ミドルウエア・ユーザー研究会(JIMUC)総会_中村会長資料.pdf
ChikakoInami1
 
PDF
人気ブロックチェーン基盤「Hyperledger Fabric」最新版を動かしてみた!
LFDT Tokyo Meetup
 
PDF
Hyperledger Fabric公式サンプル fabric-samples徹底解説
LFDT Tokyo Meetup
 
PDF
[Hardening Designers Confernece 2025]ランサムウェアでの見えざるログ・見えるログ
kataware
 
Hyperledger Fabric最新v3.x系での機能強化、変更点にキャッチアップ!
LFDT Tokyo Meetup
 
プライバシ保護のためのインターネットアーキテクチャの進化 (2025-07-11)
Jun Kurihara
 
20250710_Devinで切り拓くDB革命_〜価値創出に集中せよ〜.pdf
Masaki Yamakawa
 
PostgreSQL18新機能紹介(db tech showcase 2025 発表資料)
NTT DATA Technology & Innovation
 
20250711_日本IBM ミドルウエア・ユーザー研究会(JIMUC)総会_中村会長資料.pdf
ChikakoInami1
 
人気ブロックチェーン基盤「Hyperledger Fabric」最新版を動かしてみた!
LFDT Tokyo Meetup
 
Hyperledger Fabric公式サンプル fabric-samples徹底解説
LFDT Tokyo Meetup
 
[Hardening Designers Confernece 2025]ランサムウェアでの見えざるログ・見えるログ
kataware
 

[DLHacks LT] PytorchのDataLoader -torchtextのソースコードを読んでみた-