1. Introduction to Python
& TensorFlow
DSW Camp & Jam
December 3rd, 2016
Bayu Aldi Yansyah
Data Scientist @ Sale Stock
https://careers.salestock.io
2. - Understand the basic of Python
- Able to write and execute Python program
- Understand what is TensorFlow and how to use it
Our Goals
Overview
3. - You understand the basic of programming (What is variable, data types
etc)
I assume …
Overview
4. 1. Introduction to Python
- Why learn Python?
- Python basic
- Python data types
- Comparison operators
- Control Flow
- Function
- Class
- Module
Outline
Overview
5. 2. Introduction to Tensorflow
- What is TensorFlow?
- Programming model
- Use case: Forward propagation of hidden layers
in Feed-forward Neural Networks model
Outline
Overview
6. 1.
PYTHON
INTRODUCTION
- Python is:
1. A programming language created by Guido Van Rossum in
1991 and emphasizes productivity and code readability.
2. A general purpose language that is easy and intuitive.
3. A multi purpose language, it brings people with different
backgrounds together.
- In Python, everything is an Object.
7. 1.1.
WHY LEARN PYTHON?
MOTIVATION
- Easy to learn and intuitive.
- One of the most popular programming languages on Github.
- One of the best languages for data science. The important factor is the
Python community.
- Python is used by a bunch of cool companies like Google, Dropbox etc.
- It works very well with C and C++. For example: Sale Stock’s
fastText.py is written in Python and C++, this python package is
used/starred by folks from Baidu, Comcast, Facebook, Alibaba, and
Github. https://github.com/salestock/fastText.py
8. 2.
BASIC
WRITE & EXECUTE “HELLO WORLD” PROGRAM
print “Hello word”
hello.py
% python hello.py
Hello world
Terminal
9. 2.1.
BASIC
SYNTAX: INDENTATION
is_new = True
if is_new:
print "Is new!”
else:
print "Uh, it's old stuff"
indentation.py
% python indentation.py
Is new!
Terminal
11. Run:
2.2.
BASIC
READ-EVAL-PRINT LOOP
% python
Python 2.7.10 (default, Jul 30 2016, 18:31:42) [GCC 4.2.1 Compatible
Apple LLVM 8.0.0 (clang-800.0.34)] on darwinType "help", "copyright",
"credits" or "license" for more information.
>>> print "hello”
hello
>>> 2 + 5
7
>>> "hello".upper()
'HELLO’
>>> 3 in [1, 2, 3]
True
>>>
Terminal
12. 3.
DATA TYPES
INTRODUCTION
We will cover 6 data types in Python and their common
operations:
1. Numeric
2. Sequences
3. Sets
4. Dictionaries
5. Boolean
13. 3.1.
NUMERIC
INTRODUCTION
- There are 4 basic numeric types: Integer, Float, Long Integer and
Complex
- Common operations: Addition, Difference, Product, Quotient and
modulo
- Type conversion is required for some operation
17. Type conversion:
- int(x) : x to integer
- float(x): x to float
- long(x) : x to long integer
3.1.
NUMERIC
TYPE CONVERSIONS
# Without conversion
>>> 12/100
0
# Convert to float first
>>> float(12)/100
0.12 PYTHON REPL
18. 3.2.
SEQUENCES
INTRODUCTION
- To store multiple values in an organized and efficient fashion.
- There are three kinds of sequences in Python:
1. Strings
2. Lists
3. Tuples
19. 3.2.1.
SEQUENCES
STRINGS: INTRO
- Define new string by simply by enclosing characters in single or double
quotes.
- Slice: A[0] = ‘H’
- Range Slice: A[1:3] = ‘el’
A[a:b] is all of A[i] where a <= i < b
- Common operations are concatenation, repetition, membership
checking and formatting.
A H e l l o
index 0 1 2 3 4
20. 3.2.1.
SEQUENCES
STRINGS: DEFINE A NEW STRING
# Single line
company_name = 'Sale Stock’
# Multiline
description = ''’
Sale Stock Pte, Ltd is a fast-growing multinational
tech start up company that is currently specialising
in mobile-commerce.
''’
mission = ('Giving access to affordable,'
' high-quality clothes to everyone’
‘ who needs it.')
string.py
22. # Membership checking
>>> 'h' in 'hello’
True
>>> ‘h' not in 'hello’
False
# Formatting
>>> 'Hello, %s' % 'DSW!’
'Hello, DSW!’
>>> 'My number is %d' % 11
’My number is 11’
>>> 'pi = %f' % (22.0/7.0)
‘pi = 3.142857'
3.2.1.
SEQUENCES
STRINGS: OPERATORS (CONTINUED)
PYTHON REPL
23. 3.2.2.
SEQUENCES
LISTS: INTRO
- Each element of a list is assigned an index number. The index starts
from zero.
- Slice: B[0] = 12
- Range Slice: B[1:3] = [3,4]
B[a:b] is all of B[i] where a <= i < b
B 12 3 4 5 15
index 0 1 2 3 4
24. 3.2.2.
SEQUENCES
LISTS: DEFINE NEW LIST
# Define a list of number
>>> numbers = [1, 4, 12, 1]
>>> numbers
[1, 4, 12, 1]
# Define a list of string
>>> words = ['hey', 'there', '!']
>>> words
['hey', 'there', '!’]
PYTHON REPL
33. 3.3.
SETS
INTRODUCTION
- Just like lists except that Sets are unordered and the value is unique.
- We can’t do slice and range slice operation on Sets.
- Sets performs faster for element insertion, deletion, and membership
checking than lists and tuples.
- Sets support mathematical set operations such as testing for subsets
and finding the union or intersection of two sets.
34. 3.3.
SETS
DEFINE NEW SET
# Define a set of number
>>> numbers = set([1, 4, 12, 1])
>>> numbers
set([1, 4, 12])
# Define a set of string
>>> words = set(['hey', 'there', '!'])
>>> words
set(['hey', 'there', '!’])
PYTHON REPL
35. 3.3.
SETS
OPERATORS
# Define set a and b
>>> a = set([1, 2, 3])
>>> b = set([1, 4, 5])
# Perform union
>>> a.union(b)
set([1, 2, 3, 4, 5])
# Perform Intersection
>>> a.intersection(b)
set([1])
# Perform Difference
>>> a.difference(b)
set([2, 3])
PYTHON REPL
37. 3.4.
DICTIONARIES
INTRODUCTION
- Dictionaries is a associative array. Collection of (key, value) pairs where
the key is unique and only map to one value.
- We can add, change, and remove value from a dictionary by their key.
38. 3.4.
DICTIONARIES
DEFINE NEW DICTIONARY
# Define an empty dictionary
>>> empty_dict = {}
# Define a dictionary
>>> data = {‘name’: ‘DSW’, ‘type’: ‘camp’}
>>> data
{'type': 'camp', 'name': 'DSW'}
# Access value by key
>>> data['name']
'DSW'
PYTHON REPL
39. 3.4.
DICTIONARIES
INSERT, UPDATE & DELETE
>>> d = {'name': 'D', 'order': 4}
# Insert new key-value pairs
>>> d['last_order'] = 6
>>> d
{'last_order': 6, 'name': 'D', 'order': 4}
# Update the value
>>> d['name'] = 'D D’
>>> d
{'last_order': 6, 'name': 'D D', 'order': 4}
PYTHON REPL
40. 3.4.
DICTIONARIES
INSERT, UPDATE & DELETE (CONTINUED)
# Delete the key and value
>>> del d['order']
>>> d
{'last_order': 6, 'name': 'D D'}
PYTHON REPL
41. 3.5.
BOOLEAN
INTRODUCTION
- Represent the truth value.
- Values that considered as False: None, False, zero of any numeric type,
any empty sequences and any empty dictionaries.
42. 4.
COMPARISON OPERATORS
INTRODUCTION
- Operator that compare two or more objects.
- The result of this operator is boolean value.
- There are 3 basic comparison operators:
- Logical Comparison
- Identity Comparison
- Arithmetic Comparison
.
43. 4.1.
COMPARISON OPERATORS
LOGICAL
# Define the boolean value
>>> a = True; b = False
# Logical and
>>> a and a
True
>>> a and b
False
# Logical or
>>> a or b
True
>>> b or b
False
PYTHON REPL
47. 5.
CONTROL FLOW
INTRODUCTION
- Just like any other programming language, Python also have a basic
control flow such as if-else, for loop and while loop.
- Unlike any other programming language, we can create an easy-to-
understand control flow in python without hasle. Thanks to the nice
syntax.
- We can use break to stop the for-loop or while-loop.
50. 5.2.
CONTROL FLOW
FOR-LOOP
# Basic
for i in xrange(2):
print 'index:', I
# Iterate on sequences
scores = [0.2, 0.5]
for score in scores:
print 'score:', score
for_loop.py
51. 5.2.
CONTROL FLOW
FOR-LOOP (CONTINUED)
# Iterate on dictionaries
data = {'name': 'DSW', 'type': 'camp'}
for key in data:
value = data[key]
print key, ':', value
for_loop.py
53. 5.3.
CONTROL FLOW
WHILE-LOOP
- Just like for-loop that do iteration, but while-loop is accept boolean value
as their condition instead of iterator.
- If condition is false, the loop is stopped
56. 6.
FUNCTION
INTRODUCTION
- Function in Python is defined using the following syntax:
def function_name(arg1, optional_arg=default_value):
# do some operation here
# Or return some value
60. 7.
CLASS
INTRODUCTION
- We can use Class to encapsulate object and their logic.
- Class can be defined using the following syntax:
class ClassName:
def __init__(self, arg1, arg2):
# Set property
self.property_name= arg1
# Define a method or function that read or
# update the property
def method_name(self, arg…):
# Define here
62. 7.
CLASS
EXAMPLE (CONTINUED)
if __name__ == '__main__':
b = Book('Hunger games')
print 'Book title:', b.title
print 'Book status: borrowed=’, b.is_borrowed
# We change the state of the object
print 'Borrow the book.'
b.borrow()
print 'Book title:', b.title
print 'Book status: borrowed=', b.is_borrowed
book.py
63. 7.
CLASS
EXAMPLE OUTPUT
% python book.py
Book title: Hunger games
Book status: borrowed= False
Borrow the book.
Book title: Hunger games
Book status: borrowed= True
TERMINAL
64. 8.
MODULE
INTRODUCTION
- Python module is just a file.
- We can use module to group all related variable, constant, function and
class in one file.
- This allow us to do a modular programming.
- Recall our book.py on previous section, we will use that as an example.
65. 8.
MODULE
EXAMPLE
# Import Book class from module book.py
from book import Book
if __name__ == '__main__':
books = []
for i in xrange(10):
title = 'Book #%s' % i
book = Book(title)
books.append(book)
# Show list of available books
for b in books:
print 'Book title:', b.title
store.py
66. 8.
MODULE
EXAMPLE OUTPUT
% python store.py
Book title: Book #0
Book title: Book #1
Book title: Book #2
Book title: Book #3
Book title: Book #4
Book title: Book #5
Book title: Book #6
Book title: Book #7
Book title: Book #8
Book title: Book #9
store.py
68. 9.
TENSORFLOW
INTRODUCTION
- TensorFlow is an interface for expressing machine learning algorithms,
and an implementation for executing such algorithms.
- TensorFlow is available as Python package.
- Allows team of data scientist to express the ideas in shared
understanding concept.
69. 10.
TENSORFLOW
PROGRAMMING MODEL
- TensorFlow express a numeric computation as a graph.
- Graph nodes are operations which have any number of inputs and
outputs.
- Graph edges are tensors which flow between nodes.
70. 10.
TENSORFLOW
PROGRAMMING MODEL
- Suppose we have a Neural networks with the following hidden layer:
- We can represent this as a the computation graph:
𝑓𝜃
𝑙
𝑖
= tanh(𝑊 𝑙𝑇
𝑥𝑖 + 𝑏 𝑙
)
𝑊 𝑙𝑇
𝑥𝑖
Matrix
Multiplication
Addition
tanh
𝑏 𝑙
71. 11.
TENSORFLOW
IMPLEMENTATION IN TENSORFLOW
import numpy as np
import tensorflow as tf
# Initialize required variables
x_i = np.random.random(size=(32, 256))
# Create the computation graph
b = tf.Variable(tf.zeros((100,)))
W = tf.Variable(tf.random_uniform(shape=(256, 100), minval=-1,
maxval=1))
x = tf.placeholder(tf.float32, (None, 256))
h_i = tf.tanh(tf.matmul(x, W) + b)
forward_prop.py
72. 11.
TENSORFLOW
IMPLEMENTATION IN TENSORFLOW
# Run the computation graph within new session
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# Fetch h_i and feed x_i
sess.run(h_i, {x: x_i})
forward_prop.py
Oke,
Tujuan kita adalah
yang pertama: kita mengerti bagaimana cara melakukan pengelompokan kata berdasarkan
kesamaan semantiknya
Kemudian tujuan kita yang kedua adalah,
kita paham penerapan deep learning untuk natural language understanding.
Jadi itu tujuan kita.
Tentunya kita untuk bisa paham dan menerapkannya, kita akan ada sesi hands-on
--
Apakah kita fokus ke teori aja?
lihat dulu deh nanti.
Oke,
Tujuan kita adalah
yang pertama: kita mengerti bagaimana cara melakukan pengelompokan kata berdasarkan
kesamaan semantiknya
Kemudian tujuan kita yang kedua adalah,
kita paham penerapan deep learning untuk natural language understanding.
Jadi itu tujuan kita.
Tentunya kita untuk bisa paham dan menerapkannya, kita akan ada sesi hands-on
--
Apakah kita fokus ke teori aja?
lihat dulu deh nanti.
itu tadi tujuan kita di sesi ini,
Di sesi ini saya mengansumsikan
itu tadi tujuan kita di sesi ini,
Di sesi ini saya mengansumsikan
itu tadi tujuan kita di sesi ini,
Di sesi ini saya mengansumsikan
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas
Ini nanti di brief,
lalu di tunjukkan outputnya
# Berarti setup terminal dulu sebelum presentasi, make sure kelihatan jelas