Need some good resources to learn how to make custom vocab

IneedMrmeeseeks: <p>Hi, torch friends.<br>
I am a novice in this nlp things. I want to start with making my own vocab using my data.<br>
The problem is about OOV(out of vocabulary). I really hard to find an answer.<br>
To concrete my question, I use an example. (I may be wrong)</p>
<div class="md-table">
<table>
<thead>
<tr>
<th>id</th>
<th>tweets</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>sentence 1</td>
</tr>
<tr>
<td>1</td>
<td>sentence 2</td>
</tr>
<tr>
<td>2</td>
<td>sentence 3</td>
</tr>
</tbody>
</table>
</div><p>Let’s say I’ve created a vocab with only sentence1 and 2.<br>
what if there are new words in sentence 3?<br>
Then, How can I update my prior vocab?</p>
<p>I would also appreciate it if let me know what tokenizer is best for a torch user and links for studying. (torchtext, gensim, nltk etc.)<br>
<img src="https://discuss.pytorch.org/images/emoji/apple/pray.png?v=12" title=":pray:" class="emoji only-emoji" alt=":pray:" loading="lazy" width="20" height="20"></p>

J_Johnson: <p>It would be unusual to tokenize entire sentences. Usually, tokens represent words, ngrams, characters or sentencepiece. Examples:</p>
<p><strong>Words</strong><br>
[The, brown, fox, jumps, over, the lazy, dog, .]</p>
<p><strong>Ngrams</strong><br>
[th, e, br, own, f, ox, j, ump, s, o, ver, th, e, la, zy, d, og, .]<br>
<em>Results may vary by text, and is determined algorithmically.</em></p>
<p><strong>Chars</strong><br>
[t, h, e, _, b, …]</p>
<p><strong>Sentencepiece</strong><br>
[The, brown fox, jumps over, the, lazy dog, .]</p>
<p>With that said, I prefer using words or ngrams.</p>
<p>A lot of people make use of pre-trained embeddings/tokenizers. However, if you go this route, just be careful which one you use because most are excessively large and uncurated. For example, GLoVe pretrained (put out by Google) has website links and spam gibberish as part of the encoding set.</p>
<p>I use <code>generate_sp_model()</code> from here:</p>
<p><a href="https://pytorch.org/text/stable/data_functional.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/text/stable/data_functional.html</a></p>
<p>That creates two files, one which is a <code>.vocab</code> file and has all of the vocab in it, and can be opened and read with utf-8 encoding. Then I use that with <code>build_vocab_from_iterator</code> here:</p>
<p><a href="https://pytorch.org/text/stable/vocab.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/text/stable/vocab.html</a></p>
<p>With word tokens, 20,000 words will usually cover 99.5% of all vocab and punctuation.</p>
<p>With ngrams, you can get more coverage with less.</p>

IneedMrmeeseeks: <p>Yes, what I meant was words token in the sentences!!<br>
Thx for the usecases and links!!<br>
<img src="https://discuss.pytorch.org/images/emoji/apple/+1.png?v=12" title=":+1:" class="emoji only-emoji" alt=":+1:" loading="lazy" width="20" height="20"></p>

vdw: <p>When creating a vocabulary from a corpus it’s quite common to add some “special” tokens to that vocabulary. Which special tokens are needed depends on the task, but for example Sequence-to-Sequence models need a special <code>&lt;SOS&gt;</code> (start of sequence) and <code>&lt;EOS&gt;</code> (end of sequence) token – it doesn’t matter what they are called, the tokens just have to be unique.</p>
<p>A very common special token would be something like <code>&lt;UNK&gt;</code> (unknown) to represent any word/token never seen before. This means that when you have a news sentence, and this sentence contains words not in your vocabulary, these unknown words get (internally) replaced by <code>&lt;UNK&gt;</code> which is in your vocabulary.</p>
<p>The problem is not that you cannot extend you vocabulary but a model will be trained on the initial vocabulary. And since, e.g., the embedding layer will depend on the size of the vocabulary, add new words would mean changing this layer.</p>

IneedMrmeeseeks: <p>Thx for the reply. <img src="https://discuss.pytorch.org/images/emoji/apple/+1.png?v=12" title=":+1:" class="emoji" alt=":+1:" loading="lazy" width="20" height="20"><br>
How about making the size of the vocab dynamic?<br>
Like nn.Embedding(len(vocab), embedding_size)</p>
<p>Okay, Let’s say I encounter <code>&lt;UNK&gt;</code> tokens. Next time I want to include those <code>&lt;UNK&gt;</code> tokens as known words.<br>
In other words, is there a way that I can add new words to the initial vocab?<br>
Even if perhaps I have to train embedding all over again</p>
<p>Thx! <img src="https://discuss.pytorch.org/images/emoji/apple/pray.png?v=12" title=":pray:" class="emoji" alt=":pray:" loading="lazy" width="20" height="20"></p>

J_Johnson: <p>The embedding layer is basically just a matrix which is num_tokens by num_embeddings. Suppose you give your embedding layer 100 embedding units, i.e. 100 dimensions on which each word can be assigned meaning(i.e. wet/dry, hot/cold, person/object, etc.). When you send in the number assigned for your word, let’s say the word “the” is 10, then the embedding layer just finds the 10th vector in the matrix and replaces the integer.</p>
<p>You can always append more vectors to your embedding layer. But your model will need to finetune on the new vocabulary.</p>