methods in computational linguistics ii queens college lecture 8: dynamic programming
TRANSCRIPT
![Page 1: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/1.jpg)
Methods in Computational Linguistics II
Queens College
Lecture 8: Dynamic Programming
![Page 2: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/2.jpg)
2
Today
• Inheritance• Recursion• Dynamic Programming
• Machine Learning Primer
![Page 3: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/3.jpg)
3
Inheritance
• Objects can define relationships between objects.
• Membership operations allow for the representation of “has-a”, “has-many”, and arbitrary property relationships.
• Inheritance relationships allow for the representation of “is-a” relationships.
![Page 4: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/4.jpg)
4
Subclasses and Inheritance
class Base(object): def __init__(self): print “Base init”
class Derived(Base): def __init__(self): super(Derived, self).__init__()
![Page 5: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/5.jpg)
5
Shape Example
Shape
Rectangle Triangle Circle
Square
![Page 6: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/6.jpg)
6
Trees
• Binary Trees are a commonly used data structure.
• The core element of a tree is a node
• These nodes can contain values, and include pointers to one or more children, that are differentiated as “left” and “right”
![Page 7: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/7.jpg)
7
Tree Example
5
a
xyz15
The
![Page 8: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/8.jpg)
8
Binary Search Trees
• Binary Search trees have the properties– the value (or key), of any node is greater than
the value of its left child– the value of any node is less than the value of
its right child.
![Page 9: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/9.jpg)
9
Binary Search Tree Example
5
2
41
9
What does this have to do with Binary Search?
![Page 10: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/10.jpg)
10
Tree class
• What does a tree class require?
![Page 11: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/11.jpg)
11
Graphs
• Graphs are similar to Trees
• Graphs have:– Nodes– Edges
• Nodes can contain values• Edges connect two notes, and can also
have a value.
![Page 12: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/12.jpg)
12
Graph Example
5
2
41
9
a
a
b
c
cd
![Page 13: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/13.jpg)
13
What does a Graph class require?
![Page 14: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/14.jpg)
14
In class coding
• Using an object in a program• Initializing data in an object• Objects that you can iterate over
• Look at the Shape class• Look at the Tree class
![Page 15: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/15.jpg)
15
Recursion
• A function that calls itself is called a recursive function.
• A good recursive function must include – A stopping condition– Modification
![Page 16: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/16.jpg)
16
Example of Recursion
• Print every element of a tree.
• Search for an entry in a tree.
• Print every element of a graph.
• Search in a graph.
![Page 17: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/17.jpg)
17
Dynamic Programming
• Fibonacci Numbers.
• F(x) = F(x-1) + F(x-2)• F(2) = 1• F(1) = 1
![Page 18: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/18.jpg)
18
Recursive Fibonacci
• F(6) = F(5) + F(4)• F(5) = F(4) + F(3)• F(4) = F(3) + F(2)• F(3) = F(2) + F(1)• F(4) = F(3) + F(2)• F(3) = F(2) + F(1)• F(3) = F(2) + F(1)• F(4) = F(3) + F(2)• F(3) = F(2) + F(1)
![Page 19: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/19.jpg)
19
Or do it the smart way
• F(1) = 1• F(2) = 1• F(3) = F(2) + F(1) = 2• F(4) = F(3) + F(2) = 3• F(5) = F(4) + F(3) = 5• F(6) = F(5) + F(4) = 8
![Page 20: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/20.jpg)
20
Dynamic Programming
• Optimal Substructure• Repeated Subproblems
![Page 21: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/21.jpg)
21
Viterbi Decoding
• We know how likely we are to be in a state.
• We know how likely we are to transition from one state to another.
• Find the best state sequence.
![Page 22: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/22.jpg)
22
Lattice Every node is is labeled with P(x|k)
Every edge is labeled with a transition probability aij
![Page 23: Methods in Computational Linguistics II Queens College Lecture 8: Dynamic Programming](https://reader035.vdocuments.mx/reader035/viewer/2022070403/56649f295503460f94c434d1/html5/thumbnails/23.jpg)
23
Next Time
• Machine Learning– A primer on machine learning and its role in
Computational Linguistics.– Classification in NLTK (after break)