Machine Learning : Classification (# 1)

0 downloads 0 Views 89KB Size Report
Mar 21, 2018 - 2 # the data X and weight matrix w are randomly generated from a. 3 # standard ... import numpy as np. 7 ... 17 w = np.random.randn(D + 1). 18.
Machine Learning : Classification (# 1) Indrazno Siradjuddin March 21, 2018

1 1.1 1 2 3

Codes Sigmoid Function

# d e m o n s t r a t e s how t o c a l c u l a t e t h e ou tp ut o f a l o g i s t i c u n i t u s i n g numpy . # t h e data X and w e i g h t m a t r i x w a r e randomly g e n e r a t e d from a # s t a n d a r d normal d i s t r i b u t i o n .

4 5 6

im po rt numpy a s np

7 8 9

N = 100 D = 2

10 11 12 13 14 15

X = np . random . randn (N,D) # o n e s = np . a r r a y ( [ [ 1 ] ∗ N ] ) . T # o l d o n e s = np . o n e s ( (N, 1 ) ) Xb = np . c o n c a t e n a t e ( ( ones , X) , a x i s =1)

16 17

w = np . random . randn (D + 1 )

18 19

z = Xb . dot (w)

20 21 22

def sigmoid ( z ) : r e t u r n 1 / ( 1 + np . exp(−z ) )

23 24

print ( sigmoid ( z ) )

Program 1: Program Python Sigmoid Function

1.2 1 2

Cross Entropy Error Function

# d e m o n s t r a t e s how t o c a l c u l a t e t h e c r o s s −e n t r o p y e r r o r f u n c t i o n # i n numpy .

3 4

im po rt numpy a s np

5 6 7

N = 100 D = 2

8 9 10

X = np . random . randn (N,D)

11 12 13

# c e n t e r t h e f i r s t 50 p o i n t s a t ( −2 , −2) X [ : 5 0 , : ] = X [ : 5 0 , : ] − 2∗ np . o n e s ( ( 5 0 ,D) )

1

14 15 16

# c e n t e r t h e l a s t 50 p o i n t s a t ( 2 , 2 ) X [ 5 0 : , : ] = X [ 5 0 : , : ] + 2∗ np . o n e s ( ( 5 0 ,D) )

17 18 19

# l a b e l s : f i r s t 50 a r e 0 , l a s t 50 a r e 1 T = np . a r r a y ( [ 0 ] ∗ 5 0 + [ 1 ] ∗ 5 0 )

20 21 22 23 24

# add a column o f o n e s # o n e s = np . a r r a y ( [ [ 1 ] ∗ N ] ) . T # o l d o n e s = np . o n e s ( (N, 1 ) ) Xb = np . c o n c a t e n a t e ( ( ones , X) , a x i s =1)

25 26 27

# randomly i n i t i a l i z e t h e w e i g h t s w = np . random . randn (D + 1 )

28 29 30

# c a l c u l a t e t h e model out pu t z = Xb . dot (w)

31 32 33

def sigmoid ( z ) : r e t u r n 1 / ( 1 + np . exp(−z ) )

34 35

Y = sigmoid ( z )

36 37 38 39 40 41 42 43 44 45

# c a l c u l a t e t h e c r o s s −e n t r o p y e r r o r d e f c r o s s e n t r o p y (T, Y) : E = 0 f o r i i n r a n g e ( l e n (T) ) : i f T [ i ] == 1 : E −= np . l o g (Y[ i ] ) else : E −= np . l o g ( 1 − Y[ i ] ) return E

46 47

p r i n t ( c r o s s e n t r o p y (T, Y) )

48 49 50

# t r y i t with our c l o s e d −form s o l u t i o n w = np . a r r a y ( [ 0 , 4 , 4 ] )

51 52 53 54

# c a l c u l a t e t h e model out pu t z = Xb . dot (w) Y = sigmoid ( z )

55 56 57

# c a l c u l a t e t h e c r o s s −e n t r o p y e r r o r p r i n t ( c r o s s e n t r o p y (T, Y) )

Program 2: Program Python Cross Entropy Error Function

1.3 1

Gradient Descent: Cross Entropy Error Function

# d e m o n s t r a t e s how t o do g r a d i e n t d e s c e n t with numpy m a t r i c e s .

2 3 4

im po rt numpy a s np im po rt m a t p l o t l i b . p y p l o t a s p l t

5 6 7

N = 100 D = 2

8 9

N p e r c l a s s = N//2

10 11 12

X = np . random . randn (N,D)

13

2

14 15

# c e n t e r t h e f i r s t 50 p o i n t s a t ( −2 , −2) X [ : N p e r c l a s s , : ] = X [ : N p e r c l a s s , : ] − 2∗ np . o n e s ( ( N p e r c l a s s ,D) )

16 17 18

# c e n t e r t h e l a s t 50 p o i n t s a t ( 2 , 2 ) X[ N p e r c l a s s : , : ] = X[ N p e r c l a s s : , : ] + 2∗ np . o n e s ( ( N p e r c l a s s ,D) )

19 20 21

# l a b e l s : f i r s t N per class are 0 , l a s t N per class are 1 T = np . a r r a y ( [ 0 ] ∗ N p e r c l a s s + [ 1 ] ∗ N p e r c l a s s )

22 23 24 25 26

# add a column o f o n e s # o n e s = np . a r r a y ( [ [ 1 ] ∗ N ] ) . T # o l d o n e s = np . o n e s ( (N, 1 ) ) Xb = np . c o n c a t e n a t e ( ( ones , X) , a x i s =1)

27 28 29

# randomly i n i t i a l i z e t h e w e i g h t s w = np . random . randn (D + 1 )

30 31 32

# c a l c u l a t e t h e model out pu t z = Xb . dot (w)

33 34 35

def sigmoid ( z ) : r e t u r n 1 / ( 1 + np . exp(−z ) )

36 37 38

Y = sigmoid ( z )

39 40 41 42 43 44 45 46 47 48

# c a l c u l a t e t h e c r o s s −e n t r o p y e r r o r d e f c r o s s e n t r o p y (T, Y) : E = 0 f o r i i n r a n g e ( l e n (T) ) : i f T [ i ] == 1 : E −= np . l o g (Y[ i ] ) else : E −= np . l o g ( 1 − Y[ i ] ) return E

49 50 51 52 53 54 55

# l e t ’ s do g r a d i e n t d e s c e n t 100 t i m e s learning rate = 0.1 f o r i in range (100) : i f i % 10 == 0 : p r i n t ( c r o s s e n t r o p y (T, Y) )

56 57 58 59

# g r a d i e n t d e s c e n t w e i g h t udpate # w += l e a r n i n g r a t e ∗ np . dot ( (T − Y) . T, Xb) # o l d w += l e a r n i n g r a t e ∗ Xb . T . dot (T − Y)

60 61 62

# recalculate Y Y = s i g m o i d (Xb . dot (w) )

63 64 65

p r i n t ( ” F i n a l w : ” , w)

66 67 68 69 70 71 72

# p l o t t h e data and s e p a r a t i n g l i n e p l t . s c a t t e r (X [ : , 0 ] , X [ : , 1 ] , c=T, s =100 , a l p h a =0.5) x a x i s = np . l i n s p a c e ( −6 , 6 , 1 0 0 ) y a x i s = −(w [ 0 ] + x a x i s ∗w [ 1 ] ) / w [ 2 ] plt . plot ( x axis , y axis ) p l t . show ( )

Program 3: Program Python Cross Entropy Error Function

3