TensorFlow.org で表示
|
Google Colab で実行
|
GitHubでソースを表示 |
ノートブックをダウンロード |
word2vec は単一のアルゴリズムではなく、大規模なデータセットから単語の埋め込みを学習するために使用できるモデルアーキテクチャと最適化のファミリです。word2vec により学習された埋め込みは、さまざまなダウンストリームの自然言語処理タスクで成功することが証明されています。
注意: このチュートリアルは、ベクトル空間での単語表現の効率的な推定と単語とフレーズの分散表現とその構成に基づいていますが、論文の正確な実装ではなく、重要なアイデアを説明することを目的としています。
これらの論文では、単語の表現を学習するための 2 つの方法が提案されています。
- 連続バッグオブワードモデルでは、周囲のコンテキストワードに基づいて中間の単語を予測します。コンテキストは、与えられた (中間) 単語の前後のいくつかの単語で構成されます。このアーキテクチャでは、コンテキスト内の単語の順序が重要ではないため、バッグオブワードモデルと呼ばれます。
- 連続スキップグラムモデルは、同じ文の与えられた単語の前後の特定の範囲内の単語を予測します。この例を以下に示します。
このチュートリアルでは、スキップグラムアプローチを使用します。最初に、説明のために 1 つの文を使用して、スキップグラムとその他の概念について説明します。次に、小さなデータセットで独自の word2vec モデルをトレーニングします。このチュートリアルには、トレーニング済みの埋め込みをエクスポートして TensorFlow Embedding Projector で可視化するためのコードも含まれています。
スキップグラムとネガティブサンプリング
バッグオブワードモデルは、与えられたコンテキスト (前後の単語) から単語を予測しますが、スキップグラムモデルは、与えられた単語自体から単語のコンテキスト (前後の単語) を予測します。モデルは、トークンをスキップできる n-gram であるスキップグラムでトレーニングされます (例については、下の図を参照してください)。単語のコンテキストは、context_word が target_word の前後のコンテキストに現れる (target_word, context_word) の一連のスキップグラムペアによって表すことができます。
次の 8 つの単語の文を考えてみましょう。
The wide road shimmered in the hot sun.
この文の 8 つの単語のそれぞれのコンテキストワードは、ウィンドウサイズによって定義されます。ウィンドウサイズは、context word と見なすことができる target_word の前後の単語の範囲を指定します。以下は、さまざまなウィンドウサイズに基づくターゲットワードのスキップグラムの表です。
注意: このチュートリアルでは、ウィンドウサイズ n は、前後に n 個の単語があり、合計ウィンドウ 範囲が 2*n+1 個の単語であるということを意味します。

スキップグラムモデルのトレーニングの目的は、与えられたターゲットワードからコンテキストワードを予測する確率を最大化することです。一連の単語 w1、w2、... wT の場合、目的は平均対数確率として記述できます。

ここで、c はトレーニングコンテキストのサイズです。基本的なスキップグラムの定式化では、ソフトマックス関数を使用してこの確率を定義します。

ここで、v と v' は単語のターゲットとコンテキストのベクトル表現であり、W 語彙サイズです。
この定式化の分母を計算するには、語彙全体に対して完全なソフトマックスを実行する必要があります。これは、多くの場合、大きな項 (105-107) です。
ノイズコントラスト推定 (NCE) 損失関数は、完全なソフトマックスの効率的な近似値です。単語の分布をモデル化するのではなく、単語の埋め込みを学習することを目的として、NCE 損失を単純化してネガティブサンプリングを使用することができます。
ターゲットワードの単純化されたネガティブサンプリングの目的は、コンテキストワードをノイズ分布 Pn(w) ワードから抽出された num_ns のネガティブサンプルから区別することです。より正確には、語彙全体の完全なソフトマックスの効率的な近似は、スキップグラムペアの場合、コンテキストワードと num_ns ネガティブサンプル間の分類問題としてターゲットワードの損失を提示します。
ネガティブサンプルは、context_word が target_word の window_size の前後に現れないように、(target_word, context_word) ペアとして定義されます。この例の文の場合、以下はいくつかの潜在的なネガティブサンプルです (window_size が 2 の場合)。
(hot, shimmered)
(wide, hot)
(wide, sun)
次のセクションでは、1 つの文に対してスキップグラムとネガティブサンプルを生成します。また、サブサンプリング手法についても学習し、チュートリアルの後半でポジティブトレーニングとネガティブトレーニングサンプルの分類モデルをトレーニングします。
セットアップ
import io
import re
import string
import tqdm
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers
2022-12-14 23:26:57.448971: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 23:26:57.449067: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 23:26:57.449077: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
# Load the TensorBoard notebook extension
%load_ext tensorboard
SEED = 42
AUTOTUNE = tf.data.AUTOTUNE
例文をベクトル化する
次の文を考えてみましょう。
The wide road shimmered in the hot sun.
文をトークン化します。
sentence = "The wide road shimmered in the hot sun"
tokens = list(sentence.lower().split())
print(len(tokens))
8
トークンから整数インデックスへのマッピングを保存する語彙を作成します。
vocab, index = {}, 1 # start indexing from 1
vocab['<pad>'] = 0 # add a padding token
for token in tokens:
if token not in vocab:
vocab[token] = index
index += 1
vocab_size = len(vocab)
print(vocab)
{'<pad>': 0, 'the': 1, 'wide': 2, 'road': 3, 'shimmered': 4, 'in': 5, 'hot': 6, 'sun': 7}
整数インデックスからトークンへのマッピングを保存する逆語彙を作成します。
inverse_vocab = {index: token for token, index in vocab.items()}
print(inverse_vocab)
{0: '<pad>', 1: 'the', 2: 'wide', 3: 'road', 4: 'shimmered', 5: 'in', 6: 'hot', 7: 'sun'}
文をベクトル化します。
example_sequence = [vocab[word] for word in tokens]
print(example_sequence)
[1, 2, 3, 4, 5, 1, 6, 7]
1 つの文からスキップグラムを生成する
tf.keras.preprocessing.sequence モジュールは、word2vec のデータ準備を簡素化する便利な関数を提供します。 tf.keras.preprocessing.sequence.skipgrams を使用して、範囲 [0, vocab_size) のトークンから指定された window_size で example_sequence からスキップグラムペアを生成します。
注意: negative_samples は、ここでは 0 に設定されています。これは、この関数によって生成されたネガティブサンプルのバッチ処理にコードが少し必要だからです。次のセクションでは、別の関数を使用してネガティブサンプリングを実行します。
window_size = 2
positive_skip_grams, _ = tf.keras.preprocessing.sequence.skipgrams(
example_sequence,
vocabulary_size=vocab_size,
window_size=window_size,
negative_samples=0)
print(len(positive_skip_grams))
26
いくつかのポジティブのスキップグラムを出力します。
for target, context in positive_skip_grams[:5]:
print(f"({target}, {context}): ({inverse_vocab[target]}, {inverse_vocab[context]})")
(3, 5): (road, in) (3, 2): (road, wide) (2, 1): (wide, the) (4, 5): (shimmered, in) (4, 2): (shimmered, wide)
1 つのスキップグラムのネガティブサンプリング
skipgrams 関数は、指定されたウィンドウスパンをスライドすることにより、すべてのポジティブのスキップグラムのペアを返します。トレーニング用のネガティブサンプルとして機能する追加のスキップグラムのペアを生成するには、語彙からランダムな単語をサンプリングする必要があります。tf.random.log_uniform_candidate_sampler 関数を使用して、ウィンドウ内の特定のターゲットワードに対して num_ns のネガティブサンプルをサンプリングします。1 つのスキップグラムのターゲットワードで関数を呼び出し、コンテキストワードを真のクラスとして渡して、サンプリングから除外できます。
重要点: [5, 20] 範囲の num_ns(ポジティブなコンテキストワードあたりのネガティブサンプルの数)は、小規模なデータセットで機能することが示されています。[2, 5] 範囲の num_ns は、より大きなデータセットの場合に十分です。
# Get target and context words for one positive skip-gram.
target_word, context_word = positive_skip_grams[0]
# Set the number of negative samples per positive context.
num_ns = 4
context_class = tf.reshape(tf.constant(context_word, dtype="int64"), (1, 1))
negative_sampling_candidates, _, _ = tf.random.log_uniform_candidate_sampler(
true_classes=context_class, # class that should be sampled as 'positive'
num_true=1, # each positive skip-gram has 1 positive context class
num_sampled=num_ns, # number of negative context words to sample
unique=True, # all the negative samples should be unique
range_max=vocab_size, # pick index of the samples from [0, vocab_size]
seed=SEED, # seed for reproducibility
name="negative_sampling" # name of this operation
)
print(negative_sampling_candidates)
print([inverse_vocab[index.numpy()] for index in negative_sampling_candidates])
tf.Tensor([2 1 4 3], shape=(4,), dtype=int64) ['wide', 'the', 'shimmered', 'road']
1 つのトレーニングサンプルを作成する
与えられたポジティブの (target_word, context_word) スキップグラムに対して、target_word のウィンドウ サイズの前後に現れない num_ns のネガティブサンプルのコンテキストワードもあります。1 のポジティブの context_word と num_ns のネガティブのコンテキストワードを 1 つのテンソルにバッチ処理します。これにより、ターゲットワードごとにポジティブのスキップグラム (1 とラベル付ける) とネガティブのサンプル (0 とラベル付ける) のセットが生成されます。
# Add a dimension so you can use concatenation (in the next step).
negative_sampling_candidates = tf.expand_dims(negative_sampling_candidates, 1)
# Concatenate a positive context word with negative sampled words.
context = tf.concat([context_class, negative_sampling_candidates], 0)
# Label the first context word as `1` (positive) followed by `num_ns` `0`s (negative).
label = tf.constant([1] + [0]*num_ns, dtype="int64")
# Reshape the target to shape `(1,)` and context and label to `(num_ns+1,)`.
target = tf.squeeze(target_word)
context = tf.squeeze(context)
label = tf.squeeze(label)
上記のスキップグラムの例から、ターゲットワードのコンテキストと対応するラベルを確認してください。
print(f"target_index : {target}")
print(f"target_word : {inverse_vocab[target_word]}")
print(f"context_indices : {context}")
print(f"context_words : {[inverse_vocab[c.numpy()] for c in context]}")
print(f"label : {label}")
target_index : 3 target_word : road context_indices : [5 2 1 4 3] context_words : ['in', 'wide', 'the', 'shimmered', 'road'] label : [1 0 0 0 0]
(target, context, label) テンソルのタプルは、スキップグラム ネガティブサンプリング word2vec モデルをトレーニングするための 1 つのトレーニングサンプルを構成します。ターゲットの形状は (1,) であるのに対し、コンテキストとラベルの形状は (1+num_ns,) であることに注意してください。
print("target :", target)
print("context :", context)
print("label :", label)
target : tf.Tensor(3, shape=(), dtype=int32) context : tf.Tensor([5 2 1 4 3], shape=(5,), dtype=int64) label : tf.Tensor([1 0 0 0 0], shape=(5,), dtype=int64)
まとめ
この図は、文からトレーニングサンプルを生成する手順をまとめたものです。

temperature と code という単語は、入力文の一部ではないことに注意してください。これらは、上の図で使用されている他の特定のインデックスと同様の語彙に属しています。
すべてのステップを 1 つの関数にコンパイルする
スキップグラム サンプリングテーブル
大規模なデータセットでは語彙が多くなり、ストップワードなどのより頻繁に使用される単語の数も多くなります。一般的に出現する単語 (the、is、on など) のサンプリングから得られたトレーニングサンプルは、モデルの学習に役立つ情報をあまり提供しません。Mikolov et al. は、埋め込みの品質を改善するための有用な方法として、頻繁に使用される単語のサブサンプリングを提案しています。
tf.keras.preprocessing.sequence.skipgrams 関数は、任意のトークンをサンプリングする確率をエンコードするためのサンプリングテーブル引数を受け入れます。tf.keras.preprocessing.sequence.make_sampling_table を使用して、単語頻度ランクに基づく確率的サンプリングテーブルを生成し、それを skipgrams 関数に渡します。vocab_size が 10 の場合のサンプリング確率を調べます。
sampling_table = tf.keras.preprocessing.sequence.make_sampling_table(size=10)
print(sampling_table)
[0.00315225 0.00315225 0.00547597 0.00741556 0.00912817 0.01068435 0.01212381 0.01347162 0.01474487 0.0159558 ]
sampling_table[i] は、データセットで i 番目に最も一般的な単語をサンプリングする確率を示します。この関数は、サンプリングの単語頻度の Zipf 分布を想定しています。
重要点: tf.random.log_uniform_candidate_sampler は、語彙頻度が対数一様 (Zipf の) 分布に従うことを既に想定しています。これらの分布加重サンプリングを使用すると、ネガティブのサンプリング目標をトレーニングするための単純な損失関数を使用して、Noise Contrastive Estimation (NCE) 損失を概算するのにも役立ちます。
トレーニングデータを生成する
上記のすべての手順を、任意のテキストデータセットから取得したベクトル化された文のリストに対して呼び出せる関数にコンパイルします。スキップグラムの単語ペアをサンプリングする前に、サンプリングテーブルが作成されることに注意してください。この関数は後のセクションで使用します。
# Generates skip-gram pairs with negative sampling for a list of sequences
# (int-encoded sentences) based on window size, number of negative samples
# and vocabulary size.
def generate_training_data(sequences, window_size, num_ns, vocab_size, seed):
# Elements of each training example are appended to these lists.
targets, contexts, labels = [], [], []
# Build the sampling table for `vocab_size` tokens.
sampling_table = tf.keras.preprocessing.sequence.make_sampling_table(vocab_size)
# Iterate over all sequences (sentences) in the dataset.
for sequence in tqdm.tqdm(sequences):
# Generate positive skip-gram pairs for a sequence (sentence).
positive_skip_grams, _ = tf.keras.preprocessing.sequence.skipgrams(
sequence,
vocabulary_size=vocab_size,
sampling_table=sampling_table,
window_size=window_size,
negative_samples=0)
# Iterate over each positive skip-gram pair to produce training examples
# with a positive context word and negative samples.
for target_word, context_word in positive_skip_grams:
context_class = tf.expand_dims(
tf.constant([context_word], dtype="int64"), 1)
negative_sampling_candidates, _, _ = tf.random.log_uniform_candidate_sampler(
true_classes=context_class,
num_true=1,
num_sampled=num_ns,
unique=True,
range_max=vocab_size,
seed=seed,
name="negative_sampling")
# Build context and label vectors (for one target word)
negative_sampling_candidates = tf.expand_dims(
negative_sampling_candidates, 1)
context = tf.concat([context_class, negative_sampling_candidates], 0)
label = tf.constant([1] + [0]*num_ns, dtype="int64")
# Append each element from the training example to global lists.
targets.append(target_word)
contexts.append(context)
labels.append(label)
return targets, contexts, labels
word2vec のトレーニングデータを準備する
スキップグラム ネガティブ サンプリング ベースの word2vec モデルで 1 つの文を処理する方法を理解することにより、より大きな文のリストからトレーニングサンプルを生成できます。
テキストコーパスをダウンロードする
このチュートリアルでは、シェイクスピア著作のテキストファイルを使用します。次の行を変更して、このコードを独自のデータで実行します。
path_to_file = tf.keras.utils.get_file('shakespeare.txt', 'https://storage.googleapis.com/download.tensorflow.org/data/shakespeare.txt')
Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/shakespeare.txt 1115394/1115394 [==============================] - 0s 0us/step
ファイルからテキストを読み取り、最初の数行を出力します。
with open(path_to_file) as f:
lines = f.read().splitlines()
for line in lines[:20]:
print(line)
First Citizen: Before we proceed any further, hear me speak. All: Speak, speak. First Citizen: You are all resolved rather to die than to famish? All: Resolved. resolved. First Citizen: First, you know Caius Marcius is chief enemy to the people. All: We know't, we know't. First Citizen: Let us kill him, and we'll have corn at our own price.
空でない行を使用して、次の手順として tf.data.TextLineDataset オブジェクトを作成します。
text_ds = tf.data.TextLineDataset(path_to_file).filter(lambda x: tf.cast(tf.strings.length(x), bool))
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/autograph/pyct/static_analysis/liveness.py:83: Analyzer.lamba_check (from tensorflow.python.autograph.pyct.static_analysis.liveness) is deprecated and will be removed after 2023-09-23. Instructions for updating: Lambda fuctions will be no more assumed to be used in the statement where they are used, or at least in the same block. https://github.com/tensorflow/tensorflow/issues/56089
コーパスから文をベクトル化する
TextVectorization レイヤーを使用して、コーパスから文をベクトル化します。このレイヤの使用について詳しくは、テキスト分類のチュートリアルを参照してください。上記の最初の数文から、テキストは大文字または小文字にする必要があり、句読点を削除する必要があることに注意してください。これを行うには、TextVectorization レイヤーで使用する custom_standardization function を定義します。
# Now, create a custom standardization function to lowercase the text and
# remove punctuation.
def custom_standardization(input_data):
lowercase = tf.strings.lower(input_data)
return tf.strings.regex_replace(lowercase,
'[%s]' % re.escape(string.punctuation), '')
# Define the vocabulary size and the number of words in a sequence.
vocab_size = 4096
sequence_length = 10
# Use the `TextVectorization` layer to normalize, split, and map strings to
# integers. Set the `output_sequence_length` length to pad all samples to the
# same length.
vectorize_layer = layers.TextVectorization(
standardize=custom_standardization,
max_tokens=vocab_size,
output_mode='int',
output_sequence_length=sequence_length)
テキストデータセットで TextVectorization.adapt を呼び出して語彙を作成します。
vectorize_layer.adapt(text_ds.batch(1024))
レイヤーの状態がテキストコーパスを表すように調整されると、TextVectorization.get_vocabulary を使用して語彙にアクセスできます。この関数は、頻度によって (降順で) 並べ替えられたすべての語彙トークンのリストを返します。
# Save the created vocabulary for reference.
inverse_vocab = vectorize_layer.get_vocabulary()
print(inverse_vocab[:20])
['', '[UNK]', 'the', 'and', 'to', 'i', 'of', 'you', 'my', 'a', 'that', 'in', 'is', 'not', 'for', 'with', 'me', 'it', 'be', 'your']
vectorize_layer を使用して、text_ds (tf.data.Dataset) 内の各要素のベクトルを生成できるようになりました。Dataset.batch、Dataset.prefetch、Dataset.map、Dataset.unbatch を適用します。
# Vectorize the data in text_ds.
text_vector_ds = text_ds.batch(1024).prefetch(AUTOTUNE).map(vectorize_layer).unbatch()
データセットから配列を取得する
これで、整数でエンコードされた文の tf.data.Dataset ができました。word2vec モデルをトレーニングするためのデータセットを準備するには、データセットを文ベクトル シーケンスのリストにフラット化します。この手順は、データセット内の各文を繰り返し処理してポジティブなサンプルとネガティブなサンプルを生成するために必要です。
注意: 前に定義した generate_training_data() は TensorFlow 以外の Python/NumPy 関数を使用するため、tf.data.Dataset.map で tf.py_function や tf.numpy_function を使用することもできます。
sequences = list(text_vector_ds.as_numpy_iterator())
print(len(sequences))
32777
sequences からいくつかのサンプルを調べます。
for seq in sequences[:5]:
print(f"{seq} => {[inverse_vocab[i] for i in seq]}")
[ 89 270 0 0 0 0 0 0 0 0] => ['first', 'citizen', '', '', '', '', '', '', '', ''] [138 36 982 144 673 125 16 106 0 0] => ['before', 'we', 'proceed', 'any', 'further', 'hear', 'me', 'speak', '', ''] [34 0 0 0 0 0 0 0 0 0] => ['all', '', '', '', '', '', '', '', '', ''] [106 106 0 0 0 0 0 0 0 0] => ['speak', 'speak', '', '', '', '', '', '', '', ''] [ 89 270 0 0 0 0 0 0 0 0] => ['first', 'citizen', '', '', '', '', '', '', '', '']
シーケンスからトレーニングサンプルを生成する
sequences は、int でエンコードされた文のリストになりました。前に定義した generate_training_data 関数を呼び出すだけで、word2vec モデルのトレーニングサンプルを生成できます。要約すると、関数は各シーケンスの各単語を反復処理して、ポジティブおよびネガティブなコンテキストワードを収集します。ターゲット、コンテキスト、およびラベルの長さは同じであり、トレーニングサンプルの総数を表す必要があります。
targets, contexts, labels = generate_training_data(
sequences=sequences,
window_size=2,
num_ns=4,
vocab_size=vocab_size,
seed=SEED)
targets = np.array(targets)
contexts = np.array(contexts)[:,:,0]
labels = np.array(labels)
print('\n')
print(f"targets.shape: {targets.shape}")
print(f"contexts.shape: {contexts.shape}")
print(f"labels.shape: {labels.shape}")
0%| | 0/32777 [00:00<?, ?it/s] 0%| | 76/32777 [00:00<00:43, 753.10it/s] 0%| | 152/32777 [00:00<00:48, 669.72it/s] 1%| | 226/32777 [00:00<00:46, 693.73it/s] 1%| | 296/32777 [00:00<00:47, 684.42it/s] 1%| | 400/32777 [00:00<00:40, 801.77it/s] 1%|▏ | 481/32777 [00:00<00:44, 731.42it/s] 2%|▏ | 567/32777 [00:00<00:42, 764.21it/s] 2%|▏ | 645/32777 [00:00<00:43, 740.84it/s] 2%|▏ | 722/32777 [00:00<00:43, 745.17it/s] 2%|▏ | 800/32777 [00:01<00:42, 753.00it/s] 3%|▎ | 876/32777 [00:01<00:45, 702.29it/s] 3%|▎ | 963/32777 [00:01<00:42, 748.36it/s] 3%|▎ | 1039/32777 [00:01<00:44, 706.19it/s] 3%|▎ | 1120/32777 [00:01<00:43, 734.28it/s] 4%|▎ | 1195/32777 [00:01<00:44, 713.25it/s] 4%|▍ | 1279/32777 [00:01<00:42, 746.05it/s] 4%|▍ | 1371/32777 [00:01<00:39, 792.57it/s] 4%|▍ | 1451/32777 [00:01<00:39, 789.94it/s] 5%|▍ | 1531/32777 [00:02<00:41, 752.26it/s] 5%|▍ | 1607/32777 [00:02<00:43, 721.89it/s] 5%|▌ | 1707/32777 [00:02<00:39, 792.76it/s] 5%|▌ | 1788/32777 [00:02<00:39, 793.29it/s] 6%|▌ | 1868/32777 [00:02<00:41, 740.98it/s] 6%|▌ | 1956/32777 [00:02<00:39, 778.20it/s] 6%|▌ | 2035/32777 [00:02<00:41, 745.67it/s] 7%|▋ | 2138/32777 [00:02<00:37, 822.41it/s] 7%|▋ | 2240/32777 [00:02<00:35, 865.87it/s] 7%|▋ | 2328/32777 [00:03<00:36, 829.45it/s] 7%|▋ | 2439/32777 [00:03<00:33, 904.52it/s] 8%|▊ | 2531/32777 [00:03<00:33, 895.22it/s] 8%|▊ | 2632/32777 [00:03<00:32, 917.17it/s] 8%|▊ | 2725/32777 [00:03<00:33, 905.87it/s] 9%|▊ | 2816/32777 [00:03<00:36, 817.54it/s] 9%|▉ | 2939/32777 [00:03<00:32, 928.32it/s] 9%|▉ | 3035/32777 [00:03<00:32, 921.15it/s] 10%|▉ | 3129/32777 [00:03<00:36, 813.96it/s] 10%|▉ | 3214/32777 [00:04<00:36, 815.28it/s] 10%|█ | 3298/32777 [00:04<00:38, 775.56it/s] 10%|█ | 3403/32777 [00:04<00:34, 844.25it/s] 11%|█ | 3490/32777 [00:04<00:35, 828.45it/s] 11%|█ | 3575/32777 [00:04<00:36, 790.89it/s] 11%|█ | 3656/32777 [00:04<00:37, 785.45it/s] 11%|█▏ | 3736/32777 [00:04<00:37, 779.12it/s] 12%|█▏ | 3832/32777 [00:04<00:35, 819.89it/s] 12%|█▏ | 3915/32777 [00:04<00:35, 817.62it/s] 12%|█▏ | 3998/32777 [00:05<00:36, 793.88it/s] 12%|█▏ | 4079/32777 [00:05<00:36, 797.03it/s] 13%|█▎ | 4159/32777 [00:05<00:36, 778.39it/s] 13%|█▎ | 4238/32777 [00:05<00:37, 764.22it/s] 13%|█▎ | 4315/32777 [00:05<00:42, 672.51it/s] 13%|█▎ | 4395/32777 [00:05<00:40, 705.32it/s] 14%|█▎ | 4468/32777 [00:05<00:43, 646.97it/s] 14%|█▍ | 4535/32777 [00:05<00:43, 646.03it/s] 14%|█▍ | 4606/32777 [00:05<00:42, 659.84it/s] 14%|█▍ | 4679/32777 [00:06<00:41, 676.23it/s] 15%|█▍ | 4753/32777 [00:06<00:40, 689.51it/s] 15%|█▍ | 4833/32777 [00:06<00:38, 718.15it/s] 15%|█▍ | 4906/32777 [00:06<00:43, 637.94it/s] 15%|█▌ | 4975/32777 [00:06<00:42, 649.74it/s] 15%|█▌ | 5050/32777 [00:06<00:41, 673.30it/s] 16%|█▌ | 5119/32777 [00:06<00:43, 635.06it/s] 16%|█▌ | 5191/32777 [00:06<00:42, 652.90it/s] 16%|█▌ | 5280/32777 [00:06<00:38, 713.99it/s] 16%|█▋ | 5373/32777 [00:07<00:35, 772.06it/s] 17%|█▋ | 5452/32777 [00:07<00:39, 686.74it/s] 17%|█▋ | 5523/32777 [00:07<00:41, 662.97it/s] 17%|█▋ | 5592/32777 [00:07<00:40, 667.83it/s] 17%|█▋ | 5660/32777 [00:07<00:40, 667.56it/s] 17%|█▋ | 5728/32777 [00:07<00:44, 612.93it/s] 18%|█▊ | 5792/32777 [00:07<00:43, 616.96it/s] 18%|█▊ | 5879/32777 [00:07<00:39, 682.96it/s] 18%|█▊ | 5949/32777 [00:07<00:42, 624.58it/s] 18%|█▊ | 6014/32777 [00:08<00:44, 596.48it/s] 19%|█▊ | 6110/32777 [00:08<00:38, 691.47it/s] 19%|█▉ | 6185/32777 [00:08<00:37, 704.77it/s] 19%|█▉ | 6273/32777 [00:08<00:35, 752.93it/s] 19%|█▉ | 6350/32777 [00:08<00:38, 684.15it/s] 20%|█▉ | 6421/32777 [00:08<00:40, 647.92it/s] 20%|█▉ | 6488/32777 [00:08<00:43, 599.65it/s] 20%|█▉ | 6552/32777 [00:08<00:43, 607.44it/s] 20%|██ | 6614/32777 [00:09<00:44, 592.91it/s] 20%|██ | 6675/32777 [00:09<00:43, 595.30it/s] 21%|██ | 6736/32777 [00:09<00:44, 585.80it/s] 21%|██ | 6816/32777 [00:09<00:40, 643.90it/s] 21%|██ | 6896/32777 [00:09<00:37, 682.86it/s] 21%|██▏ | 6970/32777 [00:09<00:36, 699.24it/s] 21%|██▏ | 7047/32777 [00:09<00:36, 713.68it/s] 22%|██▏ | 7119/32777 [00:09<00:38, 674.98it/s] 22%|██▏ | 7188/32777 [00:09<00:38, 673.30it/s] 22%|██▏ | 7271/32777 [00:09<00:35, 715.76it/s] 22%|██▏ | 7346/32777 [00:10<00:35, 722.83it/s] 23%|██▎ | 7419/32777 [00:10<00:39, 634.59it/s] 23%|██▎ | 7485/32777 [00:10<00:39, 632.75it/s] 23%|██▎ | 7556/32777 [00:10<00:38, 652.46it/s] 23%|██▎ | 7623/32777 [00:10<00:41, 613.23it/s] 23%|██▎ | 7686/32777 [00:10<00:44, 566.22it/s] 24%|██▎ | 7744/32777 [00:10<00:44, 561.53it/s] 24%|██▍ | 7802/32777 [00:10<00:46, 534.33it/s] 24%|██▍ | 7875/32777 [00:11<00:42, 585.57it/s] 24%|██▍ | 7951/32777 [00:11<00:39, 628.44it/s] 24%|██▍ | 8015/32777 [00:11<00:39, 625.35it/s] 25%|██▍ | 8111/32777 [00:11<00:34, 717.61it/s] 25%|██▌ | 8197/32777 [00:11<00:32, 757.25it/s] 25%|██▌ | 8274/32777 [00:11<00:33, 734.79it/s] 25%|██▌ | 8349/32777 [00:11<00:39, 618.59it/s] 26%|██▌ | 8415/32777 [00:11<00:41, 585.52it/s] 26%|██▌ | 8477/32777 [00:11<00:42, 568.65it/s] 26%|██▌ | 8538/32777 [00:12<00:41, 578.54it/s] 26%|██▌ | 8598/32777 [00:12<00:41, 575.94it/s] 26%|██▋ | 8674/32777 [00:12<00:38, 621.23it/s] 27%|██▋ | 8742/32777 [00:12<00:37, 634.76it/s] 27%|██▋ | 8816/32777 [00:12<00:36, 662.92it/s] 27%|██▋ | 8893/32777 [00:12<00:34, 690.25it/s] 27%|██▋ | 8983/32777 [00:12<00:32, 741.14it/s] 28%|██▊ | 9063/32777 [00:12<00:31, 751.64it/s] 28%|██▊ | 9139/32777 [00:12<00:36, 648.58it/s] 28%|██▊ | 9207/32777 [00:13<00:36, 653.12it/s] 28%|██▊ | 9275/32777 [00:13<00:37, 621.47it/s] 28%|██▊ | 9339/32777 [00:13<00:38, 606.09it/s] 29%|██▊ | 9401/32777 [00:13<00:39, 593.77it/s] 29%|██▉ | 9463/32777 [00:13<00:38, 598.21it/s] 29%|██▉ | 9524/32777 [00:13<00:39, 589.19it/s] 29%|██▉ | 9584/32777 [00:13<00:43, 532.87it/s] 29%|██▉ | 9639/32777 [00:13<00:45, 504.23it/s] 30%|██▉ | 9700/32777 [00:13<00:43, 527.23it/s] 30%|██▉ | 9754/32777 [00:14<00:46, 497.48it/s] 30%|██▉ | 9805/32777 [00:14<00:50, 458.98it/s] 30%|███ | 9869/32777 [00:14<00:45, 504.14it/s] 30%|███ | 9921/32777 [00:14<00:45, 504.20it/s] 30%|███ | 9983/32777 [00:14<00:42, 531.35it/s] 31%|███ | 10040/32777 [00:14<00:42, 536.51it/s] 31%|███ | 10109/32777 [00:14<00:39, 578.88it/s] 31%|███ | 10168/32777 [00:14<00:41, 547.10it/s] 31%|███ | 10224/32777 [00:14<00:41, 541.37it/s] 31%|███▏ | 10284/32777 [00:15<00:40, 556.60it/s] 32%|███▏ | 10341/32777 [00:15<00:41, 538.12it/s] 32%|███▏ | 10396/32777 [00:15<00:44, 501.25it/s] 32%|███▏ | 10458/32777 [00:15<00:42, 529.84it/s] 32%|███▏ | 10512/32777 [00:15<00:43, 511.91it/s] 32%|███▏ | 10584/32777 [00:15<00:39, 564.52it/s] 32%|███▏ | 10642/32777 [00:15<00:39, 556.67it/s] 33%|███▎ | 10699/32777 [00:15<00:40, 545.53it/s] 33%|███▎ | 10767/32777 [00:15<00:37, 582.18it/s] 33%|███▎ | 10843/32777 [00:16<00:35, 625.85it/s] 33%|███▎ | 10906/32777 [00:16<00:35, 608.92it/s] 33%|███▎ | 10978/32777 [00:16<00:34, 633.28it/s] 34%|███▎ | 11042/32777 [00:16<00:35, 615.99it/s] 34%|███▍ | 11104/32777 [00:16<00:38, 557.71it/s] 34%|███▍ | 11161/32777 [00:16<00:39, 545.06it/s] 34%|███▍ | 11222/32777 [00:16<00:38, 562.63it/s] 34%|███▍ | 11279/32777 [00:16<00:38, 555.76it/s] 35%|███▍ | 11336/32777 [00:16<00:40, 533.07it/s] 35%|███▍ | 11411/32777 [00:17<00:36, 585.08it/s] 35%|███▍ | 11471/32777 [00:17<00:39, 537.44it/s] 35%|███▌ | 11526/32777 [00:17<00:39, 535.31it/s] 35%|███▌ | 11599/32777 [00:17<00:36, 583.17it/s] 36%|███▌ | 11664/32777 [00:17<00:35, 598.47it/s] 36%|███▌ | 11725/32777 [00:17<00:35, 589.41it/s] 36%|███▌ | 11785/32777 [00:17<00:37, 564.20it/s] 36%|███▌ | 11842/32777 [00:17<00:38, 540.34it/s] 36%|███▋ | 11897/32777 [00:17<00:40, 513.24it/s] 36%|███▋ | 11949/32777 [00:18<00:41, 505.72it/s] 37%|███▋ | 12004/32777 [00:18<00:40, 514.58it/s] 37%|███▋ | 12062/32777 [00:18<00:38, 531.65it/s] 37%|███▋ | 12128/32777 [00:18<00:36, 564.55it/s] 37%|███▋ | 12185/32777 [00:18<00:36, 565.88it/s] 37%|███▋ | 12242/32777 [00:18<00:37, 543.38it/s] 38%|███▊ | 12303/32777 [00:18<00:37, 551.37it/s] 38%|███▊ | 12360/32777 [00:18<00:36, 555.08it/s] 38%|███▊ | 12438/32777 [00:18<00:32, 619.04it/s] 38%|███▊ | 12510/32777 [00:18<00:31, 647.49it/s] 38%|███▊ | 12576/32777 [00:19<00:36, 560.14it/s] 39%|███▊ | 12635/32777 [00:19<00:36, 550.12it/s] 39%|███▊ | 12694/32777 [00:19<00:35, 558.38it/s] 39%|███▉ | 12752/32777 [00:19<00:38, 521.01it/s] 39%|███▉ | 12806/32777 [00:19<00:38, 524.63it/s] 39%|███▉ | 12881/32777 [00:19<00:34, 577.28it/s] 40%|███▉ | 12980/32777 [00:19<00:28, 690.20it/s] 40%|███▉ | 13051/32777 [00:19<00:30, 644.87it/s] 40%|████ | 13136/32777 [00:20<00:28, 693.75it/s] 40%|████ | 13207/32777 [00:20<00:29, 662.81it/s] 41%|████ | 13275/32777 [00:20<00:29, 656.85it/s] 41%|████ | 13352/32777 [00:20<00:28, 683.21it/s] 41%|████ | 13421/32777 [00:20<00:30, 641.72it/s] 41%|████ | 13486/32777 [00:20<00:30, 629.38it/s] 41%|████▏ | 13559/32777 [00:20<00:29, 656.85it/s] 42%|████▏ | 13626/32777 [00:20<00:30, 626.86it/s] 42%|████▏ | 13690/32777 [00:20<00:30, 621.26it/s] 42%|████▏ | 13753/32777 [00:21<00:32, 589.18it/s] 42%|████▏ | 13843/32777 [00:21<00:28, 668.99it/s] 42%|████▏ | 13911/32777 [00:21<00:30, 617.59it/s] 43%|████▎ | 13994/32777 [00:21<00:27, 671.14it/s] 43%|████▎ | 14086/32777 [00:21<00:25, 738.25it/s] 43%|████▎ | 14162/32777 [00:21<00:25, 722.93it/s] 43%|████▎ | 14236/32777 [00:21<00:27, 676.47it/s] 44%|████▎ | 14309/32777 [00:21<00:26, 688.81it/s] 44%|████▍ | 14385/32777 [00:21<00:25, 707.89it/s] 44%|████▍ | 14468/32777 [00:21<00:24, 736.20it/s] 44%|████▍ | 14547/32777 [00:22<00:24, 751.15it/s] 45%|████▍ | 14623/32777 [00:22<00:27, 669.81it/s] 45%|████▍ | 14692/32777 [00:22<00:30, 593.58it/s] 45%|████▌ | 14766/32777 [00:22<00:28, 626.83it/s] 45%|████▌ | 14855/32777 [00:22<00:25, 695.07it/s] 46%|████▌ | 14928/32777 [00:22<00:28, 636.86it/s] 46%|████▌ | 14995/32777 [00:22<00:30, 586.78it/s] 46%|████▌ | 15056/32777 [00:22<00:31, 558.61it/s] 46%|████▌ | 15114/32777 [00:23<00:33, 532.77it/s] 46%|████▋ | 15173/32777 [00:23<00:32, 546.79it/s] 47%|████▋ | 15242/32777 [00:23<00:30, 582.77it/s] 47%|████▋ | 15302/32777 [00:23<00:31, 550.57it/s] 47%|████▋ | 15367/32777 [00:23<00:30, 572.39it/s] 47%|████▋ | 15426/32777 [00:23<00:30, 565.16it/s] 47%|████▋ | 15503/32777 [00:23<00:27, 620.89it/s] 48%|████▊ | 15571/32777 [00:23<00:27, 636.36it/s] 48%|████▊ | 15640/32777 [00:23<00:26, 648.28it/s] 48%|████▊ | 15714/32777 [00:24<00:25, 671.46it/s] 48%|████▊ | 15782/32777 [00:24<00:25, 670.99it/s] 48%|████▊ | 15850/32777 [00:24<00:28, 603.89it/s] 49%|████▊ | 15930/32777 [00:24<00:25, 650.52it/s] 49%|████▉ | 15997/32777 [00:24<00:27, 606.74it/s] 49%|████▉ | 16067/32777 [00:24<00:26, 630.62it/s] 49%|████▉ | 16132/32777 [00:24<00:26, 623.44it/s] 49%|████▉ | 16209/32777 [00:24<00:25, 661.80it/s] 50%|████▉ | 16285/32777 [00:24<00:23, 687.21it/s] 50%|████▉ | 16361/32777 [00:25<00:23, 706.57it/s] 50%|█████ | 16433/32777 [00:25<00:26, 616.34it/s] 50%|█████ | 16498/32777 [00:25<00:26, 612.10it/s] 51%|█████ | 16570/32777 [00:25<00:25, 634.76it/s] 51%|█████ | 16654/32777 [00:25<00:23, 689.67it/s] 51%|█████ | 16725/32777 [00:25<00:26, 604.82it/s] 51%|█████ | 16789/32777 [00:25<00:27, 591.30it/s] 51%|█████▏ | 16850/32777 [00:25<00:27, 570.02it/s] 52%|█████▏ | 16927/32777 [00:25<00:25, 622.41it/s] 52%|█████▏ | 16991/32777 [00:26<00:25, 618.00it/s] 52%|█████▏ | 17075/32777 [00:26<00:23, 675.87it/s] 52%|█████▏ | 17144/32777 [00:26<00:23, 669.85it/s] 53%|█████▎ | 17216/32777 [00:26<00:22, 679.49it/s] 53%|█████▎ | 17285/32777 [00:26<00:25, 616.55it/s] 53%|█████▎ | 17349/32777 [00:26<00:26, 592.43it/s] 53%|█████▎ | 17417/32777 [00:26<00:25, 609.72it/s] 53%|█████▎ | 17479/32777 [00:26<00:26, 569.44it/s] 54%|█████▎ | 17537/32777 [00:26<00:26, 570.25it/s] 54%|█████▎ | 17598/32777 [00:27<00:26, 579.57it/s] 54%|█████▍ | 17657/32777 [00:27<00:26, 569.93it/s] 54%|█████▍ | 17715/32777 [00:27<00:28, 521.41it/s] 54%|█████▍ | 17769/32777 [00:27<00:28, 522.90it/s] 54%|█████▍ | 17822/32777 [00:27<00:29, 501.17it/s] 55%|█████▍ | 17893/32777 [00:27<00:26, 553.84it/s] 55%|█████▍ | 17961/32777 [00:27<00:25, 585.83it/s] 55%|█████▌ | 18029/32777 [00:27<00:24, 611.66it/s] 55%|█████▌ | 18091/32777 [00:27<00:24, 598.37it/s] 55%|█████▌ | 18152/32777 [00:28<00:26, 543.57it/s] 56%|█████▌ | 18212/32777 [00:28<00:26, 555.20it/s] 56%|█████▌ | 18269/32777 [00:28<00:27, 527.06it/s] 56%|█████▌ | 18325/32777 [00:28<00:26, 535.75it/s] 56%|█████▌ | 18398/32777 [00:28<00:24, 587.60it/s] 56%|█████▋ | 18458/32777 [00:28<00:24, 578.46it/s] 56%|█████▋ | 18518/32777 [00:28<00:24, 572.50it/s] 57%|█████▋ | 18600/32777 [00:28<00:22, 640.11it/s] 57%|█████▋ | 18672/32777 [00:28<00:21, 660.84it/s] 57%|█████▋ | 18739/32777 [00:29<00:23, 598.13it/s] 57%|█████▋ | 18801/32777 [00:29<00:26, 537.51it/s] 58%|█████▊ | 18861/32777 [00:29<00:25, 551.02it/s] 58%|█████▊ | 18937/32777 [00:29<00:22, 603.13it/s] 58%|█████▊ | 18999/32777 [00:29<00:22, 600.04it/s] 58%|█████▊ | 19063/32777 [00:29<00:22, 607.77it/s] 58%|█████▊ | 19125/32777 [00:29<00:22, 603.46it/s] 59%|█████▊ | 19186/32777 [00:29<00:24, 565.72it/s] 59%|█████▉ | 19258/32777 [00:29<00:22, 604.60it/s] 59%|█████▉ | 19325/32777 [00:30<00:22, 601.81it/s] 59%|█████▉ | 19386/32777 [00:30<00:22, 587.08it/s] 59%|█████▉ | 19450/32777 [00:30<00:22, 594.88it/s] 60%|█████▉ | 19514/32777 [00:30<00:21, 606.55it/s] 60%|█████▉ | 19575/32777 [00:30<00:21, 604.54it/s] 60%|█████▉ | 19636/32777 [00:30<00:21, 597.79it/s] 60%|██████ | 19696/32777 [00:30<00:21, 597.77it/s] 60%|██████ | 19763/32777 [00:30<00:21, 618.47it/s] 60%|██████ | 19829/32777 [00:30<00:20, 621.55it/s] 61%|██████ | 19892/32777 [00:31<00:21, 612.81it/s] 61%|██████ | 19963/32777 [00:31<00:20, 629.67it/s] 61%|██████ | 20026/32777 [00:31<00:22, 569.82it/s] 61%|██████▏ | 20084/32777 [00:31<00:22, 568.31it/s] 61%|██████▏ | 20142/32777 [00:31<00:23, 532.54it/s] 62%|██████▏ | 20202/32777 [00:31<00:23, 545.02it/s] 62%|██████▏ | 20272/32777 [00:31<00:21, 585.78it/s] 62%|██████▏ | 20337/32777 [00:31<00:20, 598.93it/s] 62%|██████▏ | 20398/32777 [00:31<00:21, 584.70it/s] 62%|██████▏ | 20457/32777 [00:32<00:21, 575.16it/s] 63%|██████▎ | 20515/32777 [00:32<00:21, 569.57it/s] 63%|██████▎ | 20585/32777 [00:32<00:20, 603.40it/s] 63%|██████▎ | 20680/32777 [00:32<00:17, 699.40it/s] 63%|██████▎ | 20759/32777 [00:32<00:16, 716.90it/s] 64%|██████▎ | 20831/32777 [00:32<00:17, 697.13it/s] 64%|██████▍ | 20918/32777 [00:32<00:16, 739.35it/s] 64%|██████▍ | 20995/32777 [00:32<00:16, 730.52it/s] 64%|██████▍ | 21095/32777 [00:32<00:14, 803.89it/s] 65%|██████▍ | 21176/32777 [00:32<00:15, 764.34it/s] 65%|██████▍ | 21254/32777 [00:33<00:15, 737.45it/s] 65%|██████▌ | 21329/32777 [00:33<00:16, 693.69it/s] 65%|██████▌ | 21402/32777 [00:33<00:16, 697.34it/s] 66%|██████▌ | 21491/32777 [00:33<00:15, 749.15it/s] 66%|██████▌ | 21567/32777 [00:33<00:15, 711.79it/s] 66%|██████▌ | 21665/32777 [00:33<00:14, 779.20it/s] 66%|██████▋ | 21744/32777 [00:33<00:15, 724.93it/s] 67%|██████▋ | 21818/32777 [00:33<00:15, 686.74it/s] 67%|██████▋ | 21888/32777 [00:34<00:20, 520.06it/s] 67%|██████▋ | 21956/32777 [00:34<00:19, 554.03it/s] 67%|██████▋ | 22017/32777 [00:34<00:19, 558.84it/s] 67%|██████▋ | 22089/32777 [00:34<00:17, 598.20it/s] 68%|██████▊ | 22153/32777 [00:34<00:17, 599.52it/s] 68%|██████▊ | 22224/32777 [00:34<00:16, 627.33it/s] 68%|██████▊ | 22294/32777 [00:34<00:16, 646.29it/s] 68%|██████▊ | 22361/32777 [00:34<00:16, 650.00it/s] 68%|██████▊ | 22428/32777 [00:34<00:16, 636.19it/s] 69%|██████▊ | 22493/32777 [00:35<00:16, 632.08it/s] 69%|██████▉ | 22567/32777 [00:35<00:15, 659.81it/s] 69%|██████▉ | 22634/32777 [00:35<00:15, 652.40it/s] 69%|██████▉ | 22700/32777 [00:35<00:16, 618.82it/s] 69%|██████▉ | 22771/32777 [00:35<00:15, 643.07it/s] 70%|██████▉ | 22853/32777 [00:35<00:14, 690.91it/s] 70%|██████▉ | 22923/32777 [00:35<00:14, 680.29it/s] 70%|███████ | 23007/32777 [00:35<00:13, 724.08it/s] 70%|███████ | 23080/32777 [00:35<00:14, 687.36it/s] 71%|███████ | 23179/32777 [00:36<00:12, 769.01it/s] 71%|███████ | 23257/32777 [00:36<00:12, 745.21it/s] 71%|███████ | 23333/32777 [00:36<00:13, 709.09it/s] 71%|███████▏ | 23405/32777 [00:36<00:13, 687.22it/s] 72%|███████▏ | 23475/32777 [00:36<00:13, 673.70it/s] 72%|███████▏ | 23551/32777 [00:36<00:13, 693.79it/s] 72%|███████▏ | 23621/32777 [00:36<00:13, 685.28it/s] 72%|███████▏ | 23690/32777 [00:36<00:13, 669.59it/s] 72%|███████▏ | 23758/32777 [00:36<00:13, 652.39it/s] 73%|███████▎ | 23849/32777 [00:36<00:12, 724.84it/s] 73%|███████▎ | 23925/32777 [00:37<00:12, 733.03it/s] 73%|███████▎ | 23999/32777 [00:37<00:12, 687.60it/s] 73%|███████▎ | 24079/32777 [00:37<00:12, 713.70it/s] 74%|███████▎ | 24152/32777 [00:37<00:13, 622.17it/s] 74%|███████▍ | 24217/32777 [00:37<00:14, 599.09it/s] 74%|███████▍ | 24306/32777 [00:37<00:12, 672.13it/s] 74%|███████▍ | 24376/32777 [00:37<00:12, 671.69it/s] 75%|███████▍ | 24450/32777 [00:37<00:12, 689.36it/s] 75%|███████▍ | 24521/32777 [00:38<00:12, 671.48it/s] 75%|███████▌ | 24590/32777 [00:38<00:12, 661.12it/s] 75%|███████▌ | 24685/32777 [00:38<00:11, 735.42it/s] 76%|███████▌ | 24785/32777 [00:38<00:09, 806.43it/s] 76%|███████▌ | 24867/32777 [00:38<00:10, 776.21it/s] 76%|███████▌ | 24946/32777 [00:38<00:10, 716.06it/s] 76%|███████▋ | 25042/32777 [00:38<00:09, 775.65it/s] 77%|███████▋ | 25121/32777 [00:38<00:10, 719.19it/s] 77%|███████▋ | 25227/32777 [00:38<00:09, 794.89it/s] 77%|███████▋ | 25309/32777 [00:39<00:09, 775.38it/s] 77%|███████▋ | 25394/32777 [00:39<00:09, 789.04it/s] 78%|███████▊ | 25486/32777 [00:39<00:08, 825.42it/s] 78%|███████▊ | 25581/32777 [00:39<00:08, 857.60it/s] 78%|███████▊ | 25668/32777 [00:39<00:08, 802.88it/s] 79%|███████▊ | 25750/32777 [00:39<00:09, 780.32it/s] 79%|███████▉ | 25847/32777 [00:39<00:08, 831.48it/s] 79%|███████▉ | 25932/32777 [00:39<00:08, 826.59it/s] 79%|███████▉ | 26016/32777 [00:39<00:08, 781.26it/s] 80%|███████▉ | 26098/32777 [00:40<00:08, 779.89it/s] 80%|███████▉ | 26193/32777 [00:40<00:07, 823.81it/s] 80%|████████ | 26277/32777 [00:40<00:08, 799.73it/s] 80%|████████ | 26358/32777 [00:40<00:08, 757.81it/s] 81%|████████ | 26439/32777 [00:40<00:08, 767.94it/s] 81%|████████ | 26531/32777 [00:40<00:07, 809.55it/s] 81%|████████ | 26613/32777 [00:40<00:07, 811.01it/s] 81%|████████▏ | 26695/32777 [00:40<00:07, 783.41it/s] 82%|████████▏ | 26774/32777 [00:40<00:07, 751.29it/s] 82%|████████▏ | 26852/32777 [00:40<00:07, 752.27it/s] 82%|████████▏ | 26928/32777 [00:41<00:08, 713.03it/s] 82%|████████▏ | 27025/32777 [00:41<00:07, 778.57it/s] 83%|████████▎ | 27104/32777 [00:41<00:07, 724.00it/s] 83%|████████▎ | 27178/32777 [00:41<00:07, 702.21it/s] 83%|████████▎ | 27251/32777 [00:41<00:07, 707.34it/s] 83%|████████▎ | 27323/32777 [00:41<00:07, 694.54it/s] 84%|████████▎ | 27401/32777 [00:41<00:07, 707.17it/s] 84%|████████▍ | 27473/32777 [00:41<00:07, 705.59it/s] 84%|████████▍ | 27586/32777 [00:41<00:06, 808.30it/s] 84%|████████▍ | 27667/32777 [00:42<00:06, 731.46it/s] 85%|████████▍ | 27765/32777 [00:42<00:06, 794.08it/s] 85%|████████▍ | 27854/32777 [00:42<00:06, 817.59it/s] 85%|████████▌ | 27944/32777 [00:42<00:05, 835.37it/s] 86%|████████▌ | 28029/32777 [00:42<00:05, 828.15it/s] 86%|████████▌ | 28113/32777 [00:42<00:05, 812.28it/s] 86%|████████▌ | 28195/32777 [00:42<00:06, 744.64it/s] 86%|████████▋ | 28271/32777 [00:42<00:06, 718.00it/s] 86%|████████▋ | 28344/32777 [00:43<00:06, 639.78it/s] 87%|████████▋ | 28410/32777 [00:43<00:07, 575.72it/s] 87%|████████▋ | 28501/32777 [00:43<00:06, 654.31it/s] 87%|████████▋ | 28570/32777 [00:43<00:06, 612.57it/s] 87%|████████▋ | 28661/32777 [00:43<00:05, 687.16it/s] 88%|████████▊ | 28733/32777 [00:43<00:06, 659.51it/s] 88%|████████▊ | 28802/32777 [00:43<00:05, 664.81it/s] 88%|████████▊ | 28876/32777 [00:43<00:05, 679.99it/s] 88%|████████▊ | 28946/32777 [00:43<00:05, 647.58it/s] 89%|████████▊ | 29013/32777 [00:44<00:05, 652.89it/s] 89%|████████▊ | 29083/32777 [00:44<00:05, 666.12it/s] 89%|████████▉ | 29170/32777 [00:44<00:04, 722.71it/s] 89%|████████▉ | 29258/32777 [00:44<00:04, 766.30it/s] 90%|████████▉ | 29336/32777 [00:44<00:04, 724.66it/s] 90%|████████▉ | 29410/32777 [00:44<00:04, 705.29it/s] 90%|█████████ | 29508/32777 [00:44<00:04, 781.21it/s] 90%|█████████ | 29588/32777 [00:44<00:04, 760.42it/s] 91%|█████████ | 29665/32777 [00:44<00:04, 741.31it/s] 91%|█████████ | 29740/32777 [00:45<00:04, 738.53it/s] 91%|█████████ | 29815/32777 [00:45<00:04, 707.40it/s] 91%|█████████▏| 29915/32777 [00:45<00:03, 775.52it/s] 92%|█████████▏| 30018/32777 [00:45<00:03, 847.18it/s] 92%|█████████▏| 30104/32777 [00:45<00:03, 754.08it/s] 92%|█████████▏| 30184/32777 [00:45<00:03, 763.36it/s] 92%|█████████▏| 30263/32777 [00:45<00:03, 736.58it/s] 93%|█████████▎| 30346/32777 [00:45<00:03, 751.20it/s] 93%|█████████▎| 30423/32777 [00:45<00:03, 735.27it/s] 93%|█████████▎| 30498/32777 [00:46<00:03, 697.62it/s] 93%|█████████▎| 30583/32777 [00:46<00:02, 735.46it/s] 94%|█████████▎| 30658/32777 [00:46<00:03, 688.66it/s] 94%|█████████▎| 30728/32777 [00:46<00:02, 683.75it/s] 94%|█████████▍| 30802/32777 [00:46<00:02, 696.97it/s] 94%|█████████▍| 30873/32777 [00:46<00:02, 691.87it/s] 94%|█████████▍| 30960/32777 [00:46<00:02, 742.38it/s] 95%|█████████▍| 31037/32777 [00:46<00:02, 744.77it/s] 95%|█████████▍| 31112/32777 [00:46<00:02, 681.35it/s] 95%|█████████▌| 31208/32777 [00:47<00:02, 753.50it/s] 95%|█████████▌| 31285/32777 [00:47<00:01, 756.22it/s] 96%|█████████▌| 31363/32777 [00:47<00:01, 759.70it/s] 96%|█████████▌| 31441/32777 [00:47<00:01, 760.96it/s] 96%|█████████▋| 31553/32777 [00:47<00:01, 859.81it/s] 97%|█████████▋| 31640/32777 [00:47<00:01, 731.14it/s] 97%|█████████▋| 31717/32777 [00:47<00:01, 719.79it/s] 97%|█████████▋| 31792/32777 [00:47<00:01, 727.11it/s] 97%|█████████▋| 31867/32777 [00:47<00:01, 673.50it/s] 97%|█████████▋| 31950/32777 [00:48<00:01, 711.01it/s] 98%|█████████▊| 32023/32777 [00:48<00:01, 703.85it/s] 98%|█████████▊| 32095/32777 [00:48<00:00, 705.98it/s] 98%|█████████▊| 32169/32777 [00:48<00:00, 709.01it/s] 98%|█████████▊| 32241/32777 [00:48<00:00, 658.83it/s] 99%|█████████▊| 32308/32777 [00:48<00:00, 648.83it/s] 99%|█████████▉| 32376/32777 [00:48<00:00, 645.71it/s] 99%|█████████▉| 32500/32777 [00:48<00:00, 806.42it/s] 99%|█████████▉| 32608/32777 [00:48<00:00, 876.92it/s] 100%|█████████▉| 32697/32777 [00:48<00:00, 857.79it/s] 100%|██████████| 32777/32777 [00:49<00:00, 667.71it/s] targets.shape: (65425,) contexts.shape: (65425, 5) labels.shape: (65425, 5)
データセットを構成してパフォーマンスを改善する
潜在的に多数のトレーニングサンプルに対して効率的なバッチ処理を実行するには、tf.data.Dataset API を使用します。このステップの後、word2vec モデルをトレーニングするための (target_word, context_word), (label) 要素の tf.data.Dataset オブジェクトが作成されます。
BATCH_SIZE = 1024
BUFFER_SIZE = 10000
dataset = tf.data.Dataset.from_tensor_slices(((targets, contexts), labels))
dataset = dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE, drop_remainder=True)
print(dataset)
<BatchDataset element_spec=((TensorSpec(shape=(1024,), dtype=tf.int64, name=None), TensorSpec(shape=(1024, 5), dtype=tf.int64, name=None)), TensorSpec(shape=(1024, 5), dtype=tf.int64, name=None))>
Dataset.cache と Dataset.prefetch を適用してパフォーマンスを向上させます。
dataset = dataset.cache().prefetch(buffer_size=AUTOTUNE)
print(dataset)
<PrefetchDataset element_spec=((TensorSpec(shape=(1024,), dtype=tf.int64, name=None), TensorSpec(shape=(1024, 5), dtype=tf.int64, name=None)), TensorSpec(shape=(1024, 5), dtype=tf.int64, name=None))>
モデルとトレーニング
word2vec モデルは、スキップグラムからの真のコンテキストワードと、ネガティブサンプリングによって取得された偽のコンテキストワードを区別する分類器として実装できます。ターゲットワードとコンテキストワードの埋め込みの間で内積乗算を実行して、ラベルの予測を取得し、データセット内の真のラベルに対する損失関数を計算できます。
サブクラス化された word2vec モデル
Keras Subclassing API を使用して、次のレイヤーで word2vec モデルを定義します。
target_embedding:tf.keras.layers.Embeddingレイヤーは単語がターゲットワードとして表示されたときにその単語の埋め込みを検索します。このレイヤーのパラメータ数は(vocab_size * embedded_dim)です。context_embedding: これはもう一つのtf.keras.layers.Embeddingレイヤーで単語がコンテキストワードとして表示されたときに、その単語の埋め込みを検索します。このレイヤーのパラメータ数は、target_embeddingのパラメータ数と同じです。つまり、(vocab_size * embedded_dim)です。dots: これはトレーニングペアからターゲットとコンテキストの埋め込みの内積を計算するtf.keras.layers.Dotレイヤーです。flatten:tf.keras.layers.Flattenレイヤーは、dotsレイヤーの結果をロジットにフラット化します。
サブクラス化されたモデルを使用すると、(target, context) ペアを受け入れる call() 関数を定義し、対応する埋め込みレイヤーに渡すことができる context_embedding の形状を変更して、target_embedding で内積を実行し、フラット化された結果を返します。
重要点: target_embedding レイヤーと context embedded レイヤーも共有できます。また、両方の埋め込みを連結して、最終的な word2vec 埋め込みとして使用することもできます。
class Word2Vec(tf.keras.Model):
def __init__(self, vocab_size, embedding_dim):
super(Word2Vec, self).__init__()
self.target_embedding = layers.Embedding(vocab_size,
embedding_dim,
input_length=1,
name="w2v_embedding")
self.context_embedding = layers.Embedding(vocab_size,
embedding_dim,
input_length=num_ns+1)
def call(self, pair):
target, context = pair
# target: (batch, dummy?) # The dummy axis doesn't exist in TF2.7+
# context: (batch, context)
if len(target.shape) == 2:
target = tf.squeeze(target, axis=1)
# target: (batch,)
word_emb = self.target_embedding(target)
# word_emb: (batch, embed)
context_emb = self.context_embedding(context)
# context_emb: (batch, context, embed)
dots = tf.einsum('be,bce->bc', word_emb, context_emb)
# dots: (batch, context)
return dots
損失関数の定義とモデルのコンパイル
簡単にするためには、ネガティブサンプリング損失の代わりに tf.keras.losses.CategoricalCrossEntropy を使用できます。独自のカスタム損失関数を記述する場合は、次のようにします。
def custom_loss(x_logit, y_true):
return tf.nn.sigmoid_cross_entropy_with_logits(logits=x_logit, labels=y_true)
モデルを構築します。128 の埋め込み次元で word2vec クラスをインスタンス化します (さまざまな値を試してみてください)。モデルを tf.keras.optimizers.Adam オプティマイザーでコンパイルします。
embedding_dim = 128
word2vec = Word2Vec(vocab_size, embedding_dim)
word2vec.compile(optimizer='adam',
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
また、TensorBoard のトレーニング統計をログに記録するコールバックを定義します。
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir="logs")
いくつかのエポックで、dataset でモデルをトレーニングします。
word2vec.fit(dataset, epochs=20, callbacks=[tensorboard_callback])
Epoch 1/20 63/63 [==============================] - 8s 109ms/step - loss: 1.6082 - accuracy: 0.2312 Epoch 2/20 63/63 [==============================] - 0s 3ms/step - loss: 1.5890 - accuracy: 0.5521 Epoch 3/20 63/63 [==============================] - 0s 3ms/step - loss: 1.5419 - accuracy: 0.6035 Epoch 4/20 63/63 [==============================] - 0s 3ms/step - loss: 1.4601 - accuracy: 0.5742 Epoch 5/20 63/63 [==============================] - 0s 3ms/step - loss: 1.3626 - accuracy: 0.5812 Epoch 6/20 63/63 [==============================] - 0s 3ms/step - loss: 1.2656 - accuracy: 0.6077 Epoch 7/20 63/63 [==============================] - 0s 3ms/step - loss: 1.1744 - accuracy: 0.6421 Epoch 8/20 63/63 [==============================] - 0s 3ms/step - loss: 1.0895 - accuracy: 0.6768 Epoch 9/20 63/63 [==============================] - 0s 3ms/step - loss: 1.0105 - accuracy: 0.7108 Epoch 10/20 63/63 [==============================] - 0s 3ms/step - loss: 0.9374 - accuracy: 0.7411 Epoch 11/20 63/63 [==============================] - 0s 3ms/step - loss: 0.8698 - accuracy: 0.7659 Epoch 12/20 63/63 [==============================] - 0s 3ms/step - loss: 0.8075 - accuracy: 0.7873 Epoch 13/20 63/63 [==============================] - 0s 3ms/step - loss: 0.7502 - accuracy: 0.8078 Epoch 14/20 63/63 [==============================] - 0s 3ms/step - loss: 0.6978 - accuracy: 0.8247 Epoch 15/20 63/63 [==============================] - 0s 3ms/step - loss: 0.6499 - accuracy: 0.8395 Epoch 16/20 63/63 [==============================] - 0s 3ms/step - loss: 0.6062 - accuracy: 0.8532 Epoch 17/20 63/63 [==============================] - 0s 3ms/step - loss: 0.5664 - accuracy: 0.8652 Epoch 18/20 63/63 [==============================] - 0s 3ms/step - loss: 0.5302 - accuracy: 0.8759 Epoch 19/20 63/63 [==============================] - 0s 3ms/step - loss: 0.4972 - accuracy: 0.8856 Epoch 20/20 63/63 [==============================] - 0s 3ms/step - loss: 0.4673 - accuracy: 0.8948 <keras.callbacks.History at 0x7f60f34a8310>
TensorBoard は、word2vec モデルの精度と損失を表示します。
#docs_infra: no_execute
%tensorboard --logdir logs

埋め込みのルックアップと分析
Model.get_layer と Layer.get_weights を使用して、モデルから重みを取得します。TextVectorization.get_vocabulary 関数は、1 行に 1 つのトークンでメタデータファイルを作成するための語彙を提供します。
weights = word2vec.get_layer('w2v_embedding').get_weights()[0]
vocab = vectorize_layer.get_vocabulary()
ベクトルとメタデータファイルを作成して保存します。
out_v = io.open('vectors.tsv', 'w', encoding='utf-8')
out_m = io.open('metadata.tsv', 'w', encoding='utf-8')
for index, word in enumerate(vocab):
if index == 0:
continue # skip 0, it's padding.
vec = weights[index]
out_v.write('\t'.join([str(x) for x in vec]) + "\n")
out_m.write(word + "\n")
out_v.close()
out_m.close()
vectors.tsv と metadata.tsv をダウンロードして、取得した埋め込みを埋め込みプロジェクタで分析します。
try:
from google.colab import files
files.download('vectors.tsv')
files.download('metadata.tsv')
except Exception:
pass
次のステップ
このチュートリアルでは、ゼロからネガティブサンプリングを使用してスキップグラムの word2vec モデルを実装し、取得した単語の埋め込みを視覚化する方法を実演しました。
単語ベクトルとその数学的表現についての詳細は、こちらを参照してください。
高度なテキスト処理についての詳細は、言語理解のための Transformer モデルチュートリアルを参照してください。
事前トレーニング済みの埋め込みモデルに興味がある場合は、TF-Hub CORD-19 Swivel Embeddings の探索や多言語ユニバーサルセンテンス エンコーダーも参照してください。
また、新しいデータセットでモデルをトレーニングすることもできます(TensorFlow データセットには多くのデータセットがあります)。
TensorFlow.org で表示
Google Colab で実行
GitHubでソースを表示
ノートブックをダウンロード