1
1
# Linear Support Vector Machine: Soft Margin
2
- #----------------------------------
2
+ # ----------------------------------
3
3
#
4
4
# This function shows how to use TensorFlow to
5
5
# create a soft margin SVM
27
27
# iris.data = [(Sepal Length, Sepal Width, Petal Length, Petal Width)]
28
28
iris = datasets .load_iris ()
29
29
x_vals = np .array ([[x [0 ], x [3 ]] for x in iris .data ])
30
- y_vals = np .array ([1 if y == 0 else - 1 for y in iris .target ])
30
+ y_vals = np .array ([1 if y == 0 else - 1 for y in iris .target ])
31
31
32
32
# Split data into train/test sets
33
- train_indices = np .random .choice (len (x_vals ), round (len (x_vals )* 0.8 ), replace = False )
33
+ train_indices = np .random .choice (len (x_vals ),
34
+ round (len (x_vals )* 0.8 ),
35
+ replace = False )
34
36
test_indices = np .array (list (set (range (len (x_vals ))) - set (train_indices )))
35
37
x_vals_train = x_vals [train_indices ]
36
38
x_vals_test = x_vals [test_indices ]
45
47
y_target = tf .placeholder (shape = [None , 1 ], dtype = tf .float32 )
46
48
47
49
# Create variables for linear regression
48
- A = tf .Variable (tf .random_normal (shape = [2 ,1 ]))
49
- b = tf .Variable (tf .random_normal (shape = [1 ,1 ]))
50
+ A = tf .Variable (tf .random_normal (shape = [2 , 1 ]))
51
+ b = tf .Variable (tf .random_normal (shape = [1 , 1 ]))
50
52
51
53
# Declare model operations
52
54
model_output = tf .subtract (tf .matmul (x_data , A ), b )
84
86
rand_x = x_vals_train [rand_index ]
85
87
rand_y = np .transpose ([y_vals_train [rand_index ]])
86
88
sess .run (train_step , feed_dict = {x_data : rand_x , y_target : rand_y })
87
-
89
+
88
90
temp_loss = sess .run (loss , feed_dict = {x_data : rand_x , y_target : rand_y })
89
91
loss_vec .append (temp_loss )
90
-
91
- train_acc_temp = sess .run (accuracy , feed_dict = {x_data : x_vals_train , y_target : np .transpose ([y_vals_train ])})
92
+
93
+ train_acc_temp = sess .run (accuracy , feed_dict = {
94
+ x_data : x_vals_train ,
95
+ y_target : np .transpose ([y_vals_train ])})
92
96
train_accuracy .append (train_acc_temp )
93
-
94
- test_acc_temp = sess .run (accuracy , feed_dict = {x_data : x_vals_test , y_target : np .transpose ([y_vals_test ])})
97
+
98
+ test_acc_temp = sess .run (accuracy , feed_dict = {
99
+ x_data : x_vals_test ,
100
+ y_target : np .transpose ([y_vals_test ])})
95
101
test_accuracy .append (test_acc_temp )
96
-
97
- if (i + 1 )% 100 == 0 :
98
- print ('Step #' + str (i + 1 ) + ' A = ' + str (sess .run (A )) + ' b = ' + str (sess .run (b )))
102
+
103
+ if (i + 1 ) % 100 == 0 :
104
+ print ('Step #{} A = {}, b = {}' .format (
105
+ str (i + 1 ),
106
+ str (sess .run (A )),
107
+ str (sess .run (b ))
108
+ ))
99
109
print ('Loss = ' + str (temp_loss ))
100
110
101
111
# Extract coefficients
110
120
# Get best fit line
111
121
best_fit = []
112
122
for i in x1_vals :
113
- best_fit .append (slope * i + y_intercept )
123
+ best_fit .append (slope * i + y_intercept )
114
124
115
125
# Separate I. setosa
116
- setosa_x = [d [1 ] for i ,d in enumerate (x_vals ) if y_vals [i ]== 1 ]
117
- setosa_y = [d [0 ] for i ,d in enumerate (x_vals ) if y_vals [i ]== 1 ]
118
- not_setosa_x = [d [1 ] for i ,d in enumerate (x_vals ) if y_vals [i ]== - 1 ]
119
- not_setosa_y = [d [0 ] for i ,d in enumerate (x_vals ) if y_vals [i ]== - 1 ]
126
+ setosa_x = [d [1 ] for i , d in enumerate (x_vals ) if y_vals [i ] == 1 ]
127
+ setosa_y = [d [0 ] for i , d in enumerate (x_vals ) if y_vals [i ] == 1 ]
128
+ not_setosa_x = [d [1 ] for i , d in enumerate (x_vals ) if y_vals [i ] == - 1 ]
129
+ not_setosa_y = [d [0 ] for i , d in enumerate (x_vals ) if y_vals [i ] == - 1 ]
120
130
121
131
# Plot data and line
122
132
plt .plot (setosa_x , setosa_y , 'o' , label = 'I. setosa' )
143
153
plt .title ('Loss per Generation' )
144
154
plt .xlabel ('Generation' )
145
155
plt .ylabel ('Loss' )
146
- plt .show ()
156
+ plt .show ()
0 commit comments