ChainerのTextDatasetを使ってみる
アドベントカレンダー10日目です。
今日はChainerのTextDatasetを使ってみたいと思います。
TextDatasetとは
Dataset of a line-oriented text file. This dataset reads each line of text file(s) on every call of the getitem() operator. Positions of line boundaries are cached so that you can quickliy random access the text file by the line number.
chainer.datasets.TextDataset — Chainer 5.1.0 documentation
テキストファイルを渡すことでランダムな行にアクセスしたり、メモリの使用量を減らせるとかそんなだったような気がしています。
TextDatasetの使い方
datasets.TextDataset()
にテキストファイルのパスを渡すだけです。
import chainer from chainer import datasets train = datasets.TextDataset("temp.txt") train_iter = chainer.iterators.SerialIterator(train, batch_size=1)
あとはいつも通りtrain_iter.next()
で値が取れます。
ソースコードのサンプルは以下から。
まとめ
今回はTextDatasetを使ってみました。かなりお手軽にテキストを読み込んで処理することができるので、ぜひ使ってみてください。