プリンキピア

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

9.1.4. Prediction9.1.4 予測

To evaluate our model, we first check how well it performs at one-step-ahead prediction.
英語のヒント
モデルを評価するため、まず1ステップ先の予測がどれくらいうまくいくかを確かめる。
onestep_preds = model(data.features).detach().numpy()
d2l.plot(data.time[data.tau:], [data.labels, onestep_preds], 'time', 'x',
         legend=['labels', '1-step preds'], figsize=(6, 3))
These predictions look good, even near the end at t=1000.
英語のヒント
これらの予測は、終わり近くのt=1000でもよく見える。
But what if we only observed sequence data up until time step 604 (n_train + tau) and wished to make predictions several steps into the future? Unfortunately, we cannot directly compute the one-step-ahead prediction for time step 609, because we do not know the corresponding inputs, having seen only up to x604. We can address this problem by plugging in our earlier predictions as inputs to our model for making subsequent predictions, projecting forward, one step at a time, until reaching the desired time step:
英語のヒント
しかし、時間ステップ604(n_train + tau)までしか系列データを観測しておらず、何ステップも先の未来を予測したいとしたらどうだろう。残念ながら、時間ステップ609の1ステップ先の予測を直接計算することはできない。x604までしか見ていないので、対応する入力がわからないからである。この問題には、前の予測をモデルへの入力として差し込んで次の予測を行い、望みの時間ステップに達するまで一度に1ステップずつ先へ進めることで対処できる。
xˆ605=f(x601,x602,x603,x604),xˆ606=f(x602,x603,x604,xˆ605),xˆ607=f(x603,x604,xˆ605,xˆ606),xˆ608=f(x604,xˆ605,xˆ606,xˆ607),xˆ609=f(xˆ605,xˆ606,xˆ607,xˆ608),⋮(9.1.6)
Generally, for an observed sequence x1,…,xt, its predicted output xˆt+k at time step t+k is called the k-step-ahead prediction. Since we have observed up to x604, its k-step-ahead prediction is xˆ604+k. In other words, we will have to keep on using our own predictions to make multistep-ahead predictions. Let’s see how well this goes.
英語のヒント
一般に、観測された系列x1,…,xtについて、時間ステップt+kでの予測出力xˆt+kは、kステップ先の予測と呼ばれる。x604まで観測しているので、そのkステップ先の予測はxˆ604+kである。言い換えれば、何ステップも先の予測をするには、自分自身の予測を使い続けなければならない。これがどれくらいうまくいくか見てみよう。
multistep_preds = torch.zeros(data.T)
multistep_preds[:] = data.x
for i in range(data.num_train + data.tau, data.T):
    multistep_preds[i] = model(
        multistep_preds[i - data.tau:i].reshape((1, -1)))
multistep_preds = multistep_preds.detach().numpy()

d2l.plot([data.time[data.tau:], data.time[data.num_train+data.tau:]],
         [onestep_preds, multistep_preds[data.num_train+data.tau:]], 'time',
         'x', legend=['1-step preds', 'multistep preds'], figsize=(6, 3))
Unfortunately, in this case we fail spectacularly. The predictions decay to a constant pretty quickly after a few steps. Why did the algorithm perform so much worse when predicting further into the future? Ultimately, this is down to the fact that errors build up. Let’s say that after step 1 we have some error ϵ1=ϵ¯. Now the input for step 2 is perturbed by ϵ1, hence we suffer some error in the order of ϵ2=ϵ¯+cϵ1 for some constant c, and so on. The predictions can diverge rapidly from the true observations. You may already be familiar with this common phenomenon. For instance, weather forecasts for the next 24 hours tend to be pretty accurate but beyond that, accuracy declines rapidly. We will discuss methods for improving this throughout this chapter and beyond.
英語のヒント
残念ながら、この場合は見事に失敗する。予測は数ステップ後にはかなり早く定数へと減衰してしまう。もっと先の未来を予測するとき、なぜアルゴリズムの性能はこれほど悪くなったのだろうか。結局のところ、これは誤差が積み重なることによる。ステップ1の後で、ある誤差ϵ1=ϵ¯があるとしよう。すると、ステップ2への入力はϵ1だけ乱されるので、ある定数cについてϵ2=ϵ¯+cϵ1程度の誤差を被り、以下同様に続く。予測は本当の観測から急速に離れていきうる。このよくある現象には、すでになじみがあるかもしれない。たとえば、24時間先までの天気予報はかなり正確な傾向があるが、それを超えると正確さは急速に落ちる。これを改善する方法は、この章を通じて、さらにその先でも論じる。
Let’s take a closer look at the difficulties in k-step-ahead predictions by computing predictions on the entire sequence for k=1,4,16,64.
英語のヒント
k=1,4,16,64について系列全体の予測を計算して、kステップ先の予測の難しさをもっと詳しく見てみよう。
def k_step_pred(k):
    features = []
    for i in range(data.tau):
        features.append(data.x[i : i+data.T-data.tau-k+1])
    # The (i+tau)-th element stores the (i+1)-step-ahead predictions
    for i in range(k):
        preds = model(torch.stack(features[i : i+data.tau], 1))
        features.append(preds.reshape(-1))
    return features[data.tau:]

steps = (1, 4, 16, 64)
preds = k_step_pred(steps[-1])
d2l.plot(data.time[data.tau+steps[-1]-1:],
         [preds[k - 1].detach().numpy() for k in steps], 'time', 'x',
         legend=[f'{k}-step preds' for k in steps], figsize=(6, 3))
This clearly illustrates how the quality of the prediction changes as we try to predict further into the future. While the 4-step-ahead predictions still look good, anything beyond that is almost useless.
英語のヒント
これは、もっと先の未来を予測しようとするにつれて、予測の質がどう変わるかをはっきり示している。4ステップ先の予測はまだよく見えるが、それより先のものはほとんど役に立たない。