NLP is a specialized area in AI that exclusively deals with processing of Natural language including understanding, generating text such as individual characters, words, paragraphs, documents such as whole of Wikipedia.
Language is symbolic and discrete. Both characters and words are discrete symbols. The basic elements of written language are characters. Characters form words that in turn denote objects, concepts, events, actions, and ideas. For instance, words such as “hamburger” or “pizza” each evoke in us a certain mental representations, but they are also distinct symbols, whose meaning is external to them and left to be interpreted in our heads.
While in Computer Vision (CV) or Speech Processing, basic building blocks are continuous in nature, allowing, for exanple, to move from a colorful image to a gray-scale one using a simple mathematical operation, or to compare two different colors based on their inherent properties such as hue and intensity. This can not be done easily with words - there is no simple operation that will allow us to move from the word “red” to the word “pink” without using a large lookup table or dictionary.
Language is also compositional: letters form words, and words form phrases and sentences. In order to interpret a text, we thus need to work beyond the level of letters and words, and look at long sequences of words such as sentences, or even complete documents.
The combination of the above properties leads to data sparseness. The way in which words (discrete symbols) can be combined to form meanings is practically infinite. The number of possible valid sentences is tremendous: we could never hope to enumerate all of them.
Before understanding the embedding concept, it is key to understand various pre-dating concepts to represent the discrete language into continuous numerical vector that can also help in reflecting various linguistic properties of the text in form of feature vector:
Embeddings: Basic idea is that “words should be encoded into a low-dimensional and dense vector”
The embeddings (the vector representation of each core feature) are treated as parameters of the network and are trained like the other parameters of the function f.
Feed-forward networks assume a fixed dimensional input. This can easily accommodate the case of a feature-extraction function that extracts a fixed number of features: each feature is represented as a vector, and the vectors are concatenated. This
way, each region of the resulting input vector corresponds to a different feature.
But what if the features are variable? For e.g., suppose features are words themselves such as in a document classification task.
We thus need to represent an unbounded number of features using a fixed-size vector. One way of achieving this is through a so-called continuous bag of words (CBOW ) representation.
FineTuning of Pre-trained Embedding matrix for the task:
How these “pre-trained embeddings” are obtained?:
Here’s how it works:
Initialize the Embedding Layer: The weights of the embedding layer are initialized with the pre-trained word embeddings. This means that, initially, the embedded representation of a word is its pre-trained word vector.
Forward Propagation: During training, each word in the input data is represented as a one-hot vector. This one-hot vector is input to the embedding layer, which uses it to look up the corresponding embedded representation in the embedding matrix. This embedded representation is then passed through the rest of the network.
Backward Propagation and Weight Update: The network makes a prediction, and this is compared to the true output. The difference between the predicted and true output is calculated using a loss function. This loss is then back-propagated through the network to update the weights. The weights in the embedding layer (i.e., the word embeddings) are updated in this process.
Iterate: Steps 2 and 3 are repeated for each batch of data. Over multiple iterations (epochs), the model learns to adjust the weights of the embeddings along with the weights of the other layers in the network to minimize the loss function.
In the case of Word2Vec, the model learns word embeddings from scratch. It doesn’t start with pre-existing word embeddings. Instead, it initializes with random vectors and then adjusts these vectors to minimize the prediction error in its task (either predicting the context words given a target word for Skip-Gram, or predicting the target word given the context words for CBOW).
Pre-trained word embeddings can be very useful for a sentiment analysis task. Here’s a general approach:
Prepare your data: For sentiment analysis, you’ll typically have a dataset of text samples and their corresponding sentiment labels. First, you’ll need to preprocess your text data by tokenizing the text into words, handling punctuation, and so forth.
Convert words to vectors: Next, for each word in your text data, you’ll look up its corresponding vector in the pre-trained word embeddings. This will give you a sequence of vectors for each text sample.
Aggregate the vectors: Since your model will likely require a fixed-size input, you’ll need to convert your sequence of vectors into a single vector. A common way to do this is by averaging all the vectors together, but there are also other methods like using RNNs, LSTMs, or Transformers.
Train your model: Now that your text data is represented as fixed-size vectors, you can input these vectors into your model. You’ll train your model to predict the sentiment label given the input vector.
When you have an embedding layer as part of your model, the input to the model is typically the integer-encoded representation of your text data, not the one-hot encoded vectors.
Here’s how it works:
Tokenization: You first convert your text data into a list of tokens (words).
Integer Encoding: Each unique token (word) in your text data is assigned a unique integer. This process creates a dictionary that maps words to integers. You then use this dictionary to convert your list of tokens into a list of integers.
Input to Embedding Layer: These integer-encoded tokens are then fed into the embedding layer of the model. The embedding layer uses these integers to look up the embedding vector for each word.
So, in this setup, the one-hot encoding step is skipped, and words are directly mapped to their integer indices. These indices are used to look up the embeddings in the embedding layer.
Tokenization: The text is broken down into tokens (words or subwords, depending on the model).
Integer Encoding: Each token is mapped to a unique integer, similar to the process I described earlier.
Model Processing: The integer-encoded tokens are input to the pre-trained model, which processes them using layers of transformers (in the case of BERT or GPT) or a combination of convolutional neural networks (CNNs) and long short-term memory (LSTM) networks (in the case of ELMo).
Output: The output is a set of vectors, where each vector corresponds to a token in the input. These vectors are the contextual embeddings. They represent the meaning of each word in the context of the given sentence.
Sequence Processing: The sequence of embeddings is then processed by another part of your model. This could be a Recurrent Neural Network (RNN), a Transformer, or even a simpler model like a Convolutional Neural Network (CNN) or a Feed Forward Neural Network (FFNN). This part of the model is responsible for “understanding” the sequence of embeddings and making a prediction based on them.
Sentiment Prediction: The final layer of your model is a dense layer with one neuron for each sentiment category (e.g., positive, negative, neutral). This layer makes the final sentiment prediction based on the processed embeddings.