第15章自然言語処理:事前学習
15.3.5. Loading Training Examples in Minibatches15.3.5 ミニバッチで訓練事例を読み込む
After all the center words together with their context words and sampled noise words are extracted, they will be transformed into minibatches of examples that can be iteratively loaded during training.
英語のヒント
すべての中心語を、その文脈語と抜き出したノイズ語とともに抽出したら、それらを、訓練のあいだに反復して読み込める事例のミニバッチに変換する。
In a minibatch, the example includes a center word and its context words and noise words. Due to varying context window sizes, varies for different . Thus, for each example we concatenate its context words and noise words in the
contexts_negatives variable, and pad zeros until the concatenation length reaches (max_len). To exclude paddings in the calculation of the loss, we define a mask variable masks. There is a one-to-one correspondence between elements in masks and elements in contexts_negatives, where zeros (otherwise ones) in masks correspond to paddings in contexts_negatives.英語のヒント
ミニバッチの中で、の事例は、中心語一つと、その個の文脈語と個のノイズ語を含む。文脈窓の大きさがまちまちなので、はごとに異なる。そこで、各事例について、その文脈語とノイズ語を変数
contexts_negativesの中で連結し、連結の長さが(max_len)に達するまで0を詰める。損失の計算でパディングを除くため、マスク変数masksを定義する。masksの要素とcontexts_negativesの要素の間には一対一の対応があり、masksの0(そうでなければ1)はcontexts_negativesのパディングに対応する。To distinguish between positive and negative examples, we separate context words from noise words in
contexts_negatives via a labels variable. Similar to masks, there is also a one-to-one correspondence between elements in labels and elements in contexts_negatives, where ones (otherwise zeros) in labels correspond to context words (positive examples) in contexts_negatives.英語のヒント
正例と負例を区別するため、変数
labelsを使って、contexts_negativesの中の文脈語をノイズ語から分ける。masksと同じように、labelsの要素とcontexts_negativesの要素の間にも一対一の対応があり、labelsの1(そうでなければ0)はcontexts_negativesの文脈語(正例)に対応する。The above idea is implemented in the following
batchify function. Its input data is a list with length equal to the batch size, where each element is an example consisting of the center word center, its context words context, and its noise words negative. This function returns a minibatch that can be loaded for calculations during training, such as including the mask variable.英語のヒント
上の考え方は、次の
batchify関数で実装する。その入力dataは、長さがバッチの大きさに等しいリストで、各要素は、中心語center、その文脈語context、そのノイズ語negativeからなる事例である。この関数は、マスク変数を含めるなどして、訓練のあいだの計算のために読み込めるミニバッチを返す。#@save
def batchify(data):
"""Return a minibatch of examples for skip-gram with negative sampling."""
max_len = max(len(c) + len(n) for _, c, n in data)
centers, contexts_negatives, masks, labels = [], [], [], []
for center, context, negative in data:
cur_len = len(context) + len(negative)
centers += [center]
contexts_negatives += [context + negative + [0] * (max_len - cur_len)]
masks += [[1] * cur_len + [0] * (max_len - cur_len)]
labels += [[1] * len(context) + [0] * (max_len - len(context))]
return (torch.tensor(centers).reshape((-1, 1)), torch.tensor(
contexts_negatives), torch.tensor(masks), torch.tensor(labels))Let’s test this function using a minibatch of two examples.
英語のヒント
二つの事例からなるミニバッチを使って、この関数を試そう。
x_1 = (1, [2, 2], [3, 3, 3, 3])
x_2 = (1, [2, 2, 2], [3, 3])
batch = batchify((x_1, x_2))
names = ['centers', 'contexts_negatives', 'masks', 'labels']
for name, data in zip(names, batch):
print(name, '=', data)centers = tensor([[1],
[1]])
contexts_negatives = tensor([[2, 2, 3, 3, 3, 3],
[2, 2, 2, 3, 3, 0]])
masks = tensor([[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0]])
labels = tensor([[1, 1, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0]])