Basic Python For AI: Memorization Matters!
Basic Python For AI: Memorization Matters!
Memorization Matters!
Quang-Vinh Dinh
PhD in Computer Science
Wednesday (05/06/2024)
Objectives
Data Representation Conditions Functions
== Equal Built-in Functions
!= Not equal Print value onto the
print(params)
> Greater than screen
Return class type of
< Less than type(params)
variable
>= Greater or equal than
Ask user to input a
input(prompt)
<= Less or equal than string
int(),
Type conversion
float()
Function
_ Define a function:
def function_name(paramerters):
‘‘‘
docstring
‘‘‘
# your code goes here
return result
Outline
Variable
SECTION 1
_ Declare a variable:
Integer 1, 2, 0, -1, -2
Float 1.5, 0.5, -3.21
Functions
SECTION 4
Conditions
Introduction Section 1 Page 1
❖ Python history
Introduction Section 1 Page 2
❖ Python ecosystem
Introduction Section 1 Page 3
❖ First Python program
❖ Using Google Colab
https://colab.research.google.com/notebooks/intro.ipynb#recent=true
Demo
Introduction Section 1 Page 4
❖ What should a programming language provide?
Objects Encode/Represent
Numbers Text
…, 1, 2, 3 …
1, -2 … a, b, c
-1.2, 2.7 … Hello double
1.4, 2.7, …
‘A’
… ‘1’ float
char int
Real World
Introduction Section 1 Page 5
❖ What should a programming language provide?
0, 1, 2, 3
a, b, c, d
Data Representation in Python Section 1 Page 6
❖ Logic, number and text
variable_name = variable_value
Variable values
Integer 1, 2, 3, 0, -1, -2
variable_name = variable_value
variable_name Example
Should has a meaning
❖ replace() Function
List Section 1 Page 11
❖ A container that can contain elements
data = [4, 5, 6, 7, 8, 9]
data[0]
4
Forward
0 1 2 3 4 5
index data[3]
4 5 6 7 8 9 7
Dictionary Section 1 Page 12
❖ Structure
bracket
Create a dictionary
Other Data Representation Section 1 Page 13
❖ Tabular and image data
Outline
Function
SECTION 1
_ Define a function:
return result
SECTION 2
Data Representation _ Default values:
def function_name(p1=0, p2=0):
# your code goes here
Functions print(params)
Print value onto the
screen
Return class type of
type(params)
variable
Conditions int(),
float()
Type conversion
All-in-One 2024 Functions Section 1 Page 14
❖ Motivation: repeat a task
number1 add
(Compute the addition addition
number2
between two numbers)
All-in-One 2024 Functions Section 1 Page 15
❖ Motivation: separate a task
type(parameter)
print(parameters)
All-in-One 2024 Random and Math Modules Section 1 Page 17
Demo
https://docs.python.org/3/library/math.html
Random
Module
E: Pick a ball from the basket
All-in-One 2024
Example Experiment 1 Experiment 2
❖ Design a loss function
Got a red ball Got a blue ball
−𝑙𝑜𝑔 𝑝(𝐸)
how?
Module
All-in-One 2024 Example Section 1 Page 22
❖ Compute percentage
𝑋 = {2,5,3}
𝑋 = {𝑋1 , … , 𝑋𝑁 }
𝑆 = 10
Formula
𝑋 = {2,5,3}
𝑁
𝑋𝑖
𝑆 = 𝑋𝑖 𝑃𝑖 = 𝑃 = {20%, 50%, 30%}
𝑆
𝑖=1
All-in-One 2024 Example Section 1 Page 23
❖ Softmax function
Input Probability
0 ≤ 𝑓 𝑧𝑖 ≤ 1
Input Probability
𝑓 𝑧𝑖 = 1
𝑖
𝑧1 = 1.0 𝑓(𝑧1 ) = 0.09
𝑧2 = 2.0 Softmax 𝑓(𝑧2 ) = 0.24
𝑧3 = 3.0 𝑓(𝑧3 ) = 0.67
Implementation
(straightforward)
Softmax function
Chuyển các giá trị của một vector thành các giá trị xác suất
Formula
𝑥𝑖 Input Probability
𝑒
𝑓 𝑥𝑖 =
σ𝑗 𝑒 𝑥𝑗 𝑥1 = 1.0 𝑓(𝑥1 ) = 0.09
0 ≤ 𝑓 𝑥𝑖 ≤ 1 𝑥2 = 2.0 Softmax 𝑓(𝑥2 ) = 0.24
𝑒𝑥
Hàm mũ tăng rất
nhanh khi x tăng
𝑥 ∈ [−∞, +∞)
𝑒 𝑥 ∈ [0, +∞)
𝑥
Implementation (stable) Softmax function (stable) 𝑒𝑥
(Stable) Formula Probability
X X-m
𝑥 ∈ (−∞, 0)
𝑚 = max(𝒙) 𝑥1 = 1.0 𝑥1 = −2.0 𝑓(𝑥1 ) = 0.09 𝑒 𝑥 ∈ (0, 1)
𝑒 (𝑥𝑖−𝑚) 𝑥2 = 2.0 𝑥2 = −1.0 Softmax 𝑓(𝑥2 ) = 0.24
𝑓 𝑥𝑖 =
σ𝑗 𝑒 (𝑥𝑗 −𝑚) 𝑥3 = 3.0 𝑥3 = 0 𝑓(𝑥3 ) = 0.67 𝑥
User-defined Functions
Functions in Python Section 1 Page 27
❖ Syntax
Do docstring
Explain and describe the function
Name compute_rectangle_area
Parameter height, width
Define function name
Lowercase with underscores, Output area
usually begin with a verb
Do docstring
Explain and describe the function
Default values
All-in-One 2024 Example: Derivative
Section 1 Page 30
❖ Đạo hàm cho hàm liên tục
All-in-One 2024 Example: Derivative Section 1 Page 30
❖ Đạo hàm cho hàm liên tục
𝑑 𝑓 𝑥 + ∆𝑥 − 𝑓(𝑥)
𝑓(𝑥) = lim
f(x) 𝑑𝑥 ∆𝑥→0 ∆𝑥
∆𝑦
∆𝑥
∆𝑥 cần tiến về 0 để
𝑥 𝑥 + ∆𝑥 𝑥 đường tiếp tuyến tiến
về hàm f(x) trong vùng
𝑑 𝑑𝑦 ′ lân cận tại x
𝑓 𝑥 , , 𝑦 , 𝑓′(𝑥)
𝑑𝑥 𝑑𝑥
All-in-One 2024 Example: Derivative Section 1 Page 31
❖ Implementation
𝑓 𝑥 = 𝑥 2 + 2𝑥
𝑓 ′ 𝑥 = 2𝑥 + 2
All-in-One 2024 Example: Derivative Section 1 Page 32
❖ Implementation
Introduction
Condition
_ Comparision Operators:
SECTION 2 == Equal
Data Representation !=
>
Not equal
Greater than
< Less than
>= Greater than or equal
Conditions
Branching in Python Section 1 Page 33
❖ Comparison operators
Operator Meaning
== Equal
!= Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
Branching in Python Section 1 Page 34
❖ if condition
colon
Khối code K1
keyword
Điều False
kiện if
True
x>0 x>0
True False
result = x …
result=5 result=0
Branching in Python Section 1 Page 36
❖ if-else condition
Điều False
kiện if
keyword True
True True
Khối code trong if Khối code trong elif Khối code trong else
indentation
Input: 𝑎 and 𝑏
if 𝑎 > 0 then
𝑦 = 𝑏2
if 𝑎 ≤ 0 then
𝑦= 𝑏
if 𝑎 > 0 if 𝑎 ≤ 0 if 𝑎 > 0 if 𝑎 ≤ 0
Branching in Python Section 1 Page 39
❖ if-elif-else condition
Input: 𝑎 and 𝑏
if 𝑎 = 0 then 𝑦 = 𝑏 2
if 𝑎 = 1 then 𝑦 = 𝑏
if 𝑎 = 2 then 𝑦 = 𝑏
if 𝑎 == 0 if 𝑎 == 1
if 𝑎 == 2
if 𝑎 == 0 if 𝑎 == 1
if 𝑎 == 2
Alternative to If-Else (1)
Input: 𝑎 and 𝑏 Input: 𝑎 and 𝑏
if 𝑎 = 0 then 𝑦 = 𝑏 2 𝑎 𝐾−𝑎 2
𝑦= 𝑏 + 𝑏
if 𝑎 = 𝐾 then 𝑦 = 𝑏 𝐾 𝐾
Alternative to If-Else (2)
Input: 𝑎 and 𝑏 Input: 𝑎 and 𝑏
if 𝑎 > 0 then
𝑏2 + 𝑏 𝑏2 − 𝑏 𝑎
𝑦 = 𝑏2 𝑦= +
if 𝑎 < 0 then 2 2 𝑎
𝑦= 𝑏
All-in-One 2024 Alternative to If-Else (3) Section 1 Page 42
❖ One more example
Feature Label
#features = 1 Model
#classes = 3 𝑥 𝑧0 yො 0
1 𝑧2 yො 2
y = 0 → L(𝛉) = −log(ොy0 )
How to convert into a
y = 1 → L(𝛉) = −log(ොy1 )
single function?
y = 2 → L(𝛉) = −log(ොy2 )
All-in-One 2024 If-Else Condition and Math (3) Section 1 Page 43
Feature Label
#features = 1 Model
#classes = 3 𝑥 𝑧0 yො 0
1 𝑧2 yො 2
y(1−y) 2−y
L(𝛉) = − log(ොy2 )−y(2−y)log(ොy1 )−(1−y)( )log(ොy0 )
−2 2
𝑦=2 𝑦=1 𝑦=0
Feature Label
Model
#features = 1
𝑥 𝑧0 yො 0
#classes = 3
𝑦 ∈ 0,1,2 𝑧1 Softmax yො1
1 𝑧2 yො 2
1 0 0 = − 𝑦𝑖 log(ොyi )
𝑦=0→𝒚= 0 𝑦=1→𝒚= 1 𝑦=2→𝒚= 0
0 0 1 𝑖
Alternative to If-Else (3)
Input: 𝑎 and 𝑏 Input: 𝑎 and 𝑏
if 𝑎 = 0 then 𝑦 = 𝑏 2 Convert 𝑎 into a one-hot vector 𝒗
𝒗 = [1 0 0]
if 𝑎 = 1 then 𝑦 = 𝑏 𝒖 = [𝑏 2 𝑏 𝑏] 𝒗 = [0 1 0]
if 𝑎 = 2 then 𝑦 = 𝑏 𝑦=𝒗∙𝒖 𝒗 = [0 0 1]
All-in-One 2024 If-Else and Dictionary Section 1 Page 46
❖ Mixed conditions
Input: 𝑎
if 𝑎 = 1 then 𝑦 = 5 1 5 key
value
key value
Rule-based Chatbot
All-in-One 2024 Rule-based Chatbot
Section 1 Page 47
❖ Getting Started
Choose
57
All-in-One 2024 Rule-based Chatbot
Section 1 Page 48
❖ Chat tree
Hi / Hello / Xin chào
Xin chào quý khách! Tôi có thể giúp gì cho quý khách?
Choose
Quý khách muốn mua sản phầm nào ạ? Quý khách tra cứu bằng thông tin nào ạ? Quý khách cần hỗ trợ cài đặt
hay sữa chữa ạ?
Choose Choose
Choose
Điện thoại Laptop Máy tính bảng Số điện thoại Số seri Cài đặt Sữa chữa
58
All-in-One 2024 Rule-based Chatbot
Section 1 Page 49
❖ Flowchart
Customer
Does input No
exist in the
input options?
Yes
Can answer No
without Connect to Human
humans?
Yes
Return the answer
https://if-else-tts.streamlit.app/
https://deep-codeless.vercel.app/python-emulator
All-in-One 2024 Cheat Sheet
Run a Python program Basic Operators Condition Overflow/Underflow
_ Python files have “.py” at the end of the filename _ Comparision Operators: _ Underflow:
Operators Meaning result = 1e-100
_ To run a Python file: == Equal print(result) # 1e-100
+ Summation
python file.py result = 1e-1000
!= Not equal
- Subtraction print(result) # 0.0
Virtual Environment > Greater than
* Multiplication _ Overflow:
< Less than result = 1e100
_ Install Python Virtual Environment (in Linux): print(result) # 1e+100
sudo apt-get install –y python3-env / Division >= Greater or equal than result = 1e1000
_ Create a virtual environment: <= Less or equal than print(result) # inf
% Modulo
python3 –m venv a_name
_ Activate a virtual environment: // Floor Division
_ Conditional sentence: For Loop
source a_name/bin/activate if condition1:
** Power # your code _ Create a loop using for:
elif condition2: for element in iterable:
Install new package # code inside your for
# your code
_ e.g: to install matplotlib: else: _ Some iterables:
Function # your code
pip install matplotlib
String “aivietnam”
_ Define a function:
Variable Built-in Functions Tuple (1, 2, 3)
def function_name(paramerters):
_ Declare a variable: ‘‘‘ List [1, 2, 3]
docstring Print value onto the
variable_name = variable_value print(params) Dictionary {‘key1’: 1}
‘‘‘ screen
_ Variable types: # your code goes here Return class type of range() range(0, 5, 1)
type(params)
Integer 1, 2, 0, -1, -2 variable
return result _ Special keywords:
Float 1.5, 0.5, -3.21 Ask user to input a
input(prompt)
_ Default values: string break Exit the loop
String ‘AI’, “VIETNAM”
def function_name(p1=0, p2=0): int(), Move to next
float()
Type conversion continue
Boolean True, False # your code goes here iteration
61