第22章付録:深層学習のための数学
22.10.1. Evaluating and Comparing Estimators22.10.1 推定量を評価して比べる
In statistics, an estimator is a function of given samples used to estimate the true parameter . We will write for the estimate of after observing the samples {}.
英語のヒント
統計学では、推定量とは、真のパラメータを推定するのに使われる、与えられた標本の関数である。標本{}を観測した後のの推定値をと書く。
We have seen simple examples of estimators before in section Section 22.7. If you have a number of samples from a Bernoulli random variable, then the maximum likelihood estimate for the probability the random variable is one can be obtained by counting the number of ones observed and dividing by the total number of samples. Similarly, an exercise asked you to show that the maximum likelihood estimate of the mean of a Gaussian given a number of samples is given by the average value of all the samples. These estimators will almost never give the true value of the parameter, but ideally for a large number of samples the estimate will be close.
英語のヒント
推定量の簡単な例は、前に22.7節で見た。ベルヌーイ確率変数からの標本がいくつかあれば、確率変数が1である確率の最尤推定値は、観測された1の数を数えて標本の総数で割ることで得られる。同様に、練習問題では、いくつかの標本が与えられたときのガウス分布の平均の最尤推定値が、すべての標本の平均値で与えられることを示すよう求めた。これらの推定量がパラメータの真の値を与えることはほとんどないが、理想的には、標本の数が多ければ推定値は近くなる。
As an example, we show below the true density of a Gaussian random variable with mean zero and variance one, along with a collection samples from that Gaussian. We constructed the coordinate so every point is visible and the relationship to the original density is clearer.
英語のヒント
例として、平均0、分散1のガウス確率変数の真の密度を、そのガウス分布からの標本の集まりとともに以下に示す。すべての点が見えて、もとの密度との関係がよりはっきりするように、座標を作った。
import torch
from d2l import torch as d2l
torch.pi = torch.acos(torch.zeros(1)) * 2 #define pi in torch
# Sample datapoints and create y coordinate
epsilon = 0.1
torch.manual_seed(8675309)
xs = torch.randn(size=(300,))
ys = torch.tensor(
[torch.sum(torch.exp(-(xs[:i] - xs[i])**2 / (2 * epsilon**2))\
/ torch.sqrt(2*torch.pi*epsilon**2)) / len(xs)\
for i in range(len(xs))])
# Compute true density
xd = torch.arange(torch.min(xs), torch.max(xs), 0.01)
yd = torch.exp(-xd**2/2) / torch.sqrt(2 * torch.pi)
# Plot the results
d2l.plot(xd, yd, 'x', 'density')
d2l.plt.scatter(xs, ys)
d2l.plt.axvline(x=0)
d2l.plt.axvline(x=torch.mean(xs), linestyle='--', color='purple')
d2l.plt.title(f'sample mean: {float(torch.mean(xs).item()):.2f}')
d2l.plt.show()There can be many ways to compute an estimator of a parameter . In this section, we introduce three common methods to evaluate and compare estimators: the mean squared error, the standard deviation, and statistical bias.
英語のヒント
パラメータの推定量を計算する方法はたくさんありうる。この節では、推定量を評価して比べるための三つのよくある方法を紹介する。平均二乗誤差、標準偏差、統計的バイアスである。
22.10.1.1. Mean Squared Error22.10.1.1 平均二乗誤差
Perhaps the simplest metric used to evaluate estimators is the mean squared error (MSE) (or loss) estimator which can be defined as
英語のヒント
推定量を評価するのに使われる最も単純な指標は、おそらく平均二乗誤差(MSE)(あるいは損失)推定量であり、次のように定義できる。
(22.10.1)
This allows us to quantify the average squared deviation from the true value. MSE is always non-negative. If you have read Section 3.1, you will recognize it as the most commonly used regression loss function. As a measure to evaluate an estimator, the closer its value to zero, the closer the estimator is close to the true parameter .
英語のヒント
これにより、真の値からの平均的な二乗偏差を定量化できる。MSEはいつも非負である。3.1節を読んでいれば、これが最もよく使われる回帰の損失関数であることに気づくだろう。推定量を評価する尺度としては、その値が0に近いほど、推定量は真のパラメータに近い。
22.10.1.2. Statistical Bias22.10.1.2 統計的バイアス
The MSE provides a natural metric, but we can easily imagine multiple different phenomena that might make it large. Two fundamentally important are fluctuation in the estimator due to randomness in the dataset, and systematic error in the estimator due to the estimation procedure.
英語のヒント
MSEは自然な指標を与えるが、それを大きくしうるいくつもの異なる現象が簡単に想像できる。根本的に重要な二つは、データセットのランダムさによる推定量の揺らぎと、推定の手順による推定量の系統誤差である。
First, let’s measure the systematic error. For an estimator , the mathematical illustration of statistical bias can be defined as
英語のヒント
まず、系統誤差を測ろう。推定量について、統計的バイアスを数学的に表すと、次のように定義できる。
(22.10.2)
Note that when , the expectation of the estimator is equal to the true value of parameter. In this case, we say is an unbiased estimator. In general, an unbiased estimator is better than a biased estimator since its expectation is the same as the true parameter.
英語のヒント
のとき、推定量の期待値はパラメータの真の値に等しいことに注意しよう。この場合、は不偏推定量であるという。一般に、不偏推定量は、その期待値が真のパラメータと同じなので、バイアスのある推定量よりよい。
It is worth being aware, however, that biased estimators are frequently used in practice. There are cases where unbiased estimators do not exist without further assumptions, or are intractable to compute. This may seem like a significant flaw in an estimator, however the majority of estimators encountered in practice are at least asymptotically unbiased in the sense that the bias tends to zero as the number of available samples tends to infinity: .
英語のヒント
しかし、バイアスのある推定量が実際によく使われることには気づいておく価値がある。さらなる仮定がなければ不偏推定量が存在しない場合や、計算するのが手に負えない場合がある。これは推定量の重大な欠点に見えるかもしれないが、実際に出会う推定量の大多数は、少なくとも漸近的には不偏である。つまり、使える標本の数が無限大に向かうにつれてバイアスが0に向かう、という意味においてである。
22.10.1.3. Variance and Standard Deviation22.10.1.3 分散と標準偏差
Second, let’s measure the randomness in the estimator. Recall from Section 22.6, the standard deviation (or standard error) is defined as the squared root of the variance. We may measure the degree of fluctuation of an estimator by measuring the standard deviation or variance of that estimator.
英語のヒント
次に、推定量のランダムさを測ろう。22.6節から思い出すと、標準偏差(あるいは標準誤差)は分散の平方根として定義される。推定量の揺らぎの度合いは、その推定量の標準偏差か分散を測ることで測れる。
(22.10.3)
It is important to compare (22.10.3) to (22.10.1). In this equation we do not compare to the true population value , but instead to , the expected sample mean. Thus we are not measuring how far the estimator tends to be from the true value, but instead we measuring the fluctuation of the estimator itself.
英語のヒント
22.10.1.4. The Bias-Variance Trade-off22.10.1.4 バイアスと分散のトレードオフ
It is intuitively clear that these two main components contribute to the mean squared error. What is somewhat shocking is that we can show that this is actually a decomposition of the mean squared error into these two contributions plus a third one. That is to say that we can write the mean squared error as the sum of the square of the bias, the variance and the irreducible error.
英語のヒント
この二つの主な構成要素が平均二乗誤差に寄与することは、直観的には明らかである。いくらか衝撃的なのは、これが実際には平均二乗誤差をこの二つの寄与ともう一つの寄与に分解したものであることを示せることである。つまり、平均二乗誤差は、バイアスの2乗、分散、削減できない誤差の和として書ける。
(22.10.4)
We refer the above formula as bias-variance trade-off. The mean squared error can be divided into three sources of error: the error from high bias, the error from high variance and the irreducible error. The bias error is commonly seen in a simple model (such as a linear regression model), which cannot extract high dimensional relations between the features and the outputs. If a model suffers from high bias error, we often say it is underfitting or lack of flexibilty as introduced in (Section 3.6). The high variance usually results from a too complex model, which overfits the training data. As a result, an overfitting model is sensitive to small fluctuations in the data. If a model suffers from high variance, we often say it is overfitting and lack of generalization as introduced in (Section 3.6). The irreducible error is the result from noise in the itself.
英語のヒント
上の式を、バイアスと分散のトレードオフと呼ぶ。平均二乗誤差は、三つの誤差の源に分けられる。高いバイアスによる誤差、高い分散による誤差、削減できない誤差である。バイアスによる誤差は、特徴と出力の間の高次元の関係を取り出せない、単純なモデル(線形回帰のモデルなど)でよく見られる。モデルが高いバイアスによる誤差に悩まされているとき、(3.6節で紹介したように)それは未学習である、あるいは柔軟性が足りないとよく言う。高い分散は、ふつう訓練データに過学習する複雑すぎるモデルから生じる。その結果、過学習したモデルはデータの小さな揺らぎに敏感になる。モデルが高い分散に悩まされているとき、(3.6節で紹介したように)それは過学習している、あるいは汎化が足りないとよく言う。削減できない誤差は、そのものに含まれるノイズから生じる。
22.10.1.5. Evaluating Estimators in Code22.10.1.5 コードで推定量を評価する
Since the standard deviation of an estimator has been implementing by simply calling
a.std() for a tensor a, we will skip it but implement the statistical bias and the mean squared error.英語のヒント
推定量の標準偏差は、テンソル
aについてa.std()を呼ぶだけで実装されているので、それは飛ばし、統計的バイアスと平均二乗誤差を実装する。# Statistical bias
def stat_bias(true_theta, est_theta):
return(torch.mean(est_theta) - true_theta)
# Mean squared error
def mse(data, true_theta):
return(torch.mean(torch.square(data - true_theta)))To illustrate the equation of the bias-variance trade-off, let’s simulate of normal distribution with samples. Here, we use a and . As the estimator is a function of the given samples, here we use the mean of the samples as an estimator for true in this normal distribution .
英語のヒント
バイアスと分散のトレードオフの式を示すため、個の標本で正規分布をシミュレーションしよう。ここではとを使う。推定量は与えられた標本の関数なので、ここでは標本の平均を、この正規分布の真のの推定量として使う。
theta_true = 1
sigma = 4
sample_len = 10000
samples = torch.normal(theta_true, sigma, size=(sample_len, 1))
theta_est = torch.mean(samples)
theta_esttensor(1.0170)Let’s validate the trade-off equation by calculating the summation of the squared bias and the variance of our estimator. First, calculate the MSE of our estimator.
英語のヒント
推定量のバイアスの2乗と分散の和を計算して、トレードオフの式を検証しよう。まず、推定量のMSEを計算する。
mse(samples, theta_true)tensor(16.0298)Next, we calculate as below. As you can see, the two values agree to numerical precision.
英語のヒント
次に、を以下のように計算する。見てのとおり、二つの値は数値精度の範囲で一致する。
bias = stat_bias(theta_true, theta_est)
torch.square(samples.std(unbiased=False)) + torch.square(bias)tensor(16.0298)