<p>I tend to trust deployed code more than paper write-ups, especially in a case like word2vec, where the original authors' <a href="https://github.com/tmikolov/word2vec/blob/20c129af10659f7c50e86e3be406df663beff438/word2vec.c#L407" rel="nofollow noreferrer"><code>word2vec.c</code> code</a> released by the paper's authors has been widely used &amp; served as the template for other implementations. If we look at its subsampling mechanism...</p>

<pre class="lang-c prettyprint-override"><code>        if (sample &gt; 0) {
          real ran = (sqrt(vocab[word].cn / (sample * train_words)) + 1) * (sample * train_words) / vocab[word].cn;
          next_random = next_random * (unsigned long long)25214903917 + 11;
          if (ran &lt; (next_random &amp; 0xFFFF) / (real)65536) continue;
        }
</code></pre>

<p>...we see that those words with tiny counts (<code>.cn</code>) that could give negative values in the original formula instead here give values greater-than <code>1.0</code>, and thus can never be less than the <code>long</code>-random-masked-and-scaled to never be more than <code>1.0</code> (<code>(next_random &amp; 0xFFFF) / (real)65536</code>). So, it seems the authors' intent was for all negative-values of the original formula to mean "never discard". </p>

<p>As per the <em>keras</em> <code>make_sampling_table()</code> comment &amp; implementation, they're <strong>not</strong> consulting the actual word-frequencies at all. Instead, they're assuming a Zipf-like distribution based on word-rank order to synthesize a simulated word-frequency. </p>

<p>If their assumptions were to hold – the related words are from a natural-language corpus with a Zipf-like frequency-distribution – then I'd expect their sampling probabilities to be close to down-sampling probabilities that would have been calculated from true frequency information. And that's probably "close enough" for most purposes.</p>

<p>I'm not sure why they chose this approximation. Perhaps other aspects of their usual processes have not maintained true frequencies through to this step, and they're expecting to always be working with natural-language texts, where the assumed frequencies will be generally true. </p>

<p>(As luck would have it, and because people often want to impute frequencies to public sets of word-vectors which have dropped the true counts but are still sorted from most- to least-frequent, just a few days ago I wrote <a href="https://stackoverflow.com/a/58737377/130288">an answer about simulating a fake-but-plausible distribution using Zipf's law</a> – similar to what this keras code is doing.)</p>

<p>But, if you're working with data that <strong>doesn't</strong> match their assumptions (as with your synthetic or described datasets), their sampling-probabilities will be quite different than what you would calculate yourself, with any form of the original formula that uses true word frequencies. </p>

<p>In particular, imagine a distribution with one token a million times, then a hundred tokens all appearing just 10 times each. Those hundred tokens' order in the "rank" list is arbitrary – truly, they're all tied in frequency. But the simulation-based approach, by fitting a Zipfian distribution on that ordering, will in fact be sampling each of them very differently. The one 10-occurrence word lucky enough to be in the 2nd rank position will be far more downsampled, as if it were far more frequent. And the 1st-rank "tall head" value, by having its true frequency *under-*approximated, will be less down-sampled than otherwise. Neither of those effects seem beneficial, or in the spirit of the frequent-word-downsampling option - which should only "thin out" very-frequent words, and in all cases leave words of the same frequency as each other in the original corpus roughly equivalently present to each other in the down-sampled corpus. </p>

<p>So for your case, I would go with the original formula (probability-of-discarding-that-requires-special-handling-of-negative-values), or the <code>word2vec.c</code> practical/inverted implementation (probability-of-keeping-that-saturates-at-1.0), rather than the keras-style approximation. </p>

<p>(As a totally-separate note that nonetheless may be relevant for your dataset/purposes, if you're using negative-sampling: there's another parameter controlling the relative sampling of negative examples, often fixed at <code>0.75</code> in early implementations, that <a href="https://arxiv.org/abs/1804.04212" rel="nofollow noreferrer">one paper has suggested can usefully vary for non-natural-language token distributions &amp; recommendation-related end-uses</a>. This parameter is named <code>ns_exponent</code> in the Python <code>gensim</code> implementation, but simply <a href="https://github.com/tmikolov/word2vec/blob/20c129af10659f7c50e86e3be406df663beff438/word2vec.c#L55" rel="nofollow noreferrer">a fixed <code>power</code> value internal to a sampling-table pre-calculation in the original <code>word2vec.c</code> code</a>.)</p>
Word2Vec Subsampling -- Implementation
<p>I am implementing the <a href="https://arxiv.org/pdf/1310.4546.pdf" rel="nofollow noreferrer">Skipgram</a> model, both in Pytorch and Tensorflow2. I am having doubts about the implementation of subsampling of frequent words. Verbatim from the paper, the probability of subsampling word <code>wi</code> is computed as</p>

<p><a href="https://i.stack.imgur.com/Eq2u8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Eq2u8.png" alt="enter image description here"></a></p>

<p>where <code>t</code> is a custom threshold (usually, a small value such as <em>0.0001</em>) and <code>f</code> is the frequency of the word in the document. Although the authors implemented it in a different, but almost equivalent way, let's stick with this definition.</p>

<p>When computing the <code>P(wi)</code>, we can end up with negative values. For example, assume we have 100 words, and one of them appears extremely more often than others (as it is the case for my dataset).</p>

<pre class="lang-py prettyprint-override"><code>import numpy as np
import seaborn as sns

np.random.seed(12345)

# generate counts in [1, 20]
counts = np.random.randint(low=1, high=20, size=99)

# add an extremely bigger count
counts = np.insert(counts, 0, 100000)

# compute frequencies
f = counts/counts.sum()

# define threshold as in paper
t = 0.0001

# compute probabilities as in paper
probs = 1 - np.sqrt(t/f)
sns.distplot(probs);
</code></pre>

<p><strong>Q: What is the correct way to implement subsampling using this "probability"?</strong></p>

<p>As an additional info, I have seen that in <a href="https://keras.io/preprocessing/sequence/" rel="nofollow noreferrer">keras</a> the function <code>keras.preprocessing.sequence.make_sampling_table</code> takes a different approach: </p>

<pre class="lang-py prettyprint-override"><code>def make_sampling_table(size, sampling_factor=1e-5):
    """Generates a word rank-based probabilistic sampling table.
    Used for generating the `sampling_table` argument for `skipgrams`.
    `sampling_table[i]` is the probability of sampling
    the i-th most common word in a dataset
    (more common words should be sampled less frequently, for balance).
    The sampling probabilities are generated according
    to the sampling distribution used in word2vec:
    ```
    p(word) = (min(1, sqrt(word_frequency / sampling_factor) /
        (word_frequency / sampling_factor)))
    ```
    We assume that the word frequencies follow Zipf's law (s=1) to derive
    a numerical approximation of frequency(rank):
    `frequency(rank) ~ 1/(rank * (log(rank) + gamma) + 1/2 - 1/(12*rank))`
    where `gamma` is the Euler-Mascheroni constant.
    # Arguments
        size: Int, number of possible words to sample.
        sampling_factor: The sampling factor in the word2vec formula.
    # Returns
        A 1D Numpy array of length `size` where the ith entry
        is the probability that a word of rank i should be sampled.
    """
    gamma = 0.577
    rank = np.arange(size)
    rank[0] = 1
    inv_fq = rank * (np.log(rank) + gamma) + 0.5 - 1. / (12. * rank)
    f = sampling_factor * inv_fq

    return np.minimum(1., f / np.sqrt(f))
</code></pre>