了解 word2vec (TensorFlow) 中的输入和标签
Understanding input and labels in word2vec (TensorFlow)
我正在尝试正确理解 tensorflow "Vector Representations of Words" 教程中的 batch_input
和 batch_labels
。
比如我的数据
1 1 1 1 1 1 1 1 5 251 371 371 1685 ...
...以
开头
skip_window = 2 # How many words to consider left and right.
num_skips = 1 # How many times to reuse an input to generate a label.
则生成的输入数组为:
bach_input = 1 1 1 1 1 1 5 251 371 ....
这是有道理的,从2(=window大小)之后开始,然后连续。标签:
batch_labels = 1 1 1 1 1 1 251 1 1685 371 589 ...
我不太理解这些标签。每个输入权应该有 4 个标签(window 大小 2,每边)。但是 batch_label
变量的长度相同。
来自 tensorflow 教程:
The skip-gram model takes two inputs. One is a batch full of integers
representing the source context words, the other is for the target
words.
根据教程,我将两个变量声明为:
batch = np.ndarray(shape=(batch_size), dtype=np.int32)
labels = np.ndarray(shape=(batch_size, 1), dtype=np.int32)
我应该如何解读 batch_labels
?
There are supposed to be 4 labels for each input right (window size 2, on each side). But the batch_label variable is the same length.
按键设置为num_skips = 1
。该值定义了每个单词生成的 (input, label)
个元组的数量。请参阅下面具有不同 num_skips
的示例(我的 data
序列似乎与您的不同,对此感到抱歉)。
示例 #1 - num_skips=4
batch, labels = generate_batch(batch_size=8, num_skips=4, skip_window=2)
它为每个单词生成4个标签,即使用整个上下文;由于 batch_size=8
在这批中只处理了 2 个词(12 和 6),其余的将进入下一批:
data = [5239, 3084, 12, 6, 195, 2, 3137, 46, 59, 156, 128, 742, 477, 10572, ...]
batch = [12 12 12 12 6 6 6 6]
labels = [[6 3084 5239 195 195 3084 12 2]]
示例 #2 - num_skips=2
batch, labels = generate_batch(batch_size=8, num_skips=2, skip_window=2)
在这里您会期望每个单词在 batch
序列中出现两次;这 2 个标签是从 4 个可能的词中随机抽取的:
data = [5239, 3084, 12, 6, 195, 2, 3137, 46, 59, 156, 128, 742, 477, 10572, ...]
batch = [ 12 12 6 6 195 195 2 2]
labels = [[ 195 3084 12 195 3137 12 46 195]]
示例 #3 - num_skips=1
batch, labels = generate_batch(batch_size=8, num_skips=1, skip_window=2)
最后,这个设置和你的设置一样,每个单词只生成一个标签;每个标签都是从 4 字上下文中随机抽取的:
data = [5239, 3084, 12, 6, 195, 2, 3137, 46, 59, 156, 128, 742, 477, 10572, ...]
batch = [ 12 6 195 2 3137 46 59 156]
labels = [[ 6 12 12 195 59 156 46 46]]
How should I interpret the batch_labels?
每个标签都是要从上下文中预测的中心词。但是生成的数据可能 不是全部 (context, center)
元组,这取决于生成器的设置。
还要注意 train_labels
张量是一维的。 Skip-Gram 训练模型从给定的中心词预测 任何 上下文词,而不是一次 所有 4 个上下文词。这解释了为什么所有训练对 (12, 6)
、(12, 3084)
、(12, 5239)
和 (12, 195)
都是有效的。
我正在尝试正确理解 tensorflow "Vector Representations of Words" 教程中的 batch_input
和 batch_labels
。
比如我的数据
1 1 1 1 1 1 1 1 5 251 371 371 1685 ...
...以
开头skip_window = 2 # How many words to consider left and right.
num_skips = 1 # How many times to reuse an input to generate a label.
则生成的输入数组为:
bach_input = 1 1 1 1 1 1 5 251 371 ....
这是有道理的,从2(=window大小)之后开始,然后连续。标签:
batch_labels = 1 1 1 1 1 1 251 1 1685 371 589 ...
我不太理解这些标签。每个输入权应该有 4 个标签(window 大小 2,每边)。但是 batch_label
变量的长度相同。
来自 tensorflow 教程:
The skip-gram model takes two inputs. One is a batch full of integers representing the source context words, the other is for the target words.
根据教程,我将两个变量声明为:
batch = np.ndarray(shape=(batch_size), dtype=np.int32)
labels = np.ndarray(shape=(batch_size, 1), dtype=np.int32)
我应该如何解读 batch_labels
?
There are supposed to be 4 labels for each input right (window size 2, on each side). But the batch_label variable is the same length.
按键设置为num_skips = 1
。该值定义了每个单词生成的 (input, label)
个元组的数量。请参阅下面具有不同 num_skips
的示例(我的 data
序列似乎与您的不同,对此感到抱歉)。
示例 #1 - num_skips=4
batch, labels = generate_batch(batch_size=8, num_skips=4, skip_window=2)
它为每个单词生成4个标签,即使用整个上下文;由于 batch_size=8
在这批中只处理了 2 个词(12 和 6),其余的将进入下一批:
data = [5239, 3084, 12, 6, 195, 2, 3137, 46, 59, 156, 128, 742, 477, 10572, ...]
batch = [12 12 12 12 6 6 6 6]
labels = [[6 3084 5239 195 195 3084 12 2]]
示例 #2 - num_skips=2
batch, labels = generate_batch(batch_size=8, num_skips=2, skip_window=2)
在这里您会期望每个单词在 batch
序列中出现两次;这 2 个标签是从 4 个可能的词中随机抽取的:
data = [5239, 3084, 12, 6, 195, 2, 3137, 46, 59, 156, 128, 742, 477, 10572, ...]
batch = [ 12 12 6 6 195 195 2 2]
labels = [[ 195 3084 12 195 3137 12 46 195]]
示例 #3 - num_skips=1
batch, labels = generate_batch(batch_size=8, num_skips=1, skip_window=2)
最后,这个设置和你的设置一样,每个单词只生成一个标签;每个标签都是从 4 字上下文中随机抽取的:
data = [5239, 3084, 12, 6, 195, 2, 3137, 46, 59, 156, 128, 742, 477, 10572, ...]
batch = [ 12 6 195 2 3137 46 59 156]
labels = [[ 6 12 12 195 59 156 46 46]]
How should I interpret the batch_labels?
每个标签都是要从上下文中预测的中心词。但是生成的数据可能 不是全部 (context, center)
元组,这取决于生成器的设置。
还要注意 train_labels
张量是一维的。 Skip-Gram 训练模型从给定的中心词预测 任何 上下文词,而不是一次 所有 4 个上下文词。这解释了为什么所有训练对 (12, 6)
、(12, 3084)
、(12, 5239)
和 (12, 195)
都是有效的。