Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

Introduction
Latest   Machine Learning

Introduction

Last Updated on July 25, 2023 by Editorial Team

Author(s): Manish Nayak

Originally published on Towards AI.

Machine Learning

An Intuitive Introduction of Word2Vec by Building a Word2Vec From Scratch

Understanding Word2Vec, and it’s advantages

In this article, I will try to explain Word2Vec vector representation, an unsupervised model that learns word embedding from raw text and I will also try to provide a comparison between the classical approach One-hot encoding and Word2Vec.

One-Hot-Encoded Vector Representation

The classical approach of solving text-related problems is one-hot encode the word. This approach has multiple drawbacks.

  • If the dataset has ten thousands of unique words(vocabulary) then one-hot-encoded vector representation will have ten thousands of dimensions.
  • In a one-hot-encoded vector representation, the size of the vector will be the size of the vocabulary. most of the words in the vocabulary will not be present in each document. So one-hot-encoded vector representation is a vector with mostly empty (zeros).
  • It is inefficient in computation.
  • Similar words will not have similar vectors in a one-hot-encoded vector representation.

Let’s consider the following two sentences.

I like watching a movie.

I enjoy watching a movie.

In one-hot encode vector representation of the words looks as follows

One-shot encode vector representation

Intuitively we know that enjoy and like are kind of similar words. The Euclidean distance between movie and enjoy is the same as the Euclidean distance between enjoy and like. This is a major drawback.

Word2vec Vector Representation

Word2Vec is an approach that helps us to achieve similar vectors for similar words. Words that are related to each other are mapped to points that are closer to each other in a high dimensional space. Word2Vec approach has the following advantage.

  • Word2Vec builds on the fact that words that share similar contexts also share semantic meanings.
  • Word2vec model predicts a word by using its neighbors, by learning dense vectors called embedding.
  • Word2vec is also computationally efficient.
  • Word2vec is an unsupervised model that learns word embedding from raw text.
  • Word2vec is available in two flavors: the CBOW model and the skip-gram model.
  • Skip-gram: When surrounding words(also known as context words) are predicted using the input(target) word.
  • CBOW(Continuous Bag Of Words): When the target word is predicted using the surrounding words(context words).

Word2vec is a three-layer neural network, In which the first is the input layer and the last layers are the output layer. The middle layer builds a latent representation so the input words transformed into the output vector representation.

In the Word2vec vector representation of words, we can find interesting mathematical relationships between word vectors.

king β€” man = queen β€” woman

In our previous example of two sentences.

If we follow the CBOW approach and take the surrounding(context) words as input and try to predict the target word then the output will be as follow.

A vectored form of the input and output looks like below.

In the neural network for our current example, there will be three neurons in the hidden layer and output will have five neurons with softmax function so that it will give the probability of words.

Accompanied jupyter notebook for this post can be found on Github.

Conclusion

The drawback of the classical approach is, it does not consider the order in which the words occur in the sentence and context is lost. it assumes that words in the document are independent of one another. Vector representation in this classical approach leads to data sparsity. This drawback can be overcome by Word2vec vector representation.

I hope this article helped you to get an understanding of Word2vec and why we prefer Word2vec over one-hot-encoding. It will at least provides a good explanation and a high-level comparison between one-hot-encoding and Word2vec vector representation.

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.

Published via Towards AI

Feedback ↓