Introduction to Python and TensorFlow
-
Upload
bayu-aldi-yansyah -
Category
Software
-
view
583 -
download
3
Transcript of Introduction to Python and TensorFlow
Introduction to Python & TensorFlow
DSW Camp & JamDecember 3rd, 2016Bayu Aldi YansyahData Scientist @ Sale Stockhttps://careers.salestock.io
- Understand the basic of Python- Able to write and execute Python program- Understand what is TensorFlow and how to use it
Our GoalsOverview
- You understand the basic of programming (What is variable, data types etc)
I assume …Overview
1. Introduction to Python- Why learn Python?- Python basic- Python data types- Comparison operators- Control Flow- Function- Class- Module
OutlineOverview
2. Introduction to Tensorflow- What is TensorFlow?- Programming model- Use case: Forward propagation of hidden layers in Feed-forward Neural Networks model
OutlineOverview
1.PYTHONINTRODUCTION
- 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.
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
2.BASICWRITE & EXECUTE “HELLO WORLD” PROGRAM
print “Hello word”
hello.py
% python hello.pyHello world
Terminal
2.1.BASICSYNTAX: INDENTATION
is_new = Trueif is_new: print "Is new!”else: print "Uh, it's old stuff" indentation.py
% python indentation.pyIs new!
Terminal
Run:
2.1.BASICSYNTAX: COMMENT
# Commented line is not executed# print "Hello"print "Hai""""also thisprint "hai hai" """
comment.py
% python comment.pyHai
Terminal
Run:
2.2.BASICREAD-EVAL-PRINT LOOP
% pythonPython 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 + 57>>> "hello".upper()'HELLO’>>> 3 in [1, 2, 3]True>>> Terminal
3.DATA TYPESINTRODUCTION
We will cover 6 data types in Python and their common operations:1. Numeric2. Sequences 3. Sets4. Dictionaries5. Boolean
3.1.NUMERICINTRODUCTION
- 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
3.1.NUMERIC4 TYPES
# Integer>>> 10 10# Float>>> 0.50.5# Complex>>> 10 + 5J(10+5j)# Long integer>>> 10L10L
PYTHON REPL
3.1.NUMERICOPERATORS
# Addition>>> 10 + 6 16# Difference>>> 100 – 9010# Product>>> 0.5 * 6030.0# Quotient>>> 22.0/7.03.142857142857143
PYTHON REPL
3.1.NUMERICOPERATORS (CONTINUED)
# Modulo>>> 4 % 31
PYTHON REPL
Type conversion:- int(x) : x to integer- float(x): x to float- long(x) : x to long integer
3.1.NUMERICTYPE CONVERSIONS
# Without conversion>>> 12/1000# Convert to float first>>> float(12)/1000.12 PYTHON REPL
3.2.SEQUENCESINTRODUCTION
- To store multiple values in an organized and efficient fashion.- There are three kinds of sequences in Python:
1. Strings2. Lists3. Tuples
3.2.1.SEQUENCESSTRINGS: 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 oinde
x 0 1 2 3 4
3.2.1.SEQUENCESSTRINGS: DEFINE A NEW STRING
# Single linecompany_name = 'Sale Stock’# Multilinedescription = ''’Sale Stock Pte, Ltd is a fast-growing multinationaltech start up company that is currently specialisingin mobile-commerce.''’mission = ('Giving access to affordable,'
' high-quality clothes to everyone’ ‘ who needs it.')
string.py
3.2.1.SEQUENCESSTRINGS: OPERATORS
# Concatenation>>> 'Hello ' + 'There' 'Hello There’# Repetition>>> 'hello' * 3 'hellohellohello’# Slice>>> 'hello'[0]'h’# Range Slice>>> 'hello'[0:2] 'he’
PYTHON REPL
# 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.SEQUENCESSTRINGS: OPERATORS (CONTINUED)
PYTHON REPL
3.2.2.SEQUENCESLISTS: 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 15inde
x 0 1 2 3 4
3.2.2.SEQUENCESLISTS: 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
3.2.2.SEQUENCESLISTS: SLICING
# Slicing>>> words[0]'hey’>>> words[0:]['hey', 'there', '!']>>> words[2:]['!']>>> numbers[3]1>>> numbers[:3][1, 4, 12]
PYTHON REPL
3.2.2.SEQUENCESLISTS: OPERATORS
# Concatenation>>> [1, 2, 3] + [4, 5, 6][1, 2, 3, 4, 5, 6]# Membership checking>>> 1 in [1, 2, 3]True>>> 1 not in [1, 2, 3]False# Repetition>>> ['Repeat'] * 3['Repeat', 'Repeat', 'Repeat’]
PYTHON REPL
3.2.2.SEQUENCESLISTS: INSERT, DELETE & UPDATE
>>> scores = [0.1, 0.4, 0.5]# Insert new element>>> scores.append(0.2)>>> scores[0.1, 0.4, 0.5, 0.2]# Delete element>>> scores.pop(0)0.1>>> scores[0.4, 0.5, 0.2]
PYTHON REPL
3.2.2.SEQUENCESLISTS: INSERT, DELETE & UPDATE (CONTINUED)
>>> scores[0.4, 0.5, 0.2]# Update element>>> scores[0] = 0.6>>> scores[0.6, 0.5, 0.2]
PYTHON REPL
3.2.3.SEQUENCESTUPLES: INTRO
- Tuples are similar to lists, except they are immutable.- You can’t add, change, or remove elements of a tuple
3.2.3.SEQUENCESTUPLES: DEFINE NEW TUPLE
# Define a tuple>>> tuple1 = (1, 2, 3)>>> tuple1(1, 2, 3)>>> tuple2 = 1, 2, 3>>> tuple2(1, 2, 3)
PYTHON REPL
3.2.3.SEQUENCESTUPLES: SLICING
# Slicing>>> tuple1[0]1>>> tuple1[1]2>>> tuple1[0:2](1, 2)
PYTHON REPL
3.2.3.SEQUENCESTUPLES: OPERATORS
# Concatenation>>> (1, 2, 3) + (4, 5, 6)(1, 2, 3, 4, 5, 6)# Membership checking>>> 1 in (1, 2, 3)True>>> 1 not in (1, 2, 3)False# Repetition>>> ('Re', 'peat') * 3('Re', 'peat', 'Re', 'peat', 'Re', 'peat')
PYTHON REPL
3.3.SETSINTRODUCTION
- 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.
3.3.SETSDEFINE NEW SET
# Define a set of number>>> numbers = set([1, 4, 12, 1])>>> numbersset([1, 4, 12])# Define a set of string>>> words = set(['hey', 'there', '!'])>>> wordsset(['hey', 'there', '!’])
PYTHON REPL
3.3.SETSOPERATORS
# 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
3.3.SETSINSERT & DELETE
>>> scores = set([0.1, 0.4, 0.5])# Insert new element>>> scores.add(0.2)>>> scoresset([0.5, 0.2, 0.4, 0.1])# Delete element>>> scores.remove(0.5)>>> scoresset([0.2, 0.4, 0.1])
PYTHON REPL
3.4.DICTIONARIESINTRODUCTION
- 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.
3.4.DICTIONARIESDEFINE 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
3.4.DICTIONARIESINSERT, 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
3.4.DICTIONARIESINSERT, UPDATE & DELETE (CONTINUED)
# Delete the key and value>>> del d['order']>>> d{'last_order': 6, 'name': 'D D'}
PYTHON REPL
3.5.BOOLEANINTRODUCTION
- Represent the truth value.- Values that considered as False: None, False, zero of any
numeric type, any empty sequences and any empty dictionaries.
4.COMPARISON OPERATORSINTRODUCTION
- 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
.
4.1.COMPARISON OPERATORSLOGICAL
# Define the boolean value>>> a = True; b = False# Logical and>>> a and aTrue>>> a and bFalse# Logical or>>> a or bTrue>>> b or bFalse
PYTHON REPL
4.1.COMPARISON OPERATORSLOGICAL (CONTINUED)
# Compound>>> ((a and a) or b)True>>> ((a and b) and a)False# Negation>>> not aFalse
PYTHON REPL
4.2.COMPARISON OPERATORSIDENTITY
>>> a = 1# Identity>>> a is 1True# Non-Identity>>> a is ‘1’False
PYTHON REPL
4.3.COMPARISON OPERATORSARITHMETIC
>>> 1 < 2True>>> 1 >= 5False>>> 1 == 1True>>> 1 != 2True
PYTHON REPL
5.CONTROL FLOWINTRODUCTION
- 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.
5.1.CONTROL FLOWIF-ELSE
is_exists = Trueif is_exists:
print 'Exists: true’else:
print 'Exists: false'
if_else.py
5.1.CONTROL FLOWIF-ELSE (CONTINUED)
% python if_else.py Exists: true
TERMINAL
5.2.CONTROL FLOWFOR-LOOP
# Basicfor i in xrange(2):
print 'index:', I
# Iterate on sequencesscores = [0.2, 0.5]for score in scores:
print 'score:', score
for_loop.py
5.2.CONTROL FLOWFOR-LOOP (CONTINUED)
# Iterate on dictionariesdata = {'name': 'DSW', 'type': 'camp'}for key in data:
value = data[key] print key, ':', value
for_loop.py
5.2.CONTROL FLOWFOR-LOOP (CONTINUED)
% python for_loop.py index: 0index: 1score: 0.2score: 0.5type : campname : DSW
TERMINAL
5.3.CONTROL FLOWWHILE-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
5.3.CONTROL FLOWWHILE-LOOP (CONTINUED)
# Stop the loop if i>=10i = 0while i < 10:
print 'i:', i i += 1
while_loop.py
5.3.CONTROL FLOWWHILE-LOOP (CONTINUED)
% python while_loop.py i: 0i: 1i: 2i: 3i: 4i: 5i: 6i: 7i: 8i: 9
TERMINAL
6.FUNCTIONINTRODUCTION
- 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
6.FUNCTIONEXAMPLE
# Define a sum functiondef sum(a, b):
return a + b
# Use the functionprint sum(12, 12)
sum.py
6.FUNCTIONEXAMPLE
# Define a sum functiondef sum(a, b):
return a + b
# Use the functionprint sum(12, 12)
sum.py
6.FUNCTIONEXAMPLE OUTPUT
% python sum.py 24
TERMINAL
7.CLASSINTRODUCTION
- 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 propertyself.property_name= arg1
# Define a method or function that read or # update the propertydef method_name(self, arg…):
# Define here
7.CLASSEXAMPLE
class Book: def __init__(self, title):
self.name = titleself.is_borrowed = False
def borrow(self): self.is_borrowed = True
book.py
7.CLASSEXAMPLE (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 objectprint 'Borrow the book.' b.borrow() print 'Book title:', b.title print 'Book status: borrowed=', b.is_borrowed
book.py
7.CLASSEXAMPLE OUTPUT
% python book.py Book title: Hunger gamesBook status: borrowed= FalseBorrow the book.Book title: Hunger gamesBook status: borrowed= True
TERMINAL
8.MODULEINTRODUCTION
- 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.
8.MODULEEXAMPLE
# Import Book class from module book.pyfrom book import Bookif __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
8.MODULEEXAMPLE OUTPUT
% python store.py Book title: Book #0Book title: Book #1Book title: Book #2Book title: Book #3Book title: Book #4Book title: Book #5Book title: Book #6Book title: Book #7Book title: Book #8Book title: Book #9
store.py
Introduction
9.TENSORFLOWINTRODUCTION
- 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.
10.TENSORFLOWPROGRAMMING 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.
10.TENSORFLOWPROGRAMMING 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
𝑏𝑙
11.TENSORFLOWIMPLEMENTATION IN TENSORFLOW
import numpy as npimport tensorflow as tf# Initialize required variablesx_i = np.random.random(size=(32, 256))
# Create the computation graphb = 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
11.TENSORFLOWIMPLEMENTATION IN TENSORFLOW
# Run the computation graph within new sessionsess = tf.Session()sess.run(tf.global_variables_initializer())# Fetch h_i and feed x_isess.run(h_i, {x: x_i})
forward_prop.py
Notes available here: https://github.com/pyk/talks