Fake Data with Google Back-Translator API!
Tags: machinelearning, backtranslation, smote, imbalancedata
Published: 2023-05-06 18:09:05

**Machine learning** requires techniques to address the challenges of working with terribly **imbalance** datasets. _**Data Augmentation**_ is a class of techniques you can use to generate fake data.

## SMOTE
One of the most popular ways to create fake data for multimodal datasets is the **[SMOTE](https://learn.microsoft.com/en-us/azure/machine-learning/component-reference/smote?view=azureml-api-2)** technique, which can be applied to numerical and categorical features. 
SMOTE technique is based on the KNN algorithm. You can read more about this technique here:
[![SMOTE technique visualization](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2fbh3me4c63h1gwuxz8t.png)](https://www.analyticsvidhya.com/blog/2020/10/overcoming-class-imbalance-using-smote-techniques/)

Although **SMOTE** technique can be a nice data generator for numerical and categorical features, when we apply it to the text data, it can be biased due to _duplicate text samples_. On the other hand, it can inject _noisy samples_ to the dataset.

In a real project, we were tackling with an _imbalance multimodal dataset_. The issues we were targeting to handle in this dataset were:

1. **Multimodality**: there where `numerical`, `categorical` and `text` features in this dataset. 
2. **Sever imbalance**: there were a terribly unequal proportion between the classes, e.g. 98 of Class1 and 2 percent of Class2. 
3. **Lack of data**: there were just about 80 samples of the minority class. 
4. **Non-English text**: the text feature was in Persian. It was important to generate a similar text data with an eye on keeping it more alike to the original one.

## Back-Translation Technique
As I described before, we were suffering from the lack of data for the minority class. Using the SMOTE technique, injected copied text samples to the dataset and did not improve the model. So, we need a more efficient technique. 
**[Filtered Back-Translator](https://www.kaggle.com/code/sajjadayobi360/filtered-back-translation)** was a great idea to handle this issue.
Google back-translator is a pretrained generative language model, which is in access calling it as an API. This model generates high-quality fake text data by translating text multiple times between the original and another language, thereby creating new text samples that are similar to the original. This picture shows the whole procedure in simple:

[![Back-Translation Procedure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dl10vs1krbiopv907rbo.png)](https://github.com/topics/back-translation)

Since the translations are performed by Google's translation engine, the generated text is of high quality and can be used for data augmentation and model testing. 

The Google back translator has several advantages:

- First, it requires no training data or model training, making it an easy-to-use API for generating new data. 
- Second, the high-quality generated text is due to the popularity of Google's search engine and its expertise in generating this model. 
- Finally, the generated text can be used for various applications, such as improving the performance of machine learning models.

_**To sum up...**_ 
Generating fake data is an important technique for addressing severe imbalances in datasets in machine learning. SMOTE is a popular approach, but it cannot be applied directly to text data. The Google back translator is an alternative approach that produces high-quality results and can be used to augment text data. By combining SMOTE and the Google back translator, it is possible to create fake data for multimodal datasets that include text data, resulting in improved machine learning model performance. 
We successfully used the Google back translator to generate more text data for a project with an imbalance of 98-2 in the class distribution, resulting in a 20% improvement in the F-score and a more reliable model.