プリンキピア

第9章リカレントニューラルネットワーク

9.1. Working with Sequences9.1 系列を扱う

Up until now, we have focused on models whose inputs consisted of a single feature vector 𝐱∈ℝd. The main change of perspective when developing models capable of processing sequences is that we now focus on inputs that consist of an ordered list of feature vectors 𝐱1,…,𝐱T, where each feature vector 𝐱t is indexed by a time step t∈ℤ+ lying in ℝd.
英語のヒント
これまでは、入力が一つの特徴ベクトル𝐱∈ℝdからなるモデルに焦点を当ててきた。系列を処理できるモデルを作るときの主な視点の変化は、今度は、特徴ベクトルの順序つきのリスト𝐱1,…,𝐱Tからなる入力に焦点を当てることである。ここで、各特徴ベクトル𝐱tはℝdの中にあり、時間ステップt∈ℤ+で番号づけられる。
Some datasets consist of a single massive sequence. Consider, for example, the extremely long streams of sensor readings that might be available to climate scientists. In such cases, we might create training datasets by randomly sampling subsequences of some predetermined length. More often, our data arrives as a collection of sequences. Consider the following examples: (i) a collection of documents, each represented as its own sequence of words, and each having its own length Ti; (ii) sequence representation of patient stays in the hospital, where each stay consists of a number of events and the sequence length depends roughly on the length of the stay.
英語のヒント
一つの巨大な系列からなるデータセットもある。たとえば、気候科学者が手に入れられるかもしれない、センサーの読み取り値のきわめて長い流れを考えよう。そうした場合には、あらかじめ決めた長さの部分系列をランダムにサンプリングして、訓練データセットを作るかもしれない。もっと多いのは、データが系列の集まりとして届く場合である。次の例を考えよう。(i)文書の集まりで、各文書はそれぞれの単語の系列として表され、それぞれの長さTiをもつ。(ii)患者の入院の系列表現で、各入院はいくつもの出来事からなり、系列の長さはおおよそ入院期間の長さに依存する。
Previously, when dealing with individual inputs, we assumed that they were sampled independently from the same underlying distribution P(X). While we still assume that entire sequences (e.g., entire documents or patient trajectories) are sampled independently, we cannot assume that the data arriving at each time step are independent of each other. For example, the words that likely to appear later in a document depend heavily on words occurring earlier in the document. The medicine a patient is likely to receive on the 10th day of a hospital visit depends heavily on what transpired in the previous nine days.
英語のヒント
以前は、個々の入力を扱うとき、それらが同じもとの分布P(X)から独立にサンプリングされると仮定した。系列全体(たとえば文書全体や患者の経過)が独立にサンプリングされるとはまだ仮定するが、各時間ステップに届くデータが互いに独立だとは仮定できない。たとえば、文書の後のほうに出てきそうな単語は、文書の前のほうに出てくる単語に大きく依存する。入院の10日目に患者が受けそうな薬は、それまでの9日間に起きたことに大きく依存する。
This should come as no surprise. If we did not believe that the elements in a sequence were related, we would not have bothered to model them as a sequence in the first place. Consider the usefulness of the auto-fill features that are popular on search tools and modern email clients. They are useful precisely because it is often possible to predict (imperfectly, but better than random guessing) what the likely continuations of a sequence might be, given some initial prefix. For most sequence models, we do not require independence, or even stationarity, of our sequences. Instead, we require only that the sequences themselves are sampled from some fixed underlying distribution over entire sequences.
英語のヒント
これは驚くことではないはずだ。系列の要素どうしが関係していると思わなかったなら、そもそもそれらを系列としてモデル化しようとはしなかっただろう。検索ツールや現代のメールソフトで人気の自動補完の機能の便利さを考えてみよう。それが便利なのはまさに、最初の部分が与えられれば、系列のありそうな続きを(不完全にではあるが、でたらめな推測よりはよく)予測できることが多いからである。ほとんどの系列モデルでは、系列の独立性も、定常性さえも必要としない。その代わり、系列そのものが、系列全体にわたる何らかの固定された分布からサンプリングされることだけを求める。
This flexible approach allows for such phenomena as (i) documents looking significantly different at the beginning than at the end; or (ii) patient status evolving either towards recovery or towards death over the course of a hospital stay; or (iii) customer taste evolving in predictable ways over the course of continued interaction with a recommender system.
英語のヒント
この柔軟なやり方なら、(i)文書の始めと終わりで見た目が大きく違う、(ii)入院の過程で患者の状態が回復に向かうか死に向かうかして変わっていく、(iii)推薦システムとのやりとりを続けるうちに、客の好みが予測できる形で変わっていく、といった現象も扱える。
We sometimes wish to predict a fixed target y given sequentially structured input (e.g., sentiment classification based on a movie review). At other times, we wish to predict a sequentially structured target (y1,…,yT) given a fixed input (e.g., image captioning). Still other times, our goal is to predict sequentially structured targets based on sequentially structured inputs (e.g., machine translation or video captioning). Such sequence-to-sequence tasks take two forms: (i) aligned: where the input at each time step aligns with a corresponding target (e.g., part of speech tagging); (ii) unaligned: where the input and target do not necessarily exhibit a step-for-step correspondence (e.g., machine translation).
英語のヒント
系列として構造化された入力から、固定された目標yを予測したいこともある(たとえば映画のレビューにもとづく感情の分類)。また、固定された入力から、系列として構造化された目標(y1,…,yT)を予測したいこともある(たとえば画像のキャプション生成)。さらに別の場合には、系列として構造化された入力にもとづいて、系列として構造化された目標を予測することが目標になる(たとえば機械翻訳や動画のキャプション生成)。こうした系列から系列への課題には二つの形がある。(i)整列している場合:各時間ステップの入力が対応する目標とそろっている(たとえば品詞タグづけ)。(ii)整列していない場合:入力と目標が必ずしも一歩ずつ対応しない(たとえば機械翻訳)。
Before we worry about handling targets of any kind, we can tackle the most straightforward problem: unsupervised density modeling (also called sequence modeling). Here, given a collection of sequences, our goal is to estimate the probability mass function that tells us how likely we are to see any given sequence, i.e., p(𝐱1,…,𝐱T).
英語のヒント
どんな種類の目標を扱うかを気にする前に、最も素直な問題に取り組める。教師なしの密度のモデリング(系列のモデリングとも呼ばれる)である。ここでは、系列の集まりが与えられたとき、どんな系列でもそれを見る見込みがどれくらいか、つまりp(𝐱1,…,𝐱T)を教えてくれる確率質量関数を推定することが目標である。
%matplotlib inline
import torch
from torch import nn
from d2l import torch as d2l