Skip to content

Commit 882d6dd

Browse files
committed
2 parents 5966fdb + acdb292 commit 882d6dd

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

04_Support_Vector_Machines/02_Working_with_Linear_SVMs/02_linear_svm.ipynb

+34-16
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
},
2828
{
2929
"cell_type": "code",
30-
"execution_count": 20,
31-
"metadata": {},
30+
"execution_count": null,
31+
"metadata": {
32+
"collapsed": true
33+
},
3234
"outputs": [],
3335
"source": [
3436
"import matplotlib.pyplot as plt\n",
@@ -48,8 +50,10 @@
4850
},
4951
{
5052
"cell_type": "code",
51-
"execution_count": 21,
52-
"metadata": {},
53+
"execution_count": 2,
54+
"metadata": {
55+
"collapsed": true
56+
},
5357
"outputs": [],
5458
"source": [
5559
"np.random.seed(42)\n",
@@ -66,7 +70,7 @@
6670
},
6771
{
6872
"cell_type": "code",
69-
"execution_count": 22,
73+
"execution_count": 5,
7074
"metadata": {},
7175
"outputs": [],
7276
"source": [
@@ -85,8 +89,10 @@
8589
},
8690
{
8791
"cell_type": "code",
88-
"execution_count": 23,
89-
"metadata": {},
92+
"execution_count": 6,
93+
"metadata": {
94+
"collapsed": true
95+
},
9096
"outputs": [],
9197
"source": [
9298
"train_indices = np.random.choice(len(x_vals),\n",
@@ -108,8 +114,10 @@
108114
},
109115
{
110116
"cell_type": "code",
111-
"execution_count": 24,
112-
"metadata": {},
117+
"execution_count": 7,
118+
"metadata": {
119+
"collapsed": true
120+
},
113121
"outputs": [],
114122
"source": [
115123
"# Declare batch size\n",
@@ -132,15 +140,17 @@
132140
"\n",
133141
"SVM linear model is given by the equation:\n",
134142
"\n",
135-
"$$\\left[ \\frac{1}{n} \\sum_{i=1}^{n} \\max(0, 1 - A \\cdot x - b) \\right] + \\alpha \\cdot ||A||^{2}$$\n",
143+
"$$\\left[ \\frac{1}{n} \\sum_{i=1}^{n} \\max(0, 1 - y_i(A \\cdot x + b)) \\right] + \\alpha \\cdot ||A||^{2}$$\n",
136144
"\n",
137145
"Our loss function will be the above quantity and we will tell TensorFlow to minimize it. Note that $n$ is the number of points (in a batch), $A$ is the hyperplane-normal vector (to solve for), $b$ is the hyperplane-offset (to solve for), and $\\alpha$ is the soft-margin parameter."
138146
]
139147
},
140148
{
141149
"cell_type": "code",
142150
"execution_count": 25,
143-
"metadata": {},
151+
"metadata": {
152+
"collapsed": true
153+
},
144154
"outputs": [],
145155
"source": [
146156
"# Declare model operations\n",
@@ -162,7 +172,9 @@
162172
{
163173
"cell_type": "code",
164174
"execution_count": 26,
165-
"metadata": {},
175+
"metadata": {
176+
"collapsed": true
177+
},
166178
"outputs": [],
167179
"source": [
168180
"# Declare loss function\n",
@@ -188,7 +200,9 @@
188200
{
189201
"cell_type": "code",
190202
"execution_count": 27,
191-
"metadata": {},
203+
"metadata": {
204+
"collapsed": true
205+
},
192206
"outputs": [],
193207
"source": [
194208
"# Declare prediction function\n",
@@ -281,7 +295,9 @@
281295
{
282296
"cell_type": "code",
283297
"execution_count": 29,
284-
"metadata": {},
298+
"metadata": {
299+
"collapsed": true
300+
},
285301
"outputs": [],
286302
"source": [
287303
"# Extract coefficients\n",
@@ -381,7 +397,9 @@
381397
{
382398
"cell_type": "code",
383399
"execution_count": null,
384-
"metadata": {},
400+
"metadata": {
401+
"collapsed": true
402+
},
385403
"outputs": [],
386404
"source": []
387405
}
@@ -402,7 +420,7 @@
402420
"name": "python",
403421
"nbconvert_exporter": "python",
404422
"pygments_lexer": "ipython3",
405-
"version": "3.6.1"
423+
"version": "3.6.5"
406424
}
407425
},
408426
"nbformat": 4,

04_Support_Vector_Machines/02_Working_with_Linear_SVMs/02_linear_svm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
# Split data into train/test sets
3737
train_indices = np.random.choice(len(x_vals),
38-
round(len(x_vals)*0.9),
38+
int(round(len(x_vals)*0.9)),
3939
replace=False)
4040
test_indices = np.array(list(set(range(len(x_vals))) - set(train_indices)))
4141
x_vals_train = x_vals[train_indices]

04_Support_Vector_Machines/03_Reduction_to_Linear_Regression/03_support_vector_regression.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
# Extract Coefficients
8888
[[slope]] = sess.run(A)
8989
[[y_intercept]] = sess.run(b)
90-
[width] = sess.run(epsilon)
90+
width = sess.run(epsilon)
9191

9292
# Get best fit line
9393
best_fit = []

0 commit comments

Comments
 (0)