Python Tutorial: Encoder decoder architecture

DataCamp · Beginner ·🔢 Mathematical Foundations ·6y ago

Key Takeaways

The video tutorial covers the encoder/decoder architecture in machine translation models, specifically in Python, using sequence-to-sequence models and one-hot encoding.

Full Transcript

now you will learn about the encoder/decoder architecture in more detail a machine translation model works by first consuming the words of the source language sequentially and then sequentially predicting the corresponding words in the target language however under the hood is actually two different models an encoder and a decoder let's first look at the encoder in our example the encoder takes in 100s of english words as inputs and produces a compressed representation of the inputs known as a context vector then the decoder consumes the context vector as an input and produces probabilistic predictions for each time step the word for a given time step is selected as the word with the highest probability note that though the inputs to the encoder are ones and zeros the decoder produces continuous probabilistic outputs these models are also called sequence to sequence models because they map a sequence that is an English sentence to another sequence that is a French sentence let's understand this - an analogy a teacher is explaining what an elephant is and you never seen one the teacher explains it as it's big has large ears and trunk then you create a mental image of an elephant this would be the encoding process then your friend asks what does an elephant look like then you decode that mental image of the elephant by explaining the features of an elephant or perhaps by drawing one this is the decoding process to understand the encoder/decoder architecture better let's implement a simple model that reverses a sentence first the encoder receives an one hot representation of the sentence and converts it to word ids next the decoder takes in the word ids reverses them and converts the reverse ids back to the 100% ation resulting in the reverse sentence in order to implement the encoder you will first implement a function called words to one hunt which will convert a given list of words to one not vectors the resulting one hunter will have the shape number of words by Nam classes Nam classes is three in our example the encoder function is a simple function that takes in an array of one hot wetters as the argument and returns the word ids corresponding to the one hot vectors to obtain the word ids from the one hot vectors you can use the NP dark max function and Peter Arg max computes the index of the maximum element along a given axis since one hot vectors are laid out along axis one you can use axis equals one after defining the two functions you can encode a given sentence to do that first call the words two one horn honks as the first argument and word to index as the second argument to get the one hot vectors next call the encoder function with the one hunters as the argument to obtain the context vector finally print the context vector the context contains the corresponding word IDs of the words the decoder will take in the word IDs reverse the IDS and then return the 100's of the reversed words next you write the decoder as a function which takes in the context vector produced by the encoder and produces one hunters of the reversed words the decoder function first reverses the word IDs in the context vector in numpy and 1d array can be reversed by adding Colin Colin minus one within square brackets after reversing the word IDs the one hot vectors are obtained by calling the two categorical function you also need a helper function one hot - words which will convert a set of one hot vectors to human readable words to do that the one on two words function takes in an array of one hot vectors and a dictionary index to word which Maps a word ID to a word consequentially indexed word is the reverse dictionary of word - in this used in the encoder finally you can compute the decoder output by calling the decoder function with the context vector as an argument and obtain the reversed words by calling d1 hot words function with the correct arguments now let's implement the sentence

Original Description

Want to learn more? Take the full course at https://www.datacamp.com/courses/machine-translation-in-python?embedded=true&slug=machine-translation-in-python at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work. --- Now you will learn about the encoder decoder architecture in more detail. A machine translation model works by, first, consuming the words of the source language sequentially, and then, sequentially predicting the corresponding words in the target language. However, under the hood, it is actually two different models; an encoder and a decoder. Let's first look at the encoder. In our example, the encoder takes in one-hot vectors of English words as inputs, and produces a compressed representation of the inputs known as a context vector. Then, the decoder consumes the context vector as an input and produces probabilistic predictions for each time step. The word for a given time step is selected as the word with the highest probability. Note that, though the inputs to the encoder are ones and zeros, the decoder produces continuous probabilistic outputs. These models are also called sequence to sequence models because they map a sequence, that is, an English sentence to another sequence, that is, a French sentence. Let's understand this through an analogy. A teacher is explaining what an elephant is and you've never seen one. The teacher explains it as "it's big, has large ears and a trunk". Then you create a mental image of an elephant. This would be the encoding process. Then your friend asks "what does an elephant look like?". Then, you decode that mental image of the elephant by explaining the features of an elephant or perhaps by drawing one. This is the decoding process. To understand the encoder decoder architecture better, let's implement a simple model that reverses a sentence. First, the encoder receives an one-hot representation of the sentence and converts it to word IDs. Next, the
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from DataCamp · DataCamp · 0 of 60

← Previous Next →
1 SQL Server Tutorial: Date manipulation
SQL Server Tutorial: Date manipulation
DataCamp
2 R Tutorial: Intermediate Interactive Data Visualization with plotly in R
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
3 R Tutorial: Adding aesthetics to represent a variable
R Tutorial: Adding aesthetics to represent a variable
DataCamp
4 R Tutorial: Moving Beyond Simple Interactivity
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
5 Python Tutorial: Why use ML for marketing? Strategies and use cases
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
6 Python Tutorial: Preparation for modeling
Python Tutorial: Preparation for modeling
DataCamp
7 Python Tutorial: Machine Learning modeling steps
Python Tutorial: Machine Learning modeling steps
DataCamp
8 R Tutorial: The prior model
R Tutorial: The prior model
DataCamp
9 R Tutorial: Data & the likelihood
R Tutorial: Data & the likelihood
DataCamp
10 R Tutorial: The posterior model
R Tutorial: The posterior model
DataCamp
11 R Tutorial: An Introduction to plotly
R Tutorial: An Introduction to plotly
DataCamp
12 R Tutorial: Plotting a single variable
R Tutorial: Plotting a single variable
DataCamp
13 R Tutorial: Bivariate graphics
R Tutorial: Bivariate graphics
DataCamp
14 Python Tutorial: Customer Segmentation in Python
Python Tutorial: Customer Segmentation in Python
DataCamp
15 Python Tutorial: Time cohorts
Python Tutorial: Time cohorts
DataCamp
16 Python Tutorial: Calculate cohort metrics
Python Tutorial: Calculate cohort metrics
DataCamp
17 Python Tutorial: Cohort analysis visualization
Python Tutorial: Cohort analysis visualization
DataCamp
18 R Tutorial: Building Dashboards with flexdashboard
R Tutorial: Building Dashboards with flexdashboard
DataCamp
19 R Tutorial: Anatomy of a flexdashboard
R Tutorial: Anatomy of a flexdashboard
DataCamp
20 R Tutorial: Layout basics
R Tutorial: Layout basics
DataCamp
21 R Tutorial: Advanced layouts
R Tutorial: Advanced layouts
DataCamp
22 Python Tutorial: Time Series Analysis in Python
Python Tutorial: Time Series Analysis in Python
DataCamp
23 Python Tutorial: Correlation of Two Time Series
Python Tutorial: Correlation of Two Time Series
DataCamp
24 Python Tutorial: Simple Linear Regressions
Python Tutorial: Simple Linear Regressions
DataCamp
25 Python Tutorial: Autocorrelation
Python Tutorial: Autocorrelation
DataCamp
26 R Tutorial: The gapminder dataset
R Tutorial: The gapminder dataset
DataCamp
27 R Tutorial: The filter verb
R Tutorial: The filter verb
DataCamp
28 R Tutorial: The arrange verb
R Tutorial: The arrange verb
DataCamp
29 R Tutorial: The mutate verb
R Tutorial: The mutate verb
DataCamp
30 R Tutorial: What is cluster analysis?
R Tutorial: What is cluster analysis?
DataCamp
31 R Tutorial: Distance between two observations
R Tutorial: Distance between two observations
DataCamp
32 R Tutorial: The importance of scale
R Tutorial: The importance of scale
DataCamp
33 R Tutorial: Measuring distance for categorical data
R Tutorial: Measuring distance for categorical data
DataCamp
34 Python Tutorial: Plotting multiple graphs
Python Tutorial: Plotting multiple graphs
DataCamp
35 Python Tutorial: Customizing axes
Python Tutorial: Customizing axes
DataCamp
36 Python Tutorial: Legends, annotations, & styles
Python Tutorial: Legends, annotations, & styles
DataCamp
37 Python Tutorial: Introduction to iterators
Python Tutorial: Introduction to iterators
DataCamp
38 Python Tutorial: Playing with iterators
Python Tutorial: Playing with iterators
DataCamp
39 Python Tutorial: Using iterators to load large files into memory
Python Tutorial: Using iterators to load large files into memory
DataCamp
40 SQL Tutorial: Introduction to Relational Databases in SQL
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
41 SQL Tutorial: Tables: At the core of every database
SQL Tutorial: Tables: At the core of every database
DataCamp
42 SQL Tutorial: Update your database as the structure changes
SQL Tutorial: Update your database as the structure changes
DataCamp
43 Python Tutorial: Classification-Tree Learning
Python Tutorial: Classification-Tree Learning
DataCamp
44 Python Tutorial: Decision-Tree for Classification
Python Tutorial: Decision-Tree for Classification
DataCamp
45 Python Tutorial: Decision-Tree for Regression
Python Tutorial: Decision-Tree for Regression
DataCamp
46 Python Tutorial: Census Subject Tables
Python Tutorial: Census Subject Tables
DataCamp
47 Python Tutorial: Census Geography
Python Tutorial: Census Geography
DataCamp
48 Python Tutorial: Using the Census API
Python Tutorial: Using the Census API
DataCamp
49 R Tutorial: A/B Testing in R
R Tutorial: A/B Testing in R
DataCamp
50 R Tutorial: Baseline Conversion Rates
R Tutorial: Baseline Conversion Rates
DataCamp
51 R Tutorial: Designing an Experiment - Power Analysis
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
52 R Tutorial: Introduction to qualitative data
R Tutorial: Introduction to qualitative data
DataCamp
53 R Tutorial: Understanding your qualitative variables
R Tutorial: Understanding your qualitative variables
DataCamp
54 R Tutorial: Making Better Plots
R Tutorial: Making Better Plots
DataCamp
55 SQL Tutorial: OLTP and OLAP
SQL Tutorial: OLTP and OLAP
DataCamp
56 SQL Tutorial: Storing data
SQL Tutorial: Storing data
DataCamp
57 SQL Tutorial: Database design
SQL Tutorial: Database design
DataCamp
58 Python Tutorial: Introduction to spaCy
Python Tutorial: Introduction to spaCy
DataCamp
59 Python Tutorial: Statistical Models
Python Tutorial: Statistical Models
DataCamp
60 Python Tutorial: Rule-based Matching
Python Tutorial: Rule-based Matching
DataCamp

This video tutorial teaches the encoder/decoder architecture in machine translation models, using Python and sequence-to-sequence models. It covers one-hot encoding, encoding and decoding functions, and implementing a simple model that reverses a sentence.

Key Takeaways
  1. Implement the words_to_one_hot function to convert words to one-hot vectors
  2. Define the encoder function to convert one-hot vectors to word IDs
  3. Implement the decoder function to reverse the word IDs and produce one-hot vectors of the reversed words
  4. Use the NP.argmax function to obtain the word IDs from the one-hot vectors
  5. Call the decoder function with the context vector to obtain the reversed words
💡 The encoder/decoder architecture is a key component of machine translation models, and understanding how to implement it is crucial for building effective models.

Related Reads

Up next
Marks Weightage | Quantitative Aptitude CA Foundation September 2026 | ABC Analysis | Nithin
ArivuPro Academy
Watch →