text: var store = [{
          "title": "About this blog",
          "excerpt":"I am Austin, with a master’s degree in Computer Science and Information Engineer from the National Yunlin University of Science and Technology.   I am familiar with image classification and sound recognition, and I am good at PyTorch, PyTorch Lightning, and TensorFlow in the deep learning framework.   In this blog, I will introduce deep learning, LeetCode problem, or some programming problems, and keep the notes as my programming diary.   If you have any questions, please feel free to contact me by email.   Thank you!  ","categories": [],
          "tags": ["Introduction"],
          "url": "/About_this_blog/",
          "teaser": null
        },{
          "title": "Data Augmentation in NLP: Random Synonym Replacement",
          "excerpt":"Abstract  Hello everyone, my name is Austin.   Today I want to introduce one of the NLP data augmentation methods named random synonym replacement.   In human conversation or writing, we use different words to represent the same thing.     Therefore, this method is to use different words to express the same thing when simulating human daily conversation or writing.   In this method, the key point is to use the synonym to replace the random select word to prevent the neural network overfitting.   Ok! Let’s code it.   Step  There are 3 steps in this method.   In the first step, we need to randomly select a word and set a threshold about the similarity to prevent the synonym from mismatching.   In the second step, according to the word, we can find out the top 10 similar synonyms and use the threshold to remove the similar synonyms below the threshold.   In the third step, randomly select the synonym from the previous result to replace the source word.   Requirement  Please install packages by the following list.  pip install --upgrade gensim numpy   Code   import  #import from gensim.models.word2vec import Word2Vec import gensim.downloader as api import numpy as np import random import string   class  # class   class RandomSynonymReplacement:     def __init__(self, corpus: str, similarity_threshold: float) -&gt; None:         self.model = Word2Vec(api.load(corpus))  # create the model of Word2Vec         self.similarity_threshold = similarity_threshold    # set the threshold      def __call__(self, text: str) -&gt; str:         # Split the input text with spaces to get each word         # and check if the last character is a punctuation mark         if text[-1] in string.punctuation:             words = text[:-1].split(' ')         else:             words = text.split(' ')          # randomly select a word and replace it with a synonym         for word_index in random.sample(range(len(words)), len(words)):             word = words[word_index]             # turn the selected word to lower case             # and check it whether exist in the vocabulary of the Word2Vec model             if word.lower() in self.model.wv.key_to_index:                 # get similarity word by the model of Word2Vec                 # and put it to numpy array                 similarity_word = np.array(                     self.model.wv.most_similar(word.lower()))                 # get the similarity from similarity_word                 similarity = similarity_word[:, 1].astype(np.float)                 # get the index with similarity above the threshold                 similarity_index = np.where(                     similarity &gt;= self.similarity_threshold)[0]                 # check the length of similarity_index                 if len(similarity_index):                     # randomly select the synonym                     words[words.index(word)] = random.sample(                         list(similarity_word[similarity_index, 0]), 1)[0]                     # check if the last character is a punctuation mark                     if text[-1] in string.punctuation:                         return ' '.join(words)+text[-1]                     else:                         return ' '.join(words)         return text   call  if __name__ == '__main__':     # create a class of RandomSynonymReplacement     random_synonym_replacement = RandomSynonymReplacement(         corpus='text8', similarity_threshold=0.5)      # define a string     text = 'Hello, World!'      # check the result     print(text)     print(random_synonym_replacement(text=text))   result  Hello, World! Hello, europe!   full version  The full version of code is here: https://github.com/fastyangmh/toolkit/blob/main/Python/RandomSynonymReplacement.py   Conclusion  If you have any questions, please feel free to contact me by email.   Reference  What is Gensim?  NumPy  Data Augmentation in Natural Language Processing  NLP Data Augmentation 常見方法  ","categories": ["Deep learning"],
          "tags": ["NLP","Data Augmentation"],
          "url": "/deep%20learning/Data_Augmentation_in_NLP_Random_Synonym_Replacement/",
          "teaser": null
        },{
          "title": "自然語言處理的數據增強: 隨機同義詞替換",
          "excerpt":"引言  大家好，我叫Austin。   今天我要介紹在自然語言處理中一種名為隨機同義詞替換的數據增強方法。   在人類對話或書寫時，我們常使用不同的單詞表達同一事物。     因此，這種方法是在模擬人類日常對話或寫作時，用不同的詞來表達同一事物。   在這個方法中，關鍵是用同義詞代替隨機選擇的詞來防止神經網路過擬合。   好！讓我們來寫程式吧。   步驟  此方法有 3 個步驟。   第一步，我們需要隨機選擇一個單詞，並設置一個相似度的閾值，以防止取得不相似的同義詞。   第二步，根據選定的單詞找出前10個相似的同義詞並利用閾值去除閾值以下的相似同義詞。   第三步，從上一步的結果中隨機選擇同義詞替換選定的單詞。   要求  請按照以下列表安裝套件。  pip install --upgrade gensim numpy   程式碼   import  #import from gensim.models.word2vec import Word2Vec import gensim.downloader as api import numpy as np import random import string   class  # class   class RandomSynonymReplacement:     def __init__(self, corpus: str, similarity_threshold: float) -&gt; None:         self.model = Word2Vec(api.load(corpus))  # create the model of Word2Vec         self.similarity_threshold = similarity_threshold    # set the threshold      def __call__(self, text: str) -&gt; str:         # Split the input text with spaces to get each word         # and check if the last character is a punctuation mark         if text[-1] in string.punctuation:             words = text[:-1].split(' ')         else:             words = text.split(' ')          # randomly select a word and replace it with a synonym         for word_index in random.sample(range(len(words)), len(words)):             word = words[word_index]             # turn the selected word to lower case             # and check it whether exist in the vocabulary of the Word2Vec model             if word.lower() in self.model.wv.key_to_index:                 # get similarity word by the model of Word2Vec                 # and put it to numpy array                 similarity_word = np.array(                     self.model.wv.most_similar(word.lower()))                 # get the similarity from similarity_word                 similarity = similarity_word[:, 1].astype(np.float)                 # get the index with similarity above the threshold                 similarity_index = np.where(                     similarity &gt;= self.similarity_threshold)[0]                 # check the length of similarity_index                 if len(similarity_index):                     # randomly select the synonym                     words[words.index(word)] = random.sample(                         list(similarity_word[similarity_index, 0]), 1)[0]                     # check if the last character is a punctuation mark                     if text[-1] in string.punctuation:                         return ' '.join(words)+text[-1]                     else:                         return ' '.join(words)         return text   call  if __name__ == '__main__':     # create a class of RandomSynonymReplacement     random_synonym_replacement = RandomSynonymReplacement(         corpus='text8', similarity_threshold=0.5)      # define a string     text = 'Hello, World!'      # check the result     print(text)     print(random_synonym_replacement(text=text))   result  Hello, World! Hello, europe!   完整版本  完整版的程式碼在這裡: https://github.com/fastyangmh/toolkit/blob/main/Python/RandomSynonymReplacement.py   結論  如果您有任何問題，請隨時通過電子郵件與我聯繫。   參考連結  What is Gensim?  NumPy  Data Augmentation in Natural Language Processing  NLP Data Augmentation 常見方法  ","categories": ["深度學習"],
          "tags": ["自然語言處理","數據增強"],
          "url": "/%E6%B7%B1%E5%BA%A6%E5%AD%B8%E7%BF%92/%E8%87%AA%E7%84%B6%E8%AA%9E%E8%A8%80%E8%99%95%E7%90%86%E7%9A%84%E6%95%B8%E6%93%9A%E5%A2%9E%E5%BC%B7_%E9%9A%A8%E6%A9%9F%E5%90%8C%E7%BE%A9%E8%A9%9E%E6%9B%BF%E6%8F%9B/",
          "teaser": null
        },{
          "title": "關於這個部落格",
          "excerpt":"我叫Austin，畢業於國立雲林科技大學資訊工程系碩士。   熟悉影像分類和聲音識別，擅長深度學習框架中的PyTorch、PyTorch Lightning、TensorFlow。   在這部落格中，我將介紹深度學習、LeetCode問題，或者一些程式問題，並放上我的程式日記。   如果您有任何問題，請隨時透過電子郵件與我聯繫。   謝謝！  ","categories": [],
          "tags": ["介紹"],
          "url": "/%E9%97%9C%E6%96%BC%E9%80%99%E5%80%8B%E9%83%A8%E8%90%BD%E6%A0%BC/",
          "teaser": null
        }]
  