0% found this document useful (0 votes)
27 views61 pages

Basic Python For AI: Memorization Matters!

Uploaded by

dodaonam2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views61 pages

Basic Python For AI: Memorization Matters!

Uploaded by

dodaonam2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Basic Python for AI

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:

Introduction variable_name = variable_value


_ Variable types:

Integer 1, 2, 0, -1, -2
Float 1.5, 0.5, -3.21

SECTION 2 String ‘AI’, “VIETNAM”


Boolean True, False
Data Representation
SECTION 3

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

100, 101, 102, 103

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

Float 1.5, 0.5, -3.21, 1.0

String 'Joe', 'Schmoe', "Joe", "Schmoe"

Boolean True, False


Data Representation in Python Section 1 Page 7
❖ Logic, number and text

variable_name = variable_value

variable_name Example
Should has a meaning

Cannot use keywords


All-in-One 2024 Overflow and Underflow Section 1 Page 8
❖ Why?
Text Representation in Python Section 1 Page 9
❖ String creation
Text Representation in Python Section 1 Page 10
❖ String

❖ Logic operators ❖ + and * operators ❖ Insert into a string

❖ replace() Function
List Section 1 Page 11
❖ A container that can contain elements

list_name = [element-1, …, element-n]


data = 6 5 7 1 9 2
index 0 1 2 3 4 5

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

dictionary_name = {key-1:value-1, …, key-n:value-n}

element comma colon

Create a dictionary
Other Data Representation Section 1 Page 13
❖ Tabular and image data
Outline
Function
SECTION 1
_ Define a function:

Introduction def function_name(paramerters):


‘‘‘
docstring
‘‘‘
# your code goes here

return result
SECTION 2
Data Representation _ Default values:
def function_name(p1=0, p2=0):
# your code goes here

SECTION 3 Built-in Functions

Functions print(params)
Print value onto the
screen
Return class type of
type(params)
variable

SECTION 4 Ask user to input a


input(prompt)
string

Conditions int(),
float()
Type conversion
All-in-One 2024 Functions Section 1 Page 14
❖ Motivation: repeat a task

Input Function Output


(Do something with input)

Input: what the function needs to do a task

Output: what comes out of the function

number1 add
(Compute the addition addition
number2
between two numbers)
All-in-One 2024 Functions Section 1 Page 15
❖ Motivation: separate a task

number1 compute per1


number2 (Compute percentage of per2
two numbers)
Built-in Functions
All-in-One 2024 Built-in Functions Section 1 Page 16

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

Which experiment makes you more surprised?


How to measure
the surprises?
A: Get a red ball Observation

B: Get a blue ball


𝑆𝑢𝑟𝑝𝑟𝑖𝑠𝑒(𝐸) 𝑝(𝐸)
9
p A = = 0.9 1
10 𝑆𝑢𝑟𝑝𝑟𝑖𝑠𝑒(𝐸) =
1 𝑝(𝐸)
p B = = 0.1
10
Problem?
1
−𝑙𝑜𝑔 𝑝(𝐸)
𝑝(𝐸)

−𝑙𝑜𝑔 𝑝(𝐸)
how?

Monotonic decrease of the function surprise(E) In information theory


1
𝑙𝑜𝑔 𝑆𝑢𝑟𝑝𝑟𝑖𝑠𝑒(𝐸) = 𝑙𝑜𝑔
𝑝(𝐸)
Information(𝑥) = −𝑙𝑜𝑔 𝑝(𝑥)
= −𝑙𝑜𝑔 𝑝(𝐸)
Random Section 1 Page 27

Module
All-in-One 2024 Example Section 1 Page 22
❖ Compute percentage

Data Given the data

𝑋 = {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

𝑧1 = 1.0 𝑓(𝑧1 ) = 0.12


𝑧𝑖
𝑒 Softmax
𝑃𝑖 = 𝑓 𝑧𝑖 =
σ 𝑗 𝑒 𝑧𝑗 𝑧2 = 3.0 𝑓(𝑧2 ) = 0.88

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

෍ 𝑓 𝑥𝑖 = 1 𝑥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 Giá trị nan vì
0 ≤ 𝑓 𝑥𝑖 ≤ 1 𝑥2 = 2.0 Softmax 𝑓(𝑥2 ) = 0.24 𝑒 𝑥 vượt giới
hạn lưu trữ của
෍ 𝑓 𝑥𝑖 = 1 𝑥3 = 3.0 𝑓(𝑥3 ) = 0.67
𝑖 biến

𝑒𝑥
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

Notes for function construction parentheses colon


Define function name
Lowercase with underscores
and begin with a verb
Indentation keyword
Use 4 spaces for indentation
Determine function parameters
Input data help the function done

Do docstring
Explain and describe the function

Output of the function


General structure of a function
indentation
Functions in Python Section 1 Page 28

Name compute_rectangle_area
Parameter height, width
Define function name
Lowercase with underscores, Output area
usually begin with a verb

Determine function parameters


Input data help the function done

Do docstring
Explain and describe the function

Output of the function


32
Functions in Python Section 1 Page 29
❖ Default values

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

𝑦 𝑇ℎ𝑎𝑦 đổ𝑖 𝑡ℎ𝑒𝑜 𝑦 ∆𝑦


Đạo hàm= =
𝑇ℎ𝑎𝑦 đổ𝑖 𝑡ℎ𝑒𝑜 𝑥 ∆𝑥

𝑑 𝑓 𝑥 + ∆𝑥 − 𝑓(𝑥)
𝑓(𝑥) = 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

Cho hàm số f(x)


𝑓 𝑥 = 𝑥 2 + 2𝑥

Phương trình đạo hàm


𝑓 ′ (𝑥) = 2𝑥 + 2
Công thức đạo
𝑓 𝑥 + ∆𝑥 − 𝑓 𝑥
𝑓 ′ (𝑥) = lim
∆𝑥→0 ∆𝑥
Outline
SECTION 1

Introduction
Condition

_ Comparision Operators:

SECTION 2 == Equal

Data Representation !=
>
Not equal
Greater than
< Less than
>= Greater than or equal

SECTION 3 <= Less than or equal

Functions _ Conditional sentence:


if condition1:
# your code
elif condition2:
# your code
else:
SECTION 4 # your code

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

Khối code trong if

indentation Khối code K2


Branching in Python
if 0 if 𝑥 ≤ 0
condition ReLU 𝑥 = ቊ
𝑥 if 𝑥 > 0

x=5; result=0 x=-2; result=0

x>0 x>0

True False

result = x …

result=5 result=0
Branching in Python Section 1 Page 36
❖ if-else condition

colon Khối code (K1)

Điều False
kiện if

keyword True

Khối code trong if Khối code trong else

indentation Khối Code (K2)


Branching in Python Section 1 Page 37
❖ if-elif-else condition

colon Khối code (K1)

Điều False Điều False


kiện if kiện elif

True True

Khối code trong if Khối code trong elif Khối code trong else

indentation

Khối Code (K2)


Branching in Python Section 1 Page 37
❖ if-else condition

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

𝑦 ∈ 0,1,2 𝑧1 Softmax yො1

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

𝑦 ∈ 0,1,2 𝑧1 Softmax yො1

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

Ok! but awkward!!! … and how to improve?


All-in-One 2024 Alternative to If-Else (3) Section 1 Page 44

Feature Label
Model
#features = 1
𝑥 𝑧0 yො 0
#classes = 3
𝑦 ∈ 0,1,2 𝑧1 Softmax yො1

1 𝑧2 yො 2

One-hot encoding for label Loss function


𝑦0
𝒚 = 𝑦1 𝑦𝑖 ∈ 0,1 ෍ 𝑦𝑖 = 1 ෝ) = −𝑦2 log(ොy2 )−𝑦1 log(ොy1 )−𝑦0 log(ොy0 )
L(𝒚, 𝒚
𝑦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

if 𝑎 = ′lr′ then 𝑦 = 0.1


lr 0.1
if 𝑎 = ′opt′ then 𝑦 = ′SGD′ Dictionary
opt SGD

value
key value
Rule-based Chatbot
All-in-One 2024 Rule-based Chatbot
Section 1 Page 47
❖ Getting Started

Choose

Tra cứu bảo hành

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

Tư vấn mua hàng Tra cứu bảo hành Hỗ trợ kỹ thuật

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

You might also like