基于tensorflow构建Skip-Gram模型笔记

2020-10-14  本文已影响0人  打杂算法工程师

背景

最近在学习词向量模型:Skip-Gram,其编程作个简要笔记。skip-gram的coding工作主要分为三部分:

数据预处理

image.png
其中细节的编码部分详细可见:https://github.com/NELSONZHAO/zhihu/blob/master/skip_gram/Skip-Gram-English-Corpus.ipynb
需要注意的有以下几点:
import numpy as np
def get_targets(words, idx, window_size=5)
    w_s = np.random.randint(1, window_size+1)
    if idx - w_s <0:
        targets = words[0:idx]+words[idx:(idx+w_s)]
    else:
        targets = words[(idx - w_s):idx]+words[(idx+1):(idx+w_s+1)]
    return set(targets)

def get_batches(words, batch_size, window_size):
    n_batches = len(words)
    for idx in range(0, len(words), batch_size):
        x, y = [], []
        batch = words[idx:(idx+batch_size)]
        for i in range(batch_size):
            _y = get_targets(batch, i, window_size)
            x.extend([batch[i]*len(_y)])
            y.extend(_y)
        yield x, y

模型搭建

上一篇下一篇

猜你喜欢

热点阅读