プリンキピア

第3章回帰のための線形ニューラルネットワーク

3.3.1. Generating the Dataset3.3.1 データセットの生成

For this example, we will work in low dimension for succinctness. The following code snippet generates 1000 examples with 2-dimensional features drawn from a standard normal distribution. The resulting design matrix 𝐗 belongs to ℝ1000×2. We generate each label by applying a ground truth linear function, corrupting them via additive noise 𝝐, drawn independently and identically for each example:
英語のヒント
この例では、簡潔さのために低い次元で作業する。次のコードの断片は、標準正規分布から取り出した2次元の特徴量をもつ1000個の事例を生成する。得られる計画行列𝐗はℝ1000×2に属する。各ラベルは、正解の線形関数を当てはめ、事例ごとに独立に同じ分布から取り出した加法的なノイズ𝝐で乱して生成する。
𝐲=𝐗𝐰+b+𝝐.(3.3.1)
For convenience we assume that 𝝐 is drawn from a normal distribution with mean μ=0 and standard deviation σ=0.01. Note that for object-oriented design we add the code to the __init__ method of a subclass of d2l.DataModule (introduced in Section 3.2.3). It is good practice to allow the setting of any additional hyperparameters. We accomplish this with save_hyperparameters(). The batch_size will be determined later.
英語のヒント
便宜上、𝝐は平均μ=0、標準偏差σ=0.01の正規分布から取り出されると仮定する。オブジェクト指向設計のために、コードをd2l.DataModule(3.2.3節で紹介した)のサブクラスの__init__メソッドに加えることに注意してほしい。追加のハイパーパラメータを何でも設定できるようにしておくのはよい習慣である。これはsave_hyperparameters()で実現する。batch_sizeは後で決める。
class SyntheticRegressionData(d2l.DataModule):  #@save
    """Synthetic data for linear regression."""
    def __init__(self, w, b, noise=0.01, num_train=1000, num_val=1000,
                 batch_size=32):
        super().__init__()
        self.save_hyperparameters()
        n = num_train + num_val
        self.X = torch.randn(n, len(w))
        noise = torch.randn(n, 1) * noise
        self.y = torch.matmul(self.X, w.reshape((-1, 1))) + b + noise
Below, we set the true parameters to 𝐰=[2,−3.4]⊤ and b=4.2. Later, we can check our estimated parameters against these ground truth values.
英語のヒント
下で、本当のパラメータを𝐰=[2,−3.4]⊤とb=4.2に設定する。後で、推定したパラメータをこれらの正解の値と照らし合わせられる。
data = SyntheticRegressionData(w=torch.tensor([2, -3.4]), b=4.2)
Each row in features consists of a vector in ℝ2 and each row in labels is a scalar. Let’s have a look at the first entry.
英語のヒント
featuresの各行はℝ2のベクトルからなり、labelsの各行はスカラーである。最初の項目を見てみよう。
print('features:', data.X[0],'\nlabel:', data.y[0])
features: tensor([0.9026, 1.0264])
label: tensor([2.5148])