def transform_data_fasttext(data, vocab, idx_to_counts, cbow, ngram_buckets,<EOL>ngrams, batch_size, window_size,<EOL>frequent_token_subsampling=<NUM_LIT>, dtype='<STR_LIT>',<EOL>index_dtype='<STR_LIT>'):
if ngram_buckets <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>sum_counts = float(sum(idx_to_counts))<EOL>idx_to_pdiscard = [<EOL><NUM_LIT:1> - math.sqrt(frequent_token_subsampling / (count / sum_counts))<EOL>for count in idx_to_counts]<EOL>def subsample(shard):<EOL><INDENT>return [[<EOL>t for t, r in zip(sentence,<EOL>np.random.uniform(<NUM_LIT:0>, <NUM_LIT:1>, size=len(sentence)))<EOL>if r > idx_to_pdiscard[t]] for sentence in shard]<EOL><DEDENT>data = data.transform(subsample)<EOL>batchify = nlp.data.batchify.EmbeddingCenterContextBatchify(<EOL>batch_size=batch_size, window_size=window_size, cbow=cbow,<EOL>weight_dtype=dtype, index_dtype=index_dtype)<EOL>data = data.transform(batchify)<EOL>with print_time('<STR_LIT>'):<EOL><INDENT>subword_function = nlp.vocab.create_subword_function(<EOL>'<STR_LIT>', ngrams=ngrams, num_subwords=ngram_buckets)<EOL>idx_to_subwordidxs = list(subword_function(vocab.idx_to_token))<EOL>subwordidxs = np.concatenate(idx_to_subwordidxs)<EOL>subwordidxsptr = np.cumsum([<EOL>len(subwordidxs) for subwordidxs in idx_to_subwordidxs])<EOL>subwordidxsptr = np.concatenate([<EOL>np.zeros(<NUM_LIT:1>, dtype=np.int64), subwordidxsptr])<EOL>if cbow:<EOL><INDENT>subword_lookup = functools.partial(<EOL>cbow_lookup, subwordidxs=subwordidxs,<EOL>subwordidxsptr=subwordidxsptr, offset=len(vocab))<EOL><DEDENT>else:<EOL><INDENT>subword_lookup = functools.partial(<EOL>skipgram_lookup, subwordidxs=subwordidxs,<EOL>subwordidxsptr=subwordidxsptr, offset=len(vocab))<EOL><DEDENT>max_subwordidxs_len = max(len(s) for s in idx_to_subwordidxs)<EOL>if max_subwordidxs_len > <NUM_LIT>:<EOL><INDENT>warnings.warn(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(max_subwordidxs_len))<EOL><DEDENT><DEDENT>data = UnchainStream(data)<EOL>if cbow:<EOL><INDENT>batchify_fn = cbow_fasttext_batch<EOL><DEDENT>else:<EOL><INDENT>batchify_fn = skipgram_fasttext_batch<EOL><DEDENT>batchify_fn = functools.partial(<EOL>batchify_fn, num_tokens=len(vocab) + len(subword_function),<EOL>subword_lookup=subword_lookup, dtype=dtype, index_dtype=index_dtype)<EOL>return data, batchify_fn, subword_function<EOL>
Transform a DataStream of coded DataSets to a DataStream of batches.

    Parameters
    ----------
    data : gluonnlp.data.DataStream
        DataStream where each sample is a valid input to
        gluonnlp.data.EmbeddingCenterContextBatchify.
    vocab : gluonnlp.Vocab
        Vocabulary containing all tokens whose indices occur in data. For each
        token, it's associated subwords will be computed and used for
        constructing the batches. No subwords are used if ngram_buckets is 0.
    idx_to_counts : list of int
        List of integers such that idx_to_counts[idx] represents the count of
        vocab.idx_to_token[idx] in the underlying dataset. The count
        information is used to subsample frequent words in the dataset.
        Each token is independently dropped with probability 1 - sqrt(t /
        (count / sum_counts)) where t is the hyperparameter
        frequent_token_subsampling.
    cbow : boolean
        If True, batches for CBOW are returned.
    ngram_buckets : int
        Number of hash buckets to consider for the fastText
        nlp.vocab.NGramHashes subword function.
    ngrams : list of int
        For each integer n in the list, all ngrams of length n will be
        considered by the nlp.vocab.NGramHashes subword function.
    batch_size : int
        The returned data stream iterates over batches of batch_size.
    window_size : int
        The context window size for
        gluonnlp.data.EmbeddingCenterContextBatchify.
    frequent_token_subsampling : float
        Hyperparameter for subsampling. See idx_to_counts above for more
        information.
    dtype : str or np.dtype, default 'float32'
        Data type of data array.
    index_dtype : str or np.dtype, default 'int64'
        Data type of index arrays.

    Returns
    -------
    gluonnlp.data.DataStream
        Stream over batches. Each returned element is a list corresponding to
        the arguments for the forward pass of model.SG or model.CBOW
        respectively based on if cbow is False or True. If ngarm_buckets > 0,
        the returned sample will contain ngrams. Both model.SG or model.CBOW
        will handle them correctly as long as they are initialized with the
        subword_function returned as second argument by this function (see
        below).
    gluonnlp.vocab.NGramHashes
        The subword_function used for obtaining the subwords in the returned
        batches.