Tweets, Emoji Sentiment Analysis using Q Blocks | by Michelangiolo Mazzeschi | Towards Data Science

In the following article, I will rent the Q Blocks GPU to run a computational demanding AI model. I will run a deep learning model called DeepMoji that, given a sentence, will estimate the top n emotions that can describe it (click not the link to have a try). The platform I will be renting the GPU from is still in early access, so you can try it (this platform gives you 20 Hours of GPU for FREE, an excellent start) out even before making a purchase and see if you are comfortable with it.
To proceed with this experiment, I wanted to rent GPU from an alternative provider rather than Amazon AWS, I have been told it is expensive., and it only becomes convenient if you run gigantic models.
So far, I found a good offer trying Q Blocks GPU. GPU is bought not by usage, but by time. The offer cost as little as .05$ per hour. Is a very reasonable price, you would be essentially paying a GPU at the same cost of a CPU. The distributed computing technology used by Q Blocks allows this provider to maintain prices down without diminishing quality.
If you are a beginner, definitely not. Common online compilers such as Google Colab or Kaggle offer a FREE (but not powerful) computational power. If you are working on small datasets as every beginner does, it does not require high computational power, and you can use FREE versions.
Instead, if you need to tune your model (like every professional need to) you will have to wait countless hours in front of the PC waiting for the results that will likely disappoint you. The step forward to build more challenging models is increasing your computational power.
For example, running this algorithm without using a GPU would have taken me several hours to complete. My experiment consists in reaching the completion of the project faster.
I will use the screenshots for this service to facilitate things. However, I am assuming every provider has different offers depending on your needs. Because I won’t need excessive computational power I can start using 100 Blocks per hour, the equivalent of .10$ per hour.
If this is your first time renting GPU, you might find it frustrating. When you initiate the model, it will start consuming. Therefore, it is of vital importance that you know what you are doing and you do not experiment in the notebook provided by the service. If there are bugs and you need to debug your code inside a paid workbook, your time will cost money. At the point when you have realized how much money that debugging time costed you, you will likely move to another GPU provider.
***Make sure you have a copy of your working model inside another workbook. You can simply import that copy and run it without wasting precious time.
Q Blocks has a connection to an online Jupyter Notebook. There could be some issues if you start your models in Google Colab, for example. Accessing files or downloading libraries has a slightly different syntax, and you could waste time searching for the solutions online, while your notebook is running. Because I encountered similar issues in the making of this model, I will explain how to overcome the solutions in detail.
Therefore, before code your model in a FREE Jupyter Workbook, then, if it is working, import that model on the platform you are using (in my case Q Blocks) to save time.
The provider will likely start an empty notebook with the GPU loaded. We can start coding...
The first thing you want to do is import a dataset that you can access. Every notebook has a section where to upload documents, I will later create a connection to that document importing it with pandas.
Probably, the tough thing to do when attempting this experiment will be installing torchMoji. I will divide the installation into two parts. First, I will be installing the required libraries that will allow torchMoji to run:
!pip3 install torch==1.0.1 -f https://download.pytorch.org/whl/cpu/stable !git clone https://github.com/huggingface/torchMojiimport osos.chdir('torchMoji')!pip3 install -e .#if you restart the package, the notebook risks to crash on a loop#if you managed to be curious and to make it stuck, just clic on RunTime, Factory Reset Runtime#I did not restart and worked fine#se questo funziona, poi crasha in future linee di codice, anche se chiudiamo e riapriamo dovrebbe essere a posto per 12 ore
In the second step, I will download and install the weights that will allow the Neural Network to select the emoji based on any text.
***Be very careful, there is a bug (I do not know if I could classify it as such) in Jupyter Notebook that prevents you from inputting an answer such as yes or no. To solve this issue and prevent your GPU to iterate on a loop eating your available processing power, use the following line of code that should solve the issue on Jupyter Notebook.
#!python3 scripts/download_weights.py! yes | python3 scripts/download_weights.py
In a conventional notebook, you should be allowed to choose between the options yes and no. However, Jupyter Notebook does not offer you any possibility of inputting a choice. I have bypassed it using the code above, and immediately stating the yes choice.
I will now create a function that as an input will take one string of text and, depending on the number of emotions we want to extract, will output the corresponding emojis.
#si connette a DeepMoji per una request, non posso modificare i parametri, credo!python3 examples/text_emojize.py --text f" {Stay safe from the virus} "!pip3 install --upgrade numpy!pip install numpy==1.18!pip install scipy==1.1.0!pip install scikit-learn==0.21.3import numpy as npimport emoji, jsonfrom torchmoji.global_variables import PRETRAINED_PATH, VOCAB_PATHfrom torchmoji.sentence_tokenizer import SentenceTokenizerfrom torchmoji.model_def import torchmoji_emojis  EMOJIS = ":joy: :unamused: :weary: :sob: :heart_eyes: :pensive: :ok_hand: :blush: :heart: :smirk: :grin: :notes: :flushed: :100: :sleeping: :relieved: :relaxed: :raised_hands: :two_hearts: :expressionless: :sweat_smile: :pray: :confused: :kissing_heart: :heartbeat: :neutral_face: :information_desk_person: :disappointed: :see_no_evil: :tired_face: :v: :sunglasses: :rage: :thumbsup: :cry: :sleepy: :yum: :triumph: :hand: :mask: :clap: :eyes: :gun: :persevere: :smiling_imp: :sweat: :broken_heart: :yellow_heart: :musical_note: :speak_no_evil: :wink: :skull: :confounded: :smile: :stuck_out_tongue_winking_eye: :angry: :no_good: :muscle: :facepunch: :purple_heart: :sparkling_heart: :blue_heart: :grimacing: :sparkles:".split(' ')model = torchmoji_emojis(PRETRAINED_PATH)with open(VOCAB_PATH, 'r') as f:  vocabulary = json.load(f)st = SentenceTokenizer(vocabulary, 30)def deepmojify(sentence, top_n=5, return_emoji=True, return_prob=False):  #converte lista probabilità in emoticon più probabili  def top_elements(array, k):    ind = np.argpartition(array, -k)[-k:]    return ind[np.argsort(array[ind])][::-1]tokenized, _, _ = st.tokenize_sentences([sentence])  #print(tokenized)  #lista di probabilità  prob = model(tokenized)[0]  #se ci sono errori parte da qui: too many values to unpack (expected 2), non riesce a trovare prob  #trova le n emoticono più alte   emoji_ids = top_elements(prob, top_n)#converte questi numeri in emoticons  emojis = map(lambda x: EMOJIS[x], emoji_ids)    if return_emoji == False and return_prob == False:    return None  elif return_emoji == True and return_prob == False:    return emoji.emojize(f"{sentence} {' '.join(emojis)}", use_aliases=True)  elif return_emoji == True and return_prob == True:    return emoji.emojize(f"{sentence} {' '.join(emojis)}", use_aliases=True), prob  elif return_emoji == False and return_prob == True:    return probdeepmojify('ciao, come stai?', top_n=3, return_emoji=True, return_prob=False)
The output of the inputted string will be the following:
'ciao, come stai? 💓 💛 ❤'
I will now create a function that converts a list into a dataset with its estimated emoji in different columns. As you can see from the string above, the function concatenated the emoji with the input string, I will break them and place each one in a different column of the dataset.
def emoji_dataset(list1, n_emoji=3, only_prob=False):  emoji_list = [[x] for x in list1]for _ in range(len(list1)):    for n_emo in range(1, n_emoji+1):      print(_)      if only_prob == False:        emoji_list[_].append(deepmojify(list1[_], top_n=n_emoji, return_emoji=True, return_prob=False)[2*-n_emo+1])      else:        emoji_list[_].append(deepmojify(list1[_], top_n=1, return_emoji=False, return_prob=True))emoji_list = pd.DataFrame(emoji_list)  return emoji_listdf_ = emoji_dataset(list1, 3)df_
Now that I am all set, I can run the entire project. I will download the dataset, quickly preprocess it, and then initiate the algorithm.
import pandas as pdX = pd.read_csv(open('tweets.csv'))
If you haven’t noticed yet, I am using the function open inside read_csv. Without this function, the code will return an error. This behavior is peculiar to Jupyter Notebook.
X.pop('Unnamed: 0')X = pd.DataFrame(X)X.columns = ['tweets']Xdf = X.copy()df
As a final step, I will turn the list of 25,000 tweets into a list. I can use the list as an input for the main function.
list1 = df['tweets'].to_list()
I can finally start the model. As a result, I will have a list of 25,000 tweets with corresponding emotions.
list1 = list1[0:25000]df_.to_csv('25k_emotions.csv')df_
When your model is set, do not forget to delete the Notebook to save your remaining credits.
The model performed relatively faster. Compared with the GPU of the FREE cloud compilers can reach a speed 10x faster. Running this algorithm on Google Colab required a bit more than 1 hour, by comparison, took me approximately 10 minutes to reach the same result using Q Blocks. If you are going to rent more powerful GPUs, depending on your needs, this number will only increase.
Cloud Computing is soon going to be the new norm. With revolutionary innovations such as blockchains and pixel streaming technology, people won’t need to buy GPU hardware anymore. Providers such as Q Blocks, who use a peer to peer computing, contribute by making computing power more accessible, accelerating innovation.