第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 . 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個の事例を生成する。得られる計画行列はに属する。各ラベルは、正解の線形関数を当てはめ、事例ごとに独立に同じ分布から取り出した加法的なノイズで乱して生成する。
(3.3.1)
For convenience we assume that is drawn from a normal distribution with mean and standard deviation . 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.英語のヒント
便宜上、は平均、標準偏差の正規分布から取り出されると仮定する。オブジェクト指向設計のために、コードを
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 + noiseBelow, we set the true parameters to and . Later, we can check our estimated parameters against these ground truth values.
英語のヒント
下で、本当のパラメータをとに設定する。後で、推定したパラメータをこれらの正解の値と照らし合わせられる。
data = SyntheticRegressionData(w=torch.tensor([2, -3.4]), b=4.2)Each row in
features consists of a vector in and each row in labels is a scalar. Let’s have a look at the first entry.英語のヒント
featuresの各行はのベクトルからなり、labelsの各行はスカラーである。最初の項目を見てみよう。print('features:', data.X[0],'\nlabel:', data.y[0])features: tensor([0.9026, 1.0264])
label: tensor([2.5148])