added a simple svm example

This commit is contained in:
mhjensen
2019-11-16 22:45:50 +01:00
parent 59cd80d31c
commit a405c5a10a
35 changed files with 2033 additions and 1819 deletions
+42 -38
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -170,7 +174,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Nov 15, 2019</h4></center> <!-- date -->
<center><h4>Nov 16, 2019</h4></center> <!-- date -->
<br>
<p>
@@ -194,7 +198,7 @@ MathJax.Hub.Config({
<li><a href="._svm-bs008.html">9</a></li>
<li><a href="._svm-bs009.html">10</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+41 -37
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -196,7 +200,7 @@ unlikely that we can separate classes easily by say straight lines.
<li><a href="._svm-bs009.html">10</a></li>
<li><a href="._svm-bs010.html">11</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs002.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+41 -37
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -253,7 +257,7 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._svm-bs010.html">11</a></li>
<li><a href="._svm-bs011.html">12</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs003.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+41 -37
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -194,7 +198,7 @@ $$
<li><a href="._svm-bs011.html">12</a></li>
<li><a href="._svm-bs012.html">13</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs004.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+41 -37
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -210,7 +214,7 @@ When we try to separate hyperplanes, if it exists, we can use it to construct a
<li><a href="._svm-bs012.html">13</a></li>
<li><a href="._svm-bs013.html">14</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs005.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+41 -37
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -196,7 +200,7 @@ for our data sample.
<li><a href="._svm-bs013.html">14</a></li>
<li><a href="._svm-bs014.html">15</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs006.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+41 -37
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -192,7 +196,7 @@ $$
<li><a href="._svm-bs014.html">15</a></li>
<li><a href="._svm-bs015.html">16</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs007.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+41 -37
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -196,7 +200,7 @@ $$
<li><a href="._svm-bs015.html">16</a></li>
<li><a href="._svm-bs016.html">17</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs008.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+42 -48
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -154,7 +158,7 @@ MathJax.Hub.Config({
<h2 id="___sec7" class="anchor">Solving the equations </h2>
<p>
We can now use the Newton-Raphson method or gradient descent to solve the equations
We can now use the Newton-Raphson method or different variants of the gradient descent family (from plain gradient descent to various stochastic gradient descent approaches) to solve the equations
$$
b \leftarrow b +\eta \frac{\partial C}{\partial b},
$$
@@ -166,16 +170,6 @@ $$
where \( \eta \) is our by now well-known learning rate.
<p>
There are however problems with this approach, although it looks
pretty straightforward to implement. In case we separate our data into
two distinct classes, we may up with many possible lines, as indicated
in the figure and shown by running the following program. For small
gaps between the entries, we may also end up needing many iterations
before the solutions converge and if the data cannot be separated
properly into two distinct classes, we may not experience a converge
at all.
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -200,7 +194,7 @@ at all.
<li><a href="._svm-bs016.html">17</a></li>
<li><a href="._svm-bs017.html">18</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs009.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+49 -69
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,42 +155,18 @@ MathJax.Hub.Config({
<a name="part0009"></a>
<!-- !split -->
<h2 id="___sec8" class="anchor">A better approach </h2>
<h2 id="___sec8" class="anchor">Code Example </h2>
<p>
A better approach is rather to try to define a large margin between
the two classes (if they are well separated from the beginning).
The equations we discussed above can be coded rather easily (the
framework is rather similar to what we developed for logistic
regression). We use the standard moons example to set up our data
set. We want to find a line which separates the two classes
<p>
Thus, we wish to find a margin \( M \) with \( \boldsymbol{w} \) normalized to
\( \vert\vert \boldsymbol{w}\vert\vert =1 \) subject to the condition
$$
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq M \hspace{0.1cm}\forall i=1,2,\dots, p.
$$
All points are thus at a signed distance from the decision boundary defined by the line \( L \). The parameters \( b \) and \( w_1 \) and \( w_2 \) define this line.
<p>
We seek thus the largest value \( M \) defined by
$$
\frac{1}{\vert \vert \boldsymbol{w}\vert\vert}y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq M \hspace{0.1cm}\forall i=1,2,\dots, n,
$$
or just
$$
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq M\vert \vert \boldsymbol{w}\vert\vert \hspace{0.1cm}\forall i.
$$
If we scale the equation so that \( \vert \vert \boldsymbol{w}\vert\vert = 1/M \), we have to find the minimum of
\( \boldsymbol{w}^T\boldsymbol{w}=\vert \vert \boldsymbol{w}\vert\vert \) (the norm) subject to the condition
$$
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq 1 \hspace{0.1cm}\forall i.
$$
<p>
We have thus defined our margin as the invers of the norm of \( \boldsymbol{w} \). We want to minimize the norm in order to have a as large as possible margin \( M \). Before we proceed, we need to remind ourselves about Lagrangian multipliers.
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -212,7 +192,7 @@ We have thus defined our margin as the invers of the norm of \( \boldsymbol{w} \
<li><a href="._svm-bs017.html">18</a></li>
<li><a href="._svm-bs018.html">19</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs010.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+49 -79
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,52 +155,18 @@ MathJax.Hub.Config({
<a name="part0010"></a>
<!-- !split -->
<h2 id="___sec9" class="anchor">A quick reminder on Lagrangian multipliers </h2>
<h2 id="___sec9" class="anchor">Problems with the Simpler Approach </h2>
<p>
Consider a function of three independent variables \( f(x,y,z) \) . For the function \( f \) to be an
extreme we have
$$
df=0.
$$
A necessary and sufficient condition is
$$
\frac{\partial f}{\partial x} =\frac{\partial f}{\partial y}=\frac{\partial f}{\partial z}=0,
$$
due to
$$
df = \frac{\partial f}{\partial x}dx+\frac{\partial f}{\partial y}dy+\frac{\partial f}{\partial z}dz.
$$
In many problems the variables \( x,y,z \) are often subject to constraints (such as those above for the margin)
so that they are no longer all independent. It is possible at least in principle to use each
constraint to eliminate one variable
and to proceed with a new and smaller set of independent varables.
There are however problems with this approach, although it looks
pretty straightforward to implement. When running the above code, we see that we can easily end up with many diffeent lines which separate the two classes.
<p>
The use of so-called Lagrangian multipliers is an alternative technique when the elimination
of variables is incovenient or undesirable. Assume that we have an equation of constraint on
the variables \( x,y,z \)
$$
\phi(x,y,z) = 0,
$$
resulting in
$$
d\phi = \frac{\partial \phi}{\partial x}dx+\frac{\partial \phi}{\partial y}dy+\frac{\partial \phi}{\partial z}dz =0.
$$
Now we cannot set anymore
$$
\frac{\partial f}{\partial x} =\frac{\partial f}{\partial y}=\frac{\partial f}{\partial z}=0,
$$
if \( df=0 \) is wanted
because there are now only two independent variables! Assume \( x \) and \( y \) are the independent
variables.
Then \( dz \) is no longer arbitrary.
For small
gaps between the entries, we may also end up needing many iterations
before the solutions converge and if the data cannot be separated
properly into two distinct classes, we may not experience a converge
at all.
<p>
<p>
@@ -224,7 +194,7 @@ Then \( dz \) is no longer arbitrary.
<li><a href="._svm-bs018.html">19</a></li>
<li><a href="._svm-bs019.html">20</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs011.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+67 -64
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,45 +155,44 @@ MathJax.Hub.Config({
<a name="part0011"></a>
<!-- !split -->
<h2 id="___sec10" class="anchor">Adding the muliplier </h2>
<h2 id="___sec10" class="anchor">A better approach </h2>
<p>
However, we can add to
A better approach is rather to try to define a large margin between
the two classes (if they are well separated from the beginning).
<p>
Thus, we wish to find a margin \( M \) with \( \boldsymbol{w} \) normalized to
\( \vert\vert \boldsymbol{w}\vert\vert =1 \) subject to the condition
$$
df = \frac{\partial f}{\partial x}dx+\frac{\partial f}{\partial y}dy+\frac{\partial f}{\partial z}dz,
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq M \hspace{0.1cm}\forall i=1,2,\dots, p.
$$
a multiplum of \( d\phi \), viz. \( \lambda d\phi \), resulting in
All points are thus at a signed distance from the decision boundary defined by the line \( L \). The parameters \( b \) and \( w_1 \) and \( w_2 \) define this line.
<p>
We seek thus the largest value \( M \) defined by
$$
df+\lambda d\phi = (\frac{\partial f}{\partial z}+\lambda
\frac{\partial \phi}{\partial x})dx+(\frac{\partial f}{\partial y}+\lambda\frac{\partial \phi}{\partial y})dy+
(\frac{\partial f}{\partial z}+\lambda\frac{\partial \phi}{\partial z})dz =0.
\frac{1}{\vert \vert \boldsymbol{w}\vert\vert}y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq M \hspace{0.1cm}\forall i=1,2,\dots, n,
$$
Our multiplier is chosen so that
or just
$$
\frac{\partial f}{\partial z}+\lambda\frac{\partial \phi}{\partial z} =0.
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq M\vert \vert \boldsymbol{w}\vert\vert \hspace{0.1cm}\forall i.
$$
If we scale the equation so that \( \vert \vert \boldsymbol{w}\vert\vert = 1/M \), we have to find the minimum of
\( \boldsymbol{w}^T\boldsymbol{w}=\vert \vert \boldsymbol{w}\vert\vert \) (the norm) subject to the condition
$$
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq 1 \hspace{0.1cm}\forall i.
$$
<p>
We need to remember that we took \( dx \) and \( dy \) to be arbitrary and thus we must have
$$
\frac{\partial f}{\partial x}+\lambda\frac{\partial \phi}{\partial x} =0,
$$
and
$$
\frac{\partial f}{\partial y}+\lambda\frac{\partial \phi}{\partial y} =0.
$$
When all these equations are satisfied, \( df=0 \). We have four unknowns, \( x,y,z \) and
\( \lambda \). Actually we want only \( x,y,z \), \( \lambda \) needs not to be determined,
it is therefore often called
Lagrange's undetermined multiplier.
If we have a set of constraints \( \phi_k \) we have the equations
$$
\frac{\partial f}{\partial x_i}+\sum_k\lambda_k\frac{\partial \phi_k}{\partial x_i} =0.
$$
We have thus defined our margin as the invers of the norm of
\( \boldsymbol{w} \). We want to minimize the norm in order to have a as large as
possible margin \( M \). Before we proceed, we need to remind ourselves
about Lagrangian multipliers.
<p>
<p>
@@ -217,7 +220,7 @@ $$
<li><a href="._svm-bs019.html">20</a></li>
<li><a href="._svm-bs020.html">21</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs012.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+71 -58
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,43 +155,52 @@ MathJax.Hub.Config({
<a name="part0012"></a>
<!-- !split -->
<h2 id="___sec11" class="anchor">Setting up the problem </h2>
In order to solve the above problem, we define the following Lagrangian function to be minimized
$$
{\cal L}(\lambda,b,\boldsymbol{w})=\frac{1}{2}\boldsymbol{w}^T\boldsymbol{w}-\sum_{i=1}^n\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)-1\right],
$$
where \( \lambda_i \) is a so-called Lagrange multiplier subject to the condition \( \lambda_i \geq 0 \).
<h2 id="___sec11" class="anchor">A quick reminder on Lagrangian multipliers </h2>
<p>
Taking the derivatives with respect to \( b \) and \( \boldsymbol{w} \) we obtain
Consider a function of three independent variables \( f(x,y,z) \) . For the function \( f \) to be an
extreme we have
$$
\frac{\partial {\cal L}}{\partial b} = -\sum_{i} \lambda_iy_i=0,
df=0.
$$
and
A necessary and sufficient condition is
$$
\frac{\partial {\cal L}}{\partial \boldsymbol{w}} = 0 = \boldsymbol{w}-\sum_{i} \lambda_iy_i\boldsymbol{x}_i.
\frac{\partial f}{\partial x} =\frac{\partial f}{\partial y}=\frac{\partial f}{\partial z}=0,
$$
Inserting these constraints into the equation for \( {\cal L} \) we obtain
due to
$$
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{x}_j,
df = \frac{\partial f}{\partial x}dx+\frac{\partial f}{\partial y}dy+\frac{\partial f}{\partial z}dz.
$$
subject to the constraints \( \lambda_i\geq 0 \) and \( \sum_i\lambda_iy_i=0 \).
We must in addition satisfy the <a href="https://en.wikipedia.org/wiki/Karush%E2%80%93Kuhn%E2%80%93Tucker_conditions" target="_self">Karush-Kuhn-Tucker</a> (KKT) condition
In many problems the variables \( x,y,z \) are often subject to constraints (such as those above for the margin)
so that they are no longer all independent. It is possible at least in principle to use each
constraint to eliminate one variable
and to proceed with a new and smaller set of independent varables.
<p>
The use of so-called Lagrangian multipliers is an alternative technique when the elimination
of variables is incovenient or undesirable. Assume that we have an equation of constraint on
the variables \( x,y,z \)
$$
\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) -1\right] \hspace{0.1cm}\forall i.
\phi(x,y,z) = 0,
$$
resulting in
$$
d\phi = \frac{\partial \phi}{\partial x}dx+\frac{\partial \phi}{\partial y}dy+\frac{\partial \phi}{\partial z}dz =0.
$$
<ol>
<li> If \( \lambda_i > 0 \), then \( y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1 \) and we say that \( x_i \) is on the boundary.</li>
<li> If \( y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)> 1 \), we say \( x_i \) is not on the boundary and we set \( \lambda_i=0 \).</li>
</ol>
Now we cannot set anymore
$$
\frac{\partial f}{\partial x} =\frac{\partial f}{\partial y}=\frac{\partial f}{\partial z}=0,
$$
When \( \lambda_i > 0 \), the vectors \( \boldsymbol{x}_i \) are called support vectors. They are the vectors closest to the line (or hyperplane) and define the margin \( M \).
if \( df=0 \) is wanted
because there are now only two independent variables! Assume \( x \) and \( y \) are the independent
variables.
Then \( dz \) is no longer arbitrary.
<p>
<p>
@@ -215,7 +228,7 @@ When \( \lambda_i > 0 \), the vectors \( \boldsymbol{x}_i \) are called support
<li><a href="._svm-bs020.html">21</a></li>
<li><a href="._svm-bs021.html">22</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs013.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+72 -49
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,26 +155,45 @@ MathJax.Hub.Config({
<a name="part0013"></a>
<!-- !split -->
<h2 id="___sec12" class="anchor">The problem to solve </h2>
<h2 id="___sec12" class="anchor">Adding the muliplier </h2>
<p>
We can rewrite
However, we can add to
$$
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{x}_j,
df = \frac{\partial f}{\partial x}dx+\frac{\partial f}{\partial y}dy+\frac{\partial f}{\partial z}dz,
$$
and its constraints in terms of a matrix-vector problem where we minimize w.r.t. \( \lambda \) the following problem
a multiplum of \( d\phi \), viz. \( \lambda d\phi \), resulting in
$$
\frac{1}{2} \boldsymbol{\lambda}^T\begin{bmatrix} y_1y_1\boldsymbol{x}_1^T\boldsymbol{x}_1 & y_1y_2\boldsymbol{x}_1^T\boldsymbol{x}_2 & \dots & \dots & y_1y_n\boldsymbol{x}_1^T\boldsymbol{x}_n \\
y_2y_1\boldsymbol{x}_2^T\boldsymbol{x}_1 & y_2y_2\boldsymbol{x}_2^T\boldsymbol{x}_2 & \dots & \dots & y_1y_n\boldsymbol{x}_2^T\boldsymbol{x}_n \\
\dots & \dots & \dots & \dots & \dots \\
\dots & \dots & \dots & \dots & \dots \\
y_ny_1\boldsymbol{x}_n^T\boldsymbol{x}_1 & y_ny_2\boldsymbol{x}_n^T\boldsymbol{x}_2 & \dots & \dots & y_ny_n\boldsymbol{x}_n^T\boldsymbol{x}_n \\
\end{bmatrix}\boldsymbol{\lambda}-\mathbb{1}\boldsymbol{\lambda},
df+\lambda d\phi = (\frac{\partial f}{\partial z}+\lambda
\frac{\partial \phi}{\partial x})dx+(\frac{\partial f}{\partial y}+\lambda\frac{\partial \phi}{\partial y})dy+
(\frac{\partial f}{\partial z}+\lambda\frac{\partial \phi}{\partial z})dz =0.
$$
subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vectors \( \boldsymbol{\lambda} =[\lambda_1,\lambda_2,\dots,\lambda_n] \) and
\( \boldsymbol{y}=[y_1,y_2,\dots,y_n] \).
Our multiplier is chosen so that
$$
\frac{\partial f}{\partial z}+\lambda\frac{\partial \phi}{\partial z} =0.
$$
<p>
We need to remember that we took \( dx \) and \( dy \) to be arbitrary and thus we must have
$$
\frac{\partial f}{\partial x}+\lambda\frac{\partial \phi}{\partial x} =0,
$$
and
$$
\frac{\partial f}{\partial y}+\lambda\frac{\partial \phi}{\partial y} =0.
$$
When all these equations are satisfied, \( df=0 \). We have four unknowns, \( x,y,z \) and
\( \lambda \). Actually we want only \( x,y,z \), \( \lambda \) needs not to be determined,
it is therefore often called
Lagrange's undetermined multiplier.
If we have a set of constraints \( \phi_k \) we have the equations
$$
\frac{\partial f}{\partial x_i}+\sum_k\lambda_k\frac{\partial \phi_k}{\partial x_i} =0.
$$
<p>
<p>
@@ -198,7 +221,7 @@ subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vec
<li><a href="._svm-bs021.html">22</a></li>
<li><a href="._svm-bs022.html">23</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs014.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+63 -52
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,36 +155,43 @@ MathJax.Hub.Config({
<a name="part0014"></a>
<!-- !split -->
<h2 id="___sec13" class="anchor">The last steps </h2>
<h2 id="___sec13" class="anchor">Setting up the problem </h2>
In order to solve the above problem, we define the following Lagrangian function to be minimized
$$
{\cal L}(\lambda,b,\boldsymbol{w})=\frac{1}{2}\boldsymbol{w}^T\boldsymbol{w}-\sum_{i=1}^n\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)-1\right],
$$
where \( \lambda_i \) is a so-called Lagrange multiplier subject to the condition \( \lambda_i \geq 0 \).
<p>
Solving the above problem, yields the values of \( \lambda_i \).
To find the coefficients of your hyperplane we need simply to compute
Taking the derivatives with respect to \( b \) and \( \boldsymbol{w} \) we obtain
$$
\boldsymbol{w}=\sum_{i} \lambda_iy_i\boldsymbol{x}_i.
\frac{\partial {\cal L}}{\partial b} = -\sum_{i} \lambda_iy_i=0,
$$
With our vector \( \boldsymbol{w} \) we can in turn find the value of the intercept \( b \) (here in two dimensions) via
and
$$
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1,
\frac{\partial {\cal L}}{\partial \boldsymbol{w}} = 0 = \boldsymbol{w}-\sum_{i} \lambda_iy_i\boldsymbol{x}_i.
$$
resulting in
Inserting these constraints into the equation for \( {\cal L} \) we obtain
$$
b = \frac{1}{y_i}-\boldsymbol{w}^T\boldsymbol{x}_i,
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{x}_j,
$$
or if we write it out in terms of the support vectors only, with \( N_s \) being their number, we have
subject to the constraints \( \lambda_i\geq 0 \) and \( \sum_i\lambda_iy_i=0 \).
We must in addition satisfy the <a href="https://en.wikipedia.org/wiki/Karush%E2%80%93Kuhn%E2%80%93Tucker_conditions" target="_self">Karush-Kuhn-Tucker</a> (KKT) condition
$$
b = \frac{1}{N_s}\sum_{j\in N_s}\left(y_j-\sum_{i=1}^n\lambda_iy_i\boldsymbol{x}_i^T\boldsymbol{x}_j\right).
\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) -1\right] \hspace{0.1cm}\forall i.
$$
With our hyperplane coefficients we can use our classifier to assign any observation by simply using
$$
y_i = \mathrm{sign}(\boldsymbol{w}^T\boldsymbol{x}_i+b).
$$
Below we discuss how to find the optimal values of \( \lambda_i \). Before we proceed however, we discuss now the so-called soft classifier.
<ol>
<li> If \( \lambda_i > 0 \), then \( y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1 \) and we say that \( x_i \) is on the boundary.</li>
<li> If \( y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)> 1 \), we say \( x_i \) is not on the boundary and we set \( \lambda_i=0 \).</li>
</ol>
When \( \lambda_i > 0 \), the vectors \( \boldsymbol{x}_i \) are called support vectors. They are the vectors closest to the line (or hyperplane) and define the margin \( M \).
<p>
<p>
@@ -208,7 +219,7 @@ Below we discuss how to find the optimal values of \( \lambda_i \). Before we pr
<li><a href="._svm-bs022.html">23</a></li>
<li><a href="._svm-bs023.html">24</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs015.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+53 -60
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,37 +155,26 @@ MathJax.Hub.Config({
<a name="part0015"></a>
<!-- !split -->
<h2 id="___sec14" class="anchor">A soft classifier </h2>
<h2 id="___sec14" class="anchor">The problem to solve </h2>
<p>
Till now, the margin is strictly defined by the support vectors. This defines what is called a hard classifier, that is the margins are well defined.
<p>
Suppose now that classes overlap in feature space, as shown in the
figure here. One way to deal with this problem before we define the
so-called <b>kernel approach</b>, is to allow a kind of slack in the sense
that we allow some points to be on the wrong side of the margin.
<p>
We introduce thus the so-called <b>slack</b> variables \( \boldsymbol{\xi} =[\xi_1,x_2,\dots,x_n] \) and
modify our previous equation
We can rewrite
$$
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1,
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{x}_j,
$$
to
and its constraints in terms of a matrix-vector problem where we minimize w.r.t. \( \lambda \) the following problem
$$
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1-\xi_i,
\frac{1}{2} \boldsymbol{\lambda}^T\begin{bmatrix} y_1y_1\boldsymbol{x}_1^T\boldsymbol{x}_1 & y_1y_2\boldsymbol{x}_1^T\boldsymbol{x}_2 & \dots & \dots & y_1y_n\boldsymbol{x}_1^T\boldsymbol{x}_n \\
y_2y_1\boldsymbol{x}_2^T\boldsymbol{x}_1 & y_2y_2\boldsymbol{x}_2^T\boldsymbol{x}_2 & \dots & \dots & y_1y_n\boldsymbol{x}_2^T\boldsymbol{x}_n \\
\dots & \dots & \dots & \dots & \dots \\
\dots & \dots & \dots & \dots & \dots \\
y_ny_1\boldsymbol{x}_n^T\boldsymbol{x}_1 & y_ny_2\boldsymbol{x}_n^T\boldsymbol{x}_2 & \dots & \dots & y_ny_n\boldsymbol{x}_n^T\boldsymbol{x}_n \\
\end{bmatrix}\boldsymbol{\lambda}-\mathbb{1}\boldsymbol{\lambda},
$$
with the requirement \( \xi_i\geq 0 \). The total violation is now \( \sum_i\xi \).
The value \( \xi_i \) in the constraint the last constraint corresponds to the amount by which the prediction
\( y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1 \) is on the wrong side of its margin. Hence by bounding the sum \( \sum_i \xi_i \),
we bound the total amount by which predictions fall on the wrong side of their margins.
<p>
Misclassifications occur when \( \xi_i > 1 \). Thus bounding the total sum by some value \( C \) bounds in turn the total number of
misclassifications.
subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vectors \( \boldsymbol{\lambda} =[\lambda_1,\lambda_2,\dots,\lambda_n] \) and
\( \boldsymbol{y}=[y_1,y_2,\dots,y_n] \).
<p>
<p>
@@ -209,7 +202,7 @@ misclassifications.
<li><a href="._svm-bs023.html">24</a></li>
<li><a href="._svm-bs024.html">25</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs016.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+54 -70
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,56 +155,36 @@ MathJax.Hub.Config({
<a name="part0016"></a>
<!-- !split -->
<h2 id="___sec15" class="anchor">Soft optmization problem </h2>
<h2 id="___sec15" class="anchor">The last steps </h2>
<p>
This has in turn the consequences that we change our optmization problem to finding the minimum of
Solving the above problem, yields the values of \( \lambda_i \).
To find the coefficients of your hyperplane we need simply to compute
$$
{\cal L}=\frac{1}{2}\boldsymbol{w}^T\boldsymbol{w}-\sum_{i=1}^n\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)-(1-\xi_)\right]+C\sum_{i=1}^n\xi_i-\sum_{i=1}^n\gamma_i\xi_i,
\boldsymbol{w}=\sum_{i} \lambda_iy_i\boldsymbol{x}_i.
$$
subject to
With our vector \( \boldsymbol{w} \) we can in turn find the value of the intercept \( b \) (here in two dimensions) via
$$
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1-\xi_i \hspace{0.1cm}\forall i,
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1,
$$
with the requirement \( \xi_i\geq 0 \).
<p>
Taking the derivatives with respect to \( b \) and \( \boldsymbol{w} \) we obtain
resulting in
$$
\frac{\partial {\cal L}}{\partial b} = -\sum_{i} \lambda_iy_i=0,
b = \frac{1}{y_i}-\boldsymbol{w}^T\boldsymbol{x}_i,
$$
and
or if we write it out in terms of the support vectors only, with \( N_s \) being their number, we have
$$
\frac{\partial {\cal L}}{\partial \boldsymbol{w}} = 0 = \boldsymbol{w}-\sum_{i} \lambda_iy_i\boldsymbol{x}_i,
b = \frac{1}{N_s}\sum_{j\in N_s}\left(y_j-\sum_{i=1}^n\lambda_iy_i\boldsymbol{x}_i^T\boldsymbol{x}_j\right).
$$
and
With our hyperplane coefficients we can use our classifier to assign any observation by simply using
$$
\lambda_i = C-\gamma_i \hspace{0.1cm}\forall i.
y_i = \mathrm{sign}(\boldsymbol{w}^T\boldsymbol{x}_i+b).
$$
Inserting these constraints into the equation for \( {\cal L} \) we obtain the same equation as before
$$
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{x}_j,
$$
but now subject to the constraints \( \lambda_i\geq 0 \), \( \sum_i\lambda_iy_i=0 \) and \( 0\leq\lambda_i \leq C \).
We must in addition satisfy the Karush-Kuhn-Tucker condition which now reads
$$
\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) -(1-\xi_)\right]=0 \hspace{0.1cm}\forall i,
$$
$$
\gamma_i\xi_i = 0,
$$
and
$$
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) -(1-\xi_) \geq 0 \hspace{0.1cm}\forall i.
$$
Below we discuss how to find the optimal values of \( \lambda_i \). Before we proceed however, we discuss now the so-called soft classifier.
<p>
<p>
@@ -228,7 +212,7 @@ $$
<li><a href="._svm-bs024.html">25</a></li>
<li><a href="._svm-bs025.html">26</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs017.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+65 -97
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,76 +155,38 @@ MathJax.Hub.Config({
<a name="part0017"></a>
<!-- !split -->
<h2 id="___sec16" class="anchor">Kernels and non-linearity </h2>
<h2 id="___sec16" class="anchor">A soft classifier </h2>
<p>
The cases we have studied till were all characterized by two classes
with a close to linear separability. The classifiers we have described
so far find linear boundaries in our input feature space. It is
possible to make our procedure more flexible by exploring the feature
space using other basis expansions such higher-order polynomials,
wavelets, splines etc.
Till now, the margin is strictly defined by the support vectors. This defines what is called a hard classifier, that is the margins are well defined.
<p>
If our feature space is not easy to separate, as shown in the figure
here, we can achieve a better separation by introducing more complex
basis functions. The ideal would be, as shown in the next figure, to, via a specific transformation to
obtain a separation between the classes which is almost linear.
Suppose now that classes overlap in feature space, as shown in the
figure here. One way to deal with this problem before we define the
so-called <b>kernel approach</b>, is to allow a kind of slack in the sense
that we allow some points to be on the wrong side of the margin.
<p>
The change of basis, from \( x\rightarrow z=\phi(x) \) leads to the same type of equations to be solved, except that
we need to introduce for example a polynomial transformation to a two-dimensional training set.
We introduce thus the so-called <b>slack</b> variables \( \boldsymbol{\xi} =[\xi_1,x_2,\dots,x_n] \) and
modify our previous equation
$$
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1,
$$
to
$$
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1-\xi_i,
$$
with the requirement \( \xi_i\geq 0 \). The total violation is now \( \sum_i\xi \).
The value \( \xi_i \) in the constraint the last constraint corresponds to the amount by which the prediction
\( y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1 \) is on the wrong side of its margin. Hence by bounding the sum \( \sum_i \xi_i \),
we bound the total amount by which predictions fall on the wrong side of their margins.
<p>
Misclassifications occur when \( \xi_i > 1 \). Thus bounding the total sum by some value \( C \) bounds in turn the total number of
misclassifications.
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">os</span>
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">42</span>)
<span style="color: #408080; font-style: italic"># To plot pretty figures</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;axes.labelsize&#39;</span>] <span style="color: #666666">=</span> <span style="color: #666666">14</span>
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;xtick.labelsize&#39;</span>] <span style="color: #666666">=</span> <span style="color: #666666">12</span>
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;ytick.labelsize&#39;</span>] <span style="color: #666666">=</span> <span style="color: #666666">12</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn</span> <span style="color: #008000; font-weight: bold">import</span> datasets
X1D <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">-4</span>, <span style="color: #666666">4</span>, <span style="color: #666666">9</span>)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
X2D <span style="color: #666666">=</span> np<span style="color: #666666">.</span>c_[X1D, X1D<span style="color: #666666">**2</span>]
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([<span style="color: #666666">0</span>, <span style="color: #666666">0</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">0</span>, <span style="color: #666666">0</span>])
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">11</span>, <span style="color: #666666">4</span>))
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">121</span>)
plt<span style="color: #666666">.</span>grid(<span style="color: #008000">True</span>, which<span style="color: #666666">=</span><span style="color: #BA2121">&#39;both&#39;</span>)
plt<span style="color: #666666">.</span>axhline(y<span style="color: #666666">=0</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">&#39;k&#39;</span>)
plt<span style="color: #666666">.</span>plot(X1D[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==0</span>], np<span style="color: #666666">.</span>zeros(<span style="color: #666666">4</span>), <span style="color: #BA2121">&quot;bs&quot;</span>)
plt<span style="color: #666666">.</span>plot(X1D[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==1</span>], np<span style="color: #666666">.</span>zeros(<span style="color: #666666">5</span>), <span style="color: #BA2121">&quot;g^&quot;</span>)
plt<span style="color: #666666">.</span>gca()<span style="color: #666666">.</span>get_yaxis()<span style="color: #666666">.</span>set_ticks([])
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&quot;$x_1$&quot;</span>, fontsize<span style="color: #666666">=20</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">-4.5</span>, <span style="color: #666666">4.5</span>, <span style="color: #666666">-0.2</span>, <span style="color: #666666">0.2</span>])
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
plt<span style="color: #666666">.</span>grid(<span style="color: #008000">True</span>, which<span style="color: #666666">=</span><span style="color: #BA2121">&#39;both&#39;</span>)
plt<span style="color: #666666">.</span>axhline(y<span style="color: #666666">=0</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">&#39;k&#39;</span>)
plt<span style="color: #666666">.</span>axvline(x<span style="color: #666666">=0</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">&#39;k&#39;</span>)
plt<span style="color: #666666">.</span>plot(X2D[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==0</span>], X2D[:, <span style="color: #666666">1</span>][y<span style="color: #666666">==0</span>], <span style="color: #BA2121">&quot;bs&quot;</span>)
plt<span style="color: #666666">.</span>plot(X2D[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==1</span>], X2D[:, <span style="color: #666666">1</span>][y<span style="color: #666666">==1</span>], <span style="color: #BA2121">&quot;g^&quot;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&quot;$x_1$&quot;</span>, fontsize<span style="color: #666666">=20</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&quot;$x_2$&quot;</span>, fontsize<span style="color: #666666">=20</span>, rotation<span style="color: #666666">=0</span>)
plt<span style="color: #666666">.</span>gca()<span style="color: #666666">.</span>get_yaxis()<span style="color: #666666">.</span>set_ticks([<span style="color: #666666">0</span>, <span style="color: #666666">4</span>, <span style="color: #666666">8</span>, <span style="color: #666666">12</span>, <span style="color: #666666">16</span>])
plt<span style="color: #666666">.</span>plot([<span style="color: #666666">-4.5</span>, <span style="color: #666666">4.5</span>], [<span style="color: #666666">6.5</span>, <span style="color: #666666">6.5</span>], <span style="color: #BA2121">&quot;r--&quot;</span>, linewidth<span style="color: #666666">=3</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">-4.5</span>, <span style="color: #666666">4.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">17</span>])
plt<span style="color: #666666">.</span>subplots_adjust(right<span style="color: #666666">=1</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -246,6 +212,8 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._svm-bs024.html">25</a></li>
<li><a href="._svm-bs025.html">26</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs018.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+75 -56
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,44 +155,56 @@ MathJax.Hub.Config({
<a name="part0018"></a>
<!-- !split -->
<h2 id="___sec17" class="anchor">The equations </h2>
<h2 id="___sec17" class="anchor">Soft optmization problem </h2>
<p>
Suppose we define a polynomial transformation of degree two only (we continue to live in a plane with \( x_i \) and \( y_i \) as variables)
This has in turn the consequences that we change our optmization problem to finding the minimum of
$$
z = \phi(x_i) =\left(x_i^2, y_i^2, \sqrt{2}x_iy_i\right).
{\cal L}=\frac{1}{2}\boldsymbol{w}^T\boldsymbol{w}-\sum_{i=1}^n\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)-(1-\xi_)\right]+C\sum_{i=1}^n\xi_i-\sum_{i=1}^n\gamma_i\xi_i,
$$
subject to
$$
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)=1-\xi_i \hspace{0.1cm}\forall i,
$$
with the requirement \( \xi_i\geq 0 \).
<p>
With our new basis, the equations we solved earlier are basically the same, that is we have now (without the slack option for simplicity)
Taking the derivatives with respect to \( b \) and \( \boldsymbol{w} \) we obtain
$$
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{z}_j,
\frac{\partial {\cal L}}{\partial b} = -\sum_{i} \lambda_iy_i=0,
$$
subject to the constraints \( \lambda_i\geq 0 \), \( \sum_i\lambda_iy_i=0 \), and for the support vectors
and
$$
y_i(\boldsymbol{w}^T\boldsymbol{z}_i+b)= 1 \hspace{0.1cm}\forall i,
\frac{\partial {\cal L}}{\partial \boldsymbol{w}} = 0 = \boldsymbol{w}-\sum_{i} \lambda_iy_i\boldsymbol{x}_i,
$$
from which we also find \( b \).
To compute \( \boldsymbol{z}_i^T\boldsymbol{z}_j \) we define the kerne \( K(\boldsymbol{x}_i,\boldsymbol{x}_j) \) as
and
$$
K(\boldsymbol{x}_i,\boldsymbol{x}_j)=\boldsymbol{z}_i^T\boldsymbol{z}_j= \phi(\boldsymbol{x}_i)^T\phi(\boldsymbol{x}_j).
\lambda_i = C-\gamma_i \hspace{0.1cm}\forall i.
$$
For the above example, the kernel reads
Inserting these constraints into the equation for \( {\cal L} \) we obtain the same equation as before
$$
K(\boldsymbol{x}_i,\boldsymbol{x}_j)=[x_i^2, y_i^2, \sqrt{2}x_iy_i]^T\begin{bmatrix} x_j^2 \\ y_j^2 \\ \sqrt{2}x_jy_j \end{bmatrix}=x_i^2x_j^2+2x_ix_jy_iy_j+y_i^2y_j^2.
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{x}_j,
$$
<p>
We note that this is nothing but the dot product of the two original
vectors \( (\boldsymbol{x}_i^T\boldsymbol{x}_j)^2 \). Instead of thus computing the
product in the Lagrangian of \( \boldsymbol{z}_i^T\boldsymbol{z}_j \) we simply compute
the dot product \( (\boldsymbol{x}_i^T\boldsymbol{x}_j)^2 \). This leads to the so-called
kernel trick and the result leads to the same as if we went through
the trouble of performing the transformation
\( (\phi(\boldsymbol{x}_i)^T\phi(\boldsymbol{x}_j) \) during the SVM calculations.
but now subject to the constraints \( \lambda_i\geq 0 \), \( \sum_i\lambda_iy_i=0 \) and \( 0\leq\lambda_i \leq C \).
We must in addition satisfy the Karush-Kuhn-Tucker condition which now reads
$$
\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) -(1-\xi_)\right]=0 \hspace{0.1cm}\forall i,
$$
$$
\gamma_i\xi_i = 0,
$$
and
$$
y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) -(1-\xi_) \geq 0 \hspace{0.1cm}\forall i.
$$
<p>
<p>
@@ -214,6 +230,9 @@ the trouble of performing the transformation
<li><a href="._svm-bs024.html">25</a></li>
<li><a href="._svm-bs025.html">26</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs027.html">28</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs019.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+108 -65
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,39 +155,76 @@ MathJax.Hub.Config({
<a name="part0019"></a>
<!-- !split -->
<h2 id="___sec18" class="anchor">The problem to solve </h2>
Using our definition of the kernel We can rewrite again the Lagrangian
$$
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{z}_j,
$$
subject to the constraints \( \lambda_i\geq 0 \), \( \sum_i\lambda_iy_i=0 \) in terms of a convex optimization problem
$$
\frac{1}{2} \boldsymbol{\lambda}^T\begin{bmatrix} y_1y_1K(\boldsymbol{x}_1,\boldsymbol{x}_1) & y_1y_2K(\boldsymbol{x}_1,\boldsymbol{x}_2) & \dots & \dots & y_1y_nK(\boldsymbol{x}_1,\boldsymbol{x}_n) \\
y_2y_1K(\boldsymbol{x}_2,\boldsymbol{x}_1) & y_2y_2(\boldsymbol{x}_2,\boldsymbol{x}_2) & \dots & \dots & y_1y_nK(\boldsymbol{x}_2,\boldsymbol{x}_n) \\
\dots & \dots & \dots & \dots & \dots \\
\dots & \dots & \dots & \dots & \dots \\
y_ny_1K(\boldsymbol{x}_n,\boldsymbol{x}_1) & y_ny_2K(\boldsymbol{x}_n\boldsymbol{x}_2) & \dots & \dots & y_ny_nK(\boldsymbol{x}_n,\boldsymbol{x}_n) \\
\end{bmatrix}\boldsymbol{\lambda}-\mathbb{1}\boldsymbol{\lambda},
$$
subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vectors \( \boldsymbol{\lambda} =[\lambda_1,\lambda_2,\dots,\lambda_n] \) and
\( \boldsymbol{y}=[y_1,y_2,\dots,y_n] \).
If we add the slack constants this leads to the additional constraint \( 0\leq \lambda_i \leq C \).
<h2 id="___sec18" class="anchor">Kernels and non-linearity </h2>
<p>
We can rewrite this (see the solutions below) in terms of a convex optimization problem of the type
$$
\begin{align*}
&\mathrm{min}_{\lambda}\hspace{0.2cm} \frac{1}{2}\boldsymbol{\lambda}^T\boldsymbol{P}\boldsymbol{\lambda}+\boldsymbol{q}^T\boldsymbol{\lambda},\\ \nonumber
&\mathrm{subject\hspace{0.1cm}to} \hspace{0.2cm} \boldsymbol{G}\boldsymbol{\lambda} \preceq \boldsymbol{h} \hspace{0.2cm} \wedge \boldsymbol{A}\boldsymbol{\lambda}=f.
\end{align*}
$$
The cases we have studied till were all characterized by two classes
with a close to linear separability. The classifiers we have described
so far find linear boundaries in our input feature space. It is
possible to make our procedure more flexible by exploring the feature
space using other basis expansions such higher-order polynomials,
wavelets, splines etc.
Below we discuss how to solve these equations. Here we note that the matrix \( \boldsymbol{P} \) has matrix elements \( p_{ij}=y_iy_jK(\boldsymbol{x}_i,\boldsymbol{x}_j) \).
Given a kernel \( K \) and the targets \( y_i \) this matrix is easy to set up. The constraint \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \) leads to \( f=0 \) and \( \boldsymbol{A}=\boldsymbol{y} \). How to set up the matrix \( \boldsymbol{G} \) is discussed later. Here note that the inequalities \( 0\leq \lambda_i \leq C \) can be split up into
\( 0\leq \lambda_i \) and \( \lambda_i \leq C \). These two inequalities define then the matrix \( \boldsymbol{G} \) and the vector \( \boldsymbol{h} \).
<p>
If our feature space is not easy to separate, as shown in the figure
here, we can achieve a better separation by introducing more complex
basis functions. The ideal would be, as shown in the next figure, to, via a specific transformation to
obtain a separation between the classes which is almost linear.
<p>
The change of basis, from \( x\rightarrow z=\phi(x) \) leads to the same type of equations to be solved, except that
we need to introduce for example a polynomial transformation to a two-dimensional training set.
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">os</span>
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">42</span>)
<span style="color: #408080; font-style: italic"># To plot pretty figures</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;axes.labelsize&#39;</span>] <span style="color: #666666">=</span> <span style="color: #666666">14</span>
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;xtick.labelsize&#39;</span>] <span style="color: #666666">=</span> <span style="color: #666666">12</span>
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;ytick.labelsize&#39;</span>] <span style="color: #666666">=</span> <span style="color: #666666">12</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn</span> <span style="color: #008000; font-weight: bold">import</span> datasets
X1D <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">-4</span>, <span style="color: #666666">4</span>, <span style="color: #666666">9</span>)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
X2D <span style="color: #666666">=</span> np<span style="color: #666666">.</span>c_[X1D, X1D<span style="color: #666666">**2</span>]
y <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([<span style="color: #666666">0</span>, <span style="color: #666666">0</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">0</span>, <span style="color: #666666">0</span>])
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">11</span>, <span style="color: #666666">4</span>))
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">121</span>)
plt<span style="color: #666666">.</span>grid(<span style="color: #008000">True</span>, which<span style="color: #666666">=</span><span style="color: #BA2121">&#39;both&#39;</span>)
plt<span style="color: #666666">.</span>axhline(y<span style="color: #666666">=0</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">&#39;k&#39;</span>)
plt<span style="color: #666666">.</span>plot(X1D[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==0</span>], np<span style="color: #666666">.</span>zeros(<span style="color: #666666">4</span>), <span style="color: #BA2121">&quot;bs&quot;</span>)
plt<span style="color: #666666">.</span>plot(X1D[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==1</span>], np<span style="color: #666666">.</span>zeros(<span style="color: #666666">5</span>), <span style="color: #BA2121">&quot;g^&quot;</span>)
plt<span style="color: #666666">.</span>gca()<span style="color: #666666">.</span>get_yaxis()<span style="color: #666666">.</span>set_ticks([])
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&quot;$x_1$&quot;</span>, fontsize<span style="color: #666666">=20</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">-4.5</span>, <span style="color: #666666">4.5</span>, <span style="color: #666666">-0.2</span>, <span style="color: #666666">0.2</span>])
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
plt<span style="color: #666666">.</span>grid(<span style="color: #008000">True</span>, which<span style="color: #666666">=</span><span style="color: #BA2121">&#39;both&#39;</span>)
plt<span style="color: #666666">.</span>axhline(y<span style="color: #666666">=0</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">&#39;k&#39;</span>)
plt<span style="color: #666666">.</span>axvline(x<span style="color: #666666">=0</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">&#39;k&#39;</span>)
plt<span style="color: #666666">.</span>plot(X2D[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==0</span>], X2D[:, <span style="color: #666666">1</span>][y<span style="color: #666666">==0</span>], <span style="color: #BA2121">&quot;bs&quot;</span>)
plt<span style="color: #666666">.</span>plot(X2D[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==1</span>], X2D[:, <span style="color: #666666">1</span>][y<span style="color: #666666">==1</span>], <span style="color: #BA2121">&quot;g^&quot;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&quot;$x_1$&quot;</span>, fontsize<span style="color: #666666">=20</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&quot;$x_2$&quot;</span>, fontsize<span style="color: #666666">=20</span>, rotation<span style="color: #666666">=0</span>)
plt<span style="color: #666666">.</span>gca()<span style="color: #666666">.</span>get_yaxis()<span style="color: #666666">.</span>set_ticks([<span style="color: #666666">0</span>, <span style="color: #666666">4</span>, <span style="color: #666666">8</span>, <span style="color: #666666">12</span>, <span style="color: #666666">16</span>])
plt<span style="color: #666666">.</span>plot([<span style="color: #666666">-4.5</span>, <span style="color: #666666">4.5</span>], [<span style="color: #666666">6.5</span>, <span style="color: #666666">6.5</span>], <span style="color: #BA2121">&quot;r--&quot;</span>, linewidth<span style="color: #666666">=3</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">-4.5</span>, <span style="color: #666666">4.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">17</span>])
plt<span style="color: #666666">.</span>subplots_adjust(right<span style="color: #666666">=1</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -207,6 +248,8 @@ Given a kernel \( K \) and the targets \( y_i \) this matrix is easy to set up.
<li><a href="._svm-bs024.html">25</a></li>
<li><a href="._svm-bs025.html">26</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs027.html">28</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs020.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+73 -56
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,33 +155,44 @@ MathJax.Hub.Config({
<a name="part0020"></a>
<!-- !split -->
<h2 id="___sec19" class="anchor">Different kernels and Mercer's theorem </h2>
<h2 id="___sec19" class="anchor">The equations </h2>
<p>
There are several popular kernels being used. These are
<ol>
<li> Linear: \( K(\boldsymbol{x},\boldsymbol{y})=\boldsymbol{x}^T\boldsymbol{y} \),</li>
<li> Polynomial: \( K(\boldsymbol{x},\boldsymbol{y})=(\boldsymbol{x}^T\boldsymbol{y}+\gamma)^d \),</li>
<li> Gaussian Radial Basis Function: \( K(\boldsymbol{x},\boldsymbol{y})=\exp{\left(-\gamma\vert\vert\boldsymbol{x}-\boldsymbol{y}\vert\vert^2\right)} \),</li>
<li> Tanh: \( K(\boldsymbol{x},\boldsymbol{y})=\tanh{(\boldsymbol{x}^T\boldsymbol{y}+\gamma)} \),</li>
</ol>
and many other ones.
Suppose we define a polynomial transformation of degree two only (we continue to live in a plane with \( x_i \) and \( y_i \) as variables)
$$
z = \phi(x_i) =\left(x_i^2, y_i^2, \sqrt{2}x_iy_i\right).
$$
<p>
An important theorem for us is <a href="https://en.wikipedia.org/wiki/Mercer%27s_theorem" target="_self">Mercer's theorem</a>.
The theorem states that if a kernel function \( K \) is symmetric, continuous and leads to a positive semi-definite matrix \( \boldsymbol{P} \) then
there exists a function \( \phi \) that maps \( \boldsymbol{x}_i \) and \( \boldsymbol{x}_j \) into another space
(possibly with much higher dimensions) such that
With our new basis, the equations we solved earlier are basically the same, that is we have now (without the slack option for simplicity)
$$
K(\boldsymbol{x}_i,\boldsymbol{x}_j)=\phi(\boldsymbol{x}_i)^T\phi(\boldsymbol{x}_j).
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{z}_j,
$$
So you can use \( K \) as a kernel since you know \( \phi \) exists, even if
you don&#8217;t know what \( \phi \) is.
Note that some frequently used kernels (such as the Sigmoid kernel) don&#8217;t respect all of Mercer&#8217;s conditions, yet they generally work
well in practice.
subject to the constraints \( \lambda_i\geq 0 \), \( \sum_i\lambda_iy_i=0 \), and for the support vectors
$$
y_i(\boldsymbol{w}^T\boldsymbol{z}_i+b)= 1 \hspace{0.1cm}\forall i,
$$
from which we also find \( b \).
To compute \( \boldsymbol{z}_i^T\boldsymbol{z}_j \) we define the kerne \( K(\boldsymbol{x}_i,\boldsymbol{x}_j) \) as
$$
K(\boldsymbol{x}_i,\boldsymbol{x}_j)=\boldsymbol{z}_i^T\boldsymbol{z}_j= \phi(\boldsymbol{x}_i)^T\phi(\boldsymbol{x}_j).
$$
For the above example, the kernel reads
$$
K(\boldsymbol{x}_i,\boldsymbol{x}_j)=[x_i^2, y_i^2, \sqrt{2}x_iy_i]^T\begin{bmatrix} x_j^2 \\ y_j^2 \\ \sqrt{2}x_jy_j \end{bmatrix}=x_i^2x_j^2+2x_ix_jy_iy_j+y_i^2y_j^2.
$$
<p>
We note that this is nothing but the dot product of the two original
vectors \( (\boldsymbol{x}_i^T\boldsymbol{x}_j)^2 \). Instead of thus computing the
product in the Lagrangian of \( \boldsymbol{z}_i^T\boldsymbol{z}_j \) we simply compute
the dot product \( (\boldsymbol{x}_i^T\boldsymbol{x}_j)^2 \). This leads to the so-called
kernel trick and the result leads to the same as if we went through
the trouble of performing the transformation
\( (\phi(\boldsymbol{x}_i)^T\phi(\boldsymbol{x}_j) \) during the SVM calculations.
<p>
<p>
@@ -201,6 +216,8 @@ well in practice.
<li><a href="._svm-bs024.html">25</a></li>
<li><a href="._svm-bs025.html">26</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs027.html">28</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs021.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+72 -226
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,199 +155,39 @@ MathJax.Hub.Config({
<a name="part0021"></a>
<!-- !split -->
<h2 id="___sec20" class="anchor">The moons example </h2>
<h2 id="___sec20" class="anchor">The problem to solve </h2>
Using our definition of the kernel We can rewrite again the Lagrangian
$$
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{z}_j,
$$
subject to the constraints \( \lambda_i\geq 0 \), \( \sum_i\lambda_iy_i=0 \) in terms of a convex optimization problem
$$
\frac{1}{2} \boldsymbol{\lambda}^T\begin{bmatrix} y_1y_1K(\boldsymbol{x}_1,\boldsymbol{x}_1) & y_1y_2K(\boldsymbol{x}_1,\boldsymbol{x}_2) & \dots & \dots & y_1y_nK(\boldsymbol{x}_1,\boldsymbol{x}_n) \\
y_2y_1K(\boldsymbol{x}_2,\boldsymbol{x}_1) & y_2y_2(\boldsymbol{x}_2,\boldsymbol{x}_2) & \dots & \dots & y_1y_nK(\boldsymbol{x}_2,\boldsymbol{x}_n) \\
\dots & \dots & \dots & \dots & \dots \\
\dots & \dots & \dots & \dots & \dots \\
y_ny_1K(\boldsymbol{x}_n,\boldsymbol{x}_1) & y_ny_2K(\boldsymbol{x}_n\boldsymbol{x}_2) & \dots & \dots & y_ny_nK(\boldsymbol{x}_n,\boldsymbol{x}_n) \\
\end{bmatrix}\boldsymbol{\lambda}-\mathbb{1}\boldsymbol{\lambda},
$$
subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vectors \( \boldsymbol{\lambda} =[\lambda_1,\lambda_2,\dots,\lambda_n] \) and
\( \boldsymbol{y}=[y_1,y_2,\dots,y_n] \).
If we add the slack constants this leads to the additional constraint \( 0\leq \lambda_i \leq C \).
<p>
We can rewrite this (see the solutions below) in terms of a convex optimization problem of the type
$$
\begin{align*}
&\mathrm{min}_{\lambda}\hspace{0.2cm} \frac{1}{2}\boldsymbol{\lambda}^T\boldsymbol{P}\boldsymbol{\lambda}+\boldsymbol{q}^T\boldsymbol{\lambda},\\ \nonumber
&\mathrm{subject\hspace{0.1cm}to} \hspace{0.2cm} \boldsymbol{G}\boldsymbol{\lambda} \preceq \boldsymbol{h} \hspace{0.2cm} \wedge \boldsymbol{A}\boldsymbol{\lambda}=f.
\end{align*}
$$
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">__future__</span> <span style="color: #008000; font-weight: bold">import</span> division, print_function, unicode_literals
Below we discuss how to solve these equations. Here we note that the matrix \( \boldsymbol{P} \) has matrix elements \( p_{ij}=y_iy_jK(\boldsymbol{x}_i,\boldsymbol{x}_j) \).
Given a kernel \( K \) and the targets \( y_i \) this matrix is easy to set up. The constraint \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \) leads to \( f=0 \) and \( \boldsymbol{A}=\boldsymbol{y} \). How to set up the matrix \( \boldsymbol{G} \) is discussed later. Here note that the inequalities \( 0\leq \lambda_i \leq C \) can be split up into
\( 0\leq \lambda_i \) and \( \lambda_i \leq C \). These two inequalities define then the matrix \( \boldsymbol{G} \) and the vector \( \boldsymbol{h} \).
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">42</span>)
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;axes.labelsize&#39;</span>] <span style="color: #666666">=</span> <span style="color: #666666">14</span>
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;xtick.labelsize&#39;</span>] <span style="color: #666666">=</span> <span style="color: #666666">12</span>
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;ytick.labelsize&#39;</span>] <span style="color: #666666">=</span> <span style="color: #666666">12</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn</span> <span style="color: #008000; font-weight: bold">import</span> datasets
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.pipeline</span> <span style="color: #008000; font-weight: bold">import</span> Pipeline
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> LinearSVC
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> make_moons
X, y <span style="color: #666666">=</span> make_moons(n_samples<span style="color: #666666">=100</span>, noise<span style="color: #666666">=0.15</span>, random_state<span style="color: #666666">=42</span>)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">plot_dataset</span>(X, y, axes):
plt<span style="color: #666666">.</span>plot(X[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==0</span>], X[:, <span style="color: #666666">1</span>][y<span style="color: #666666">==0</span>], <span style="color: #BA2121">&quot;bs&quot;</span>)
plt<span style="color: #666666">.</span>plot(X[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==1</span>], X[:, <span style="color: #666666">1</span>][y<span style="color: #666666">==1</span>], <span style="color: #BA2121">&quot;g^&quot;</span>)
plt<span style="color: #666666">.</span>axis(axes)
plt<span style="color: #666666">.</span>grid(<span style="color: #008000">True</span>, which<span style="color: #666666">=</span><span style="color: #BA2121">&#39;both&#39;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&quot;$x_1$&quot;</span>, fontsize<span style="color: #666666">=20</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&quot;$x_2$&quot;</span>, fontsize<span style="color: #666666">=20</span>, rotation<span style="color: #666666">=0</span>)
plot_dataset(X, y, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plt<span style="color: #666666">.</span>show()
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> make_moons
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.pipeline</span> <span style="color: #008000; font-weight: bold">import</span> Pipeline
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> PolynomialFeatures
polynomial_svm_clf <span style="color: #666666">=</span> Pipeline([
(<span style="color: #BA2121">&quot;poly_features&quot;</span>, PolynomialFeatures(degree<span style="color: #666666">=3</span>)),
(<span style="color: #BA2121">&quot;scaler&quot;</span>, StandardScaler()),
(<span style="color: #BA2121">&quot;svm_clf&quot;</span>, LinearSVC(C<span style="color: #666666">=10</span>, loss<span style="color: #666666">=</span><span style="color: #BA2121">&quot;hinge&quot;</span>, random_state<span style="color: #666666">=42</span>))
])
polynomial_svm_clf<span style="color: #666666">.</span>fit(X, y)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">plot_predictions</span>(clf, axes):
x0s <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(axes[<span style="color: #666666">0</span>], axes[<span style="color: #666666">1</span>], <span style="color: #666666">100</span>)
x1s <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(axes[<span style="color: #666666">2</span>], axes[<span style="color: #666666">3</span>], <span style="color: #666666">100</span>)
x0, x1 <span style="color: #666666">=</span> np<span style="color: #666666">.</span>meshgrid(x0s, x1s)
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>c_[x0<span style="color: #666666">.</span>ravel(), x1<span style="color: #666666">.</span>ravel()]
y_pred <span style="color: #666666">=</span> clf<span style="color: #666666">.</span>predict(X)<span style="color: #666666">.</span>reshape(x0<span style="color: #666666">.</span>shape)
y_decision <span style="color: #666666">=</span> clf<span style="color: #666666">.</span>decision_function(X)<span style="color: #666666">.</span>reshape(x0<span style="color: #666666">.</span>shape)
plt<span style="color: #666666">.</span>contourf(x0, x1, y_pred, cmap<span style="color: #666666">=</span>plt<span style="color: #666666">.</span>cm<span style="color: #666666">.</span>brg, alpha<span style="color: #666666">=0.2</span>)
plt<span style="color: #666666">.</span>contourf(x0, x1, y_decision, cmap<span style="color: #666666">=</span>plt<span style="color: #666666">.</span>cm<span style="color: #666666">.</span>brg, alpha<span style="color: #666666">=0.1</span>)
plot_predictions(polynomial_svm_clf, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plot_dataset(X, y, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plt<span style="color: #666666">.</span>show()
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
poly_kernel_svm_clf <span style="color: #666666">=</span> Pipeline([
(<span style="color: #BA2121">&quot;scaler&quot;</span>, StandardScaler()),
(<span style="color: #BA2121">&quot;svm_clf&quot;</span>, SVC(kernel<span style="color: #666666">=</span><span style="color: #BA2121">&quot;poly&quot;</span>, degree<span style="color: #666666">=3</span>, coef0<span style="color: #666666">=1</span>, C<span style="color: #666666">=5</span>))
])
poly_kernel_svm_clf<span style="color: #666666">.</span>fit(X, y)
poly100_kernel_svm_clf <span style="color: #666666">=</span> Pipeline([
(<span style="color: #BA2121">&quot;scaler&quot;</span>, StandardScaler()),
(<span style="color: #BA2121">&quot;svm_clf&quot;</span>, SVC(kernel<span style="color: #666666">=</span><span style="color: #BA2121">&quot;poly&quot;</span>, degree<span style="color: #666666">=10</span>, coef0<span style="color: #666666">=100</span>, C<span style="color: #666666">=5</span>))
])
poly100_kernel_svm_clf<span style="color: #666666">.</span>fit(X, y)
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">11</span>, <span style="color: #666666">4</span>))
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">121</span>)
plot_predictions(poly_kernel_svm_clf, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plot_dataset(X, y, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r&quot;$d=3, r=1, C=5$&quot;</span>, fontsize<span style="color: #666666">=18</span>)
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
plot_predictions(poly100_kernel_svm_clf, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plot_dataset(X, y, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r&quot;$d=10, r=100, C=5$&quot;</span>, fontsize<span style="color: #666666">=18</span>)
plt<span style="color: #666666">.</span>show()
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">gaussian_rbf</span>(x, landmark, gamma):
<span style="color: #008000; font-weight: bold">return</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>gamma <span style="color: #666666">*</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>norm(x <span style="color: #666666">-</span> landmark, axis<span style="color: #666666">=1</span>)<span style="color: #666666">**2</span>)
gamma <span style="color: #666666">=</span> <span style="color: #666666">0.3</span>
x1s <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">-4.5</span>, <span style="color: #666666">4.5</span>, <span style="color: #666666">200</span>)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
x2s <span style="color: #666666">=</span> gaussian_rbf(x1s, <span style="color: #666666">-2</span>, gamma)
x3s <span style="color: #666666">=</span> gaussian_rbf(x1s, <span style="color: #666666">1</span>, gamma)
XK <span style="color: #666666">=</span> np<span style="color: #666666">.</span>c_[gaussian_rbf(X1D, <span style="color: #666666">-2</span>, gamma), gaussian_rbf(X1D, <span style="color: #666666">1</span>, gamma)]
yk <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([<span style="color: #666666">0</span>, <span style="color: #666666">0</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">0</span>, <span style="color: #666666">0</span>])
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">11</span>, <span style="color: #666666">4</span>))
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">121</span>)
plt<span style="color: #666666">.</span>grid(<span style="color: #008000">True</span>, which<span style="color: #666666">=</span><span style="color: #BA2121">&#39;both&#39;</span>)
plt<span style="color: #666666">.</span>axhline(y<span style="color: #666666">=0</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">&#39;k&#39;</span>)
plt<span style="color: #666666">.</span>scatter(x<span style="color: #666666">=</span>[<span style="color: #666666">-2</span>, <span style="color: #666666">1</span>], y<span style="color: #666666">=</span>[<span style="color: #666666">0</span>, <span style="color: #666666">0</span>], s<span style="color: #666666">=150</span>, alpha<span style="color: #666666">=0.5</span>, c<span style="color: #666666">=</span><span style="color: #BA2121">&quot;red&quot;</span>)
plt<span style="color: #666666">.</span>plot(X1D[:, <span style="color: #666666">0</span>][yk<span style="color: #666666">==0</span>], np<span style="color: #666666">.</span>zeros(<span style="color: #666666">4</span>), <span style="color: #BA2121">&quot;bs&quot;</span>)
plt<span style="color: #666666">.</span>plot(X1D[:, <span style="color: #666666">0</span>][yk<span style="color: #666666">==1</span>], np<span style="color: #666666">.</span>zeros(<span style="color: #666666">5</span>), <span style="color: #BA2121">&quot;g^&quot;</span>)
plt<span style="color: #666666">.</span>plot(x1s, x2s, <span style="color: #BA2121">&quot;g--&quot;</span>)
plt<span style="color: #666666">.</span>plot(x1s, x3s, <span style="color: #BA2121">&quot;b:&quot;</span>)
plt<span style="color: #666666">.</span>gca()<span style="color: #666666">.</span>get_yaxis()<span style="color: #666666">.</span>set_ticks([<span style="color: #666666">0</span>, <span style="color: #666666">0.25</span>, <span style="color: #666666">0.5</span>, <span style="color: #666666">0.75</span>, <span style="color: #666666">1</span>])
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&quot;$x_1$&quot;</span>, fontsize<span style="color: #666666">=20</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&quot;Similarity&quot;</span>, fontsize<span style="color: #666666">=14</span>)
plt<span style="color: #666666">.</span>annotate(<span style="color: #BA2121">r&#39;$\mathbf{x}$&#39;</span>,
xy<span style="color: #666666">=</span>(X1D[<span style="color: #666666">3</span>, <span style="color: #666666">0</span>], <span style="color: #666666">0</span>),
xytext<span style="color: #666666">=</span>(<span style="color: #666666">-0.5</span>, <span style="color: #666666">0.20</span>),
ha<span style="color: #666666">=</span><span style="color: #BA2121">&quot;center&quot;</span>,
arrowprops<span style="color: #666666">=</span><span style="color: #008000">dict</span>(facecolor<span style="color: #666666">=</span><span style="color: #BA2121">&#39;black&#39;</span>, shrink<span style="color: #666666">=0.1</span>),
fontsize<span style="color: #666666">=18</span>,
)
plt<span style="color: #666666">.</span>text(<span style="color: #666666">-2</span>, <span style="color: #666666">0.9</span>, <span style="color: #BA2121">&quot;$x_2$&quot;</span>, ha<span style="color: #666666">=</span><span style="color: #BA2121">&quot;center&quot;</span>, fontsize<span style="color: #666666">=20</span>)
plt<span style="color: #666666">.</span>text(<span style="color: #666666">1</span>, <span style="color: #666666">0.9</span>, <span style="color: #BA2121">&quot;$x_3$&quot;</span>, ha<span style="color: #666666">=</span><span style="color: #BA2121">&quot;center&quot;</span>, fontsize<span style="color: #666666">=20</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">-4.5</span>, <span style="color: #666666">4.5</span>, <span style="color: #666666">-0.1</span>, <span style="color: #666666">1.1</span>])
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
plt<span style="color: #666666">.</span>grid(<span style="color: #008000">True</span>, which<span style="color: #666666">=</span><span style="color: #BA2121">&#39;both&#39;</span>)
plt<span style="color: #666666">.</span>axhline(y<span style="color: #666666">=0</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">&#39;k&#39;</span>)
plt<span style="color: #666666">.</span>axvline(x<span style="color: #666666">=0</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">&#39;k&#39;</span>)
plt<span style="color: #666666">.</span>plot(XK[:, <span style="color: #666666">0</span>][yk<span style="color: #666666">==0</span>], XK[:, <span style="color: #666666">1</span>][yk<span style="color: #666666">==0</span>], <span style="color: #BA2121">&quot;bs&quot;</span>)
plt<span style="color: #666666">.</span>plot(XK[:, <span style="color: #666666">0</span>][yk<span style="color: #666666">==1</span>], XK[:, <span style="color: #666666">1</span>][yk<span style="color: #666666">==1</span>], <span style="color: #BA2121">&quot;g^&quot;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&quot;$x_2$&quot;</span>, fontsize<span style="color: #666666">=20</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&quot;$x_3$ &quot;</span>, fontsize<span style="color: #666666">=20</span>, rotation<span style="color: #666666">=0</span>)
plt<span style="color: #666666">.</span>annotate(<span style="color: #BA2121">r&#39;$\phi\left(\mathbf{x}\right)$&#39;</span>,
xy<span style="color: #666666">=</span>(XK[<span style="color: #666666">3</span>, <span style="color: #666666">0</span>], XK[<span style="color: #666666">3</span>, <span style="color: #666666">1</span>]),
xytext<span style="color: #666666">=</span>(<span style="color: #666666">0.65</span>, <span style="color: #666666">0.50</span>),
ha<span style="color: #666666">=</span><span style="color: #BA2121">&quot;center&quot;</span>,
arrowprops<span style="color: #666666">=</span><span style="color: #008000">dict</span>(facecolor<span style="color: #666666">=</span><span style="color: #BA2121">&#39;black&#39;</span>, shrink<span style="color: #666666">=0.1</span>),
fontsize<span style="color: #666666">=18</span>,
)
plt<span style="color: #666666">.</span>plot([<span style="color: #666666">-0.1</span>, <span style="color: #666666">1.1</span>], [<span style="color: #666666">0.57</span>, <span style="color: #666666">-0.1</span>], <span style="color: #BA2121">&quot;r--&quot;</span>, linewidth<span style="color: #666666">=3</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">-0.1</span>, <span style="color: #666666">1.1</span>, <span style="color: #666666">-0.1</span>, <span style="color: #666666">1.1</span>])
plt<span style="color: #666666">.</span>subplots_adjust(right<span style="color: #666666">=1</span>)
plt<span style="color: #666666">.</span>show()
x1_example <span style="color: #666666">=</span> X1D[<span style="color: #666666">3</span>, <span style="color: #666666">0</span>]
<span style="color: #008000; font-weight: bold">for</span> landmark <span style="color: #AA22FF; font-weight: bold">in</span> (<span style="color: #666666">-2</span>, <span style="color: #666666">1</span>):
k <span style="color: #666666">=</span> gaussian_rbf(np<span style="color: #666666">.</span>array([[x1_example]]), np<span style="color: #666666">.</span>array([[landmark]]), gamma)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Phi({}, {}) = {}&quot;</span><span style="color: #666666">.</span>format(x1_example, landmark, k))
rbf_kernel_svm_clf <span style="color: #666666">=</span> Pipeline([
(<span style="color: #BA2121">&quot;scaler&quot;</span>, StandardScaler()),
(<span style="color: #BA2121">&quot;svm_clf&quot;</span>, SVC(kernel<span style="color: #666666">=</span><span style="color: #BA2121">&quot;rbf&quot;</span>, gamma<span style="color: #666666">=5</span>, C<span style="color: #666666">=0.001</span>))
])
rbf_kernel_svm_clf<span style="color: #666666">.</span>fit(X, y)
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
gamma1, gamma2 <span style="color: #666666">=</span> <span style="color: #666666">0.1</span>, <span style="color: #666666">5</span>
C1, C2 <span style="color: #666666">=</span> <span style="color: #666666">0.001</span>, <span style="color: #666666">1000</span>
hyperparams <span style="color: #666666">=</span> (gamma1, C1), (gamma1, C2), (gamma2, C1), (gamma2, C2)
svm_clfs <span style="color: #666666">=</span> []
<span style="color: #008000; font-weight: bold">for</span> gamma, C <span style="color: #AA22FF; font-weight: bold">in</span> hyperparams:
rbf_kernel_svm_clf <span style="color: #666666">=</span> Pipeline([
(<span style="color: #BA2121">&quot;scaler&quot;</span>, StandardScaler()),
(<span style="color: #BA2121">&quot;svm_clf&quot;</span>, SVC(kernel<span style="color: #666666">=</span><span style="color: #BA2121">&quot;rbf&quot;</span>, gamma<span style="color: #666666">=</span>gamma, C<span style="color: #666666">=</span>C))
])
rbf_kernel_svm_clf<span style="color: #666666">.</span>fit(X, y)
svm_clfs<span style="color: #666666">.</span>append(rbf_kernel_svm_clf)
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">11</span>, <span style="color: #666666">7</span>))
<span style="color: #008000; font-weight: bold">for</span> i, svm_clf <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">enumerate</span>(svm_clfs):
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">221</span> <span style="color: #666666">+</span> i)
plot_predictions(svm_clf, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plot_dataset(X, y, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
gamma, C <span style="color: #666666">=</span> hyperparams[i]
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r&quot;$\gamma = {}, C = {}$&quot;</span><span style="color: #666666">.</span>format(gamma, C), fontsize<span style="color: #666666">=16</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -365,6 +209,8 @@ plt<span style="color: #666666">.</span>show()
<li><a href="._svm-bs024.html">25</a></li>
<li><a href="._svm-bs025.html">26</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs027.html">28</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs022.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+64 -52
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,27 +155,33 @@ MathJax.Hub.Config({
<a name="part0022"></a>
<!-- !split -->
<h2 id="___sec21" class="anchor">Mathematical optimization of convex functions </h2>
<h2 id="___sec21" class="anchor">Different kernels and Mercer's theorem </h2>
<p>
A mathematical (quadratic) optimization problem, or just optimization problem, has the form
There are several popular kernels being used. These are
<ol>
<li> Linear: \( K(\boldsymbol{x},\boldsymbol{y})=\boldsymbol{x}^T\boldsymbol{y} \),</li>
<li> Polynomial: \( K(\boldsymbol{x},\boldsymbol{y})=(\boldsymbol{x}^T\boldsymbol{y}+\gamma)^d \),</li>
<li> Gaussian Radial Basis Function: \( K(\boldsymbol{x},\boldsymbol{y})=\exp{\left(-\gamma\vert\vert\boldsymbol{x}-\boldsymbol{y}\vert\vert^2\right)} \),</li>
<li> Tanh: \( K(\boldsymbol{x},\boldsymbol{y})=\tanh{(\boldsymbol{x}^T\boldsymbol{y}+\gamma)} \),</li>
</ol>
and many other ones.
<p>
An important theorem for us is <a href="https://en.wikipedia.org/wiki/Mercer%27s_theorem" target="_self">Mercer's theorem</a>.
The theorem states that if a kernel function \( K \) is symmetric, continuous and leads to a positive semi-definite matrix \( \boldsymbol{P} \) then
there exists a function \( \phi \) that maps \( \boldsymbol{x}_i \) and \( \boldsymbol{x}_j \) into another space
(possibly with much higher dimensions) such that
$$
\begin{align*}
&\mathrm{min}_{\lambda}\hspace{0.2cm} \frac{1}{2}\boldsymbol{\lambda}^T\boldsymbol{P}\boldsymbol{\lambda}+\boldsymbol{q}^T\boldsymbol{\lambda},\\ \nonumber
&\mathrm{subject\hspace{0.1cm}to} \hspace{0.2cm} \boldsymbol{G}\boldsymbol{\lambda} \preceq \boldsymbol{h} \wedge \boldsymbol{A}\boldsymbol{\lambda}=f.
\end{align*}
K(\boldsymbol{x}_i,\boldsymbol{x}_j)=\phi(\boldsymbol{x}_i)^T\phi(\boldsymbol{x}_j).
$$
subject to some constraints for say a selected set \( i=1,2,\dots, n \).
In our case we are optimizing with respect to the Lagrangian multipliers \( \lambda_i \), and the
vector \( \boldsymbol{\lambda}=[\lambda_1, \lambda_2,\dots, \lambda_n] \) is the optimization variable we are dealing with.
<p>
In our case we are particularly interested in a class of optimization problems called convex optmization problems.
In our discussion on gradient descent methods we discussed at length the definition of a convex function.
<p>
Convex optimization problems play a central role in applied mathematics and we recommend strongly <a href="http://web.stanford.edu/~boyd/cvxbook/" target="_self">Boyd and Vandenberghe's text on the topics</a>.
So you can use \( K \) as a kernel since you know \( \phi \) exists, even if
you don&#8217;t know what \( \phi \) is.
Note that some frequently used kernels (such as the Sigmoid kernel) don&#8217;t respect all of Mercer&#8217;s conditions, yet they generally work
well in practice.
<p>
<p>
@@ -193,6 +203,8 @@ Convex optimization problems play a central role in applied mathematics and we r
<li><a href="._svm-bs024.html">25</a></li>
<li><a href="._svm-bs025.html">26</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs027.html">28</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs023.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+231 -55
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,29 +155,199 @@ MathJax.Hub.Config({
<a name="part0023"></a>
<!-- !split -->
<h2 id="___sec22" class="anchor">How do we solve these problems? </h2>
<p>
If we use Python as programming language and wish to venture beyond
<b>scikit-learn</b>, <b>tensorflow</b> and similar software which makes our
lives so much easier, we need to dive into the wonderful world of
quadratic programming. We can, if we wish, solve the minimization
problem using say standard gradient methods or conjugate gradient
methods. However, these methods tend to exhibit a rather slow
converge. So, welcome to the promised land of quadratic programming.
<p>
The functions we need are contained in the quadratic programming package <b>CVXOPT</b> and we need to import it together with <b>numpy</b> as
<h2 id="___sec22" class="anchor">The moons example </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">cvxopt</span>
</pre></div>
<p>
This will make our life much easier. You don't need t write your own optimizer.
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">__future__</span> <span style="color: #008000; font-weight: bold">import</span> division, print_function, unicode_literals
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>seed(<span style="color: #666666">42</span>)
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;axes.labelsize&#39;</span>] <span style="color: #666666">=</span> <span style="color: #666666">14</span>
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;xtick.labelsize&#39;</span>] <span style="color: #666666">=</span> <span style="color: #666666">12</span>
plt<span style="color: #666666">.</span>rcParams[<span style="color: #BA2121">&#39;ytick.labelsize&#39;</span>] <span style="color: #666666">=</span> <span style="color: #666666">12</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn</span> <span style="color: #008000; font-weight: bold">import</span> datasets
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.pipeline</span> <span style="color: #008000; font-weight: bold">import</span> Pipeline
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> LinearSVC
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> make_moons
X, y <span style="color: #666666">=</span> make_moons(n_samples<span style="color: #666666">=100</span>, noise<span style="color: #666666">=0.15</span>, random_state<span style="color: #666666">=42</span>)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">plot_dataset</span>(X, y, axes):
plt<span style="color: #666666">.</span>plot(X[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==0</span>], X[:, <span style="color: #666666">1</span>][y<span style="color: #666666">==0</span>], <span style="color: #BA2121">&quot;bs&quot;</span>)
plt<span style="color: #666666">.</span>plot(X[:, <span style="color: #666666">0</span>][y<span style="color: #666666">==1</span>], X[:, <span style="color: #666666">1</span>][y<span style="color: #666666">==1</span>], <span style="color: #BA2121">&quot;g^&quot;</span>)
plt<span style="color: #666666">.</span>axis(axes)
plt<span style="color: #666666">.</span>grid(<span style="color: #008000">True</span>, which<span style="color: #666666">=</span><span style="color: #BA2121">&#39;both&#39;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&quot;$x_1$&quot;</span>, fontsize<span style="color: #666666">=20</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&quot;$x_2$&quot;</span>, fontsize<span style="color: #666666">=20</span>, rotation<span style="color: #666666">=0</span>)
plot_dataset(X, y, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plt<span style="color: #666666">.</span>show()
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> make_moons
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.pipeline</span> <span style="color: #008000; font-weight: bold">import</span> Pipeline
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> PolynomialFeatures
polynomial_svm_clf <span style="color: #666666">=</span> Pipeline([
(<span style="color: #BA2121">&quot;poly_features&quot;</span>, PolynomialFeatures(degree<span style="color: #666666">=3</span>)),
(<span style="color: #BA2121">&quot;scaler&quot;</span>, StandardScaler()),
(<span style="color: #BA2121">&quot;svm_clf&quot;</span>, LinearSVC(C<span style="color: #666666">=10</span>, loss<span style="color: #666666">=</span><span style="color: #BA2121">&quot;hinge&quot;</span>, random_state<span style="color: #666666">=42</span>))
])
polynomial_svm_clf<span style="color: #666666">.</span>fit(X, y)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">plot_predictions</span>(clf, axes):
x0s <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(axes[<span style="color: #666666">0</span>], axes[<span style="color: #666666">1</span>], <span style="color: #666666">100</span>)
x1s <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(axes[<span style="color: #666666">2</span>], axes[<span style="color: #666666">3</span>], <span style="color: #666666">100</span>)
x0, x1 <span style="color: #666666">=</span> np<span style="color: #666666">.</span>meshgrid(x0s, x1s)
X <span style="color: #666666">=</span> np<span style="color: #666666">.</span>c_[x0<span style="color: #666666">.</span>ravel(), x1<span style="color: #666666">.</span>ravel()]
y_pred <span style="color: #666666">=</span> clf<span style="color: #666666">.</span>predict(X)<span style="color: #666666">.</span>reshape(x0<span style="color: #666666">.</span>shape)
y_decision <span style="color: #666666">=</span> clf<span style="color: #666666">.</span>decision_function(X)<span style="color: #666666">.</span>reshape(x0<span style="color: #666666">.</span>shape)
plt<span style="color: #666666">.</span>contourf(x0, x1, y_pred, cmap<span style="color: #666666">=</span>plt<span style="color: #666666">.</span>cm<span style="color: #666666">.</span>brg, alpha<span style="color: #666666">=0.2</span>)
plt<span style="color: #666666">.</span>contourf(x0, x1, y_decision, cmap<span style="color: #666666">=</span>plt<span style="color: #666666">.</span>cm<span style="color: #666666">.</span>brg, alpha<span style="color: #666666">=0.1</span>)
plot_predictions(polynomial_svm_clf, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plot_dataset(X, y, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plt<span style="color: #666666">.</span>show()
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
poly_kernel_svm_clf <span style="color: #666666">=</span> Pipeline([
(<span style="color: #BA2121">&quot;scaler&quot;</span>, StandardScaler()),
(<span style="color: #BA2121">&quot;svm_clf&quot;</span>, SVC(kernel<span style="color: #666666">=</span><span style="color: #BA2121">&quot;poly&quot;</span>, degree<span style="color: #666666">=3</span>, coef0<span style="color: #666666">=1</span>, C<span style="color: #666666">=5</span>))
])
poly_kernel_svm_clf<span style="color: #666666">.</span>fit(X, y)
poly100_kernel_svm_clf <span style="color: #666666">=</span> Pipeline([
(<span style="color: #BA2121">&quot;scaler&quot;</span>, StandardScaler()),
(<span style="color: #BA2121">&quot;svm_clf&quot;</span>, SVC(kernel<span style="color: #666666">=</span><span style="color: #BA2121">&quot;poly&quot;</span>, degree<span style="color: #666666">=10</span>, coef0<span style="color: #666666">=100</span>, C<span style="color: #666666">=5</span>))
])
poly100_kernel_svm_clf<span style="color: #666666">.</span>fit(X, y)
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">11</span>, <span style="color: #666666">4</span>))
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">121</span>)
plot_predictions(poly_kernel_svm_clf, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plot_dataset(X, y, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r&quot;$d=3, r=1, C=5$&quot;</span>, fontsize<span style="color: #666666">=18</span>)
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
plot_predictions(poly100_kernel_svm_clf, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plot_dataset(X, y, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r&quot;$d=10, r=100, C=5$&quot;</span>, fontsize<span style="color: #666666">=18</span>)
plt<span style="color: #666666">.</span>show()
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">gaussian_rbf</span>(x, landmark, gamma):
<span style="color: #008000; font-weight: bold">return</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>gamma <span style="color: #666666">*</span> np<span style="color: #666666">.</span>linalg<span style="color: #666666">.</span>norm(x <span style="color: #666666">-</span> landmark, axis<span style="color: #666666">=1</span>)<span style="color: #666666">**2</span>)
gamma <span style="color: #666666">=</span> <span style="color: #666666">0.3</span>
x1s <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">-4.5</span>, <span style="color: #666666">4.5</span>, <span style="color: #666666">200</span>)<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, <span style="color: #666666">1</span>)
x2s <span style="color: #666666">=</span> gaussian_rbf(x1s, <span style="color: #666666">-2</span>, gamma)
x3s <span style="color: #666666">=</span> gaussian_rbf(x1s, <span style="color: #666666">1</span>, gamma)
XK <span style="color: #666666">=</span> np<span style="color: #666666">.</span>c_[gaussian_rbf(X1D, <span style="color: #666666">-2</span>, gamma), gaussian_rbf(X1D, <span style="color: #666666">1</span>, gamma)]
yk <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([<span style="color: #666666">0</span>, <span style="color: #666666">0</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">1</span>, <span style="color: #666666">0</span>, <span style="color: #666666">0</span>])
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">11</span>, <span style="color: #666666">4</span>))
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">121</span>)
plt<span style="color: #666666">.</span>grid(<span style="color: #008000">True</span>, which<span style="color: #666666">=</span><span style="color: #BA2121">&#39;both&#39;</span>)
plt<span style="color: #666666">.</span>axhline(y<span style="color: #666666">=0</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">&#39;k&#39;</span>)
plt<span style="color: #666666">.</span>scatter(x<span style="color: #666666">=</span>[<span style="color: #666666">-2</span>, <span style="color: #666666">1</span>], y<span style="color: #666666">=</span>[<span style="color: #666666">0</span>, <span style="color: #666666">0</span>], s<span style="color: #666666">=150</span>, alpha<span style="color: #666666">=0.5</span>, c<span style="color: #666666">=</span><span style="color: #BA2121">&quot;red&quot;</span>)
plt<span style="color: #666666">.</span>plot(X1D[:, <span style="color: #666666">0</span>][yk<span style="color: #666666">==0</span>], np<span style="color: #666666">.</span>zeros(<span style="color: #666666">4</span>), <span style="color: #BA2121">&quot;bs&quot;</span>)
plt<span style="color: #666666">.</span>plot(X1D[:, <span style="color: #666666">0</span>][yk<span style="color: #666666">==1</span>], np<span style="color: #666666">.</span>zeros(<span style="color: #666666">5</span>), <span style="color: #BA2121">&quot;g^&quot;</span>)
plt<span style="color: #666666">.</span>plot(x1s, x2s, <span style="color: #BA2121">&quot;g--&quot;</span>)
plt<span style="color: #666666">.</span>plot(x1s, x3s, <span style="color: #BA2121">&quot;b:&quot;</span>)
plt<span style="color: #666666">.</span>gca()<span style="color: #666666">.</span>get_yaxis()<span style="color: #666666">.</span>set_ticks([<span style="color: #666666">0</span>, <span style="color: #666666">0.25</span>, <span style="color: #666666">0.5</span>, <span style="color: #666666">0.75</span>, <span style="color: #666666">1</span>])
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&quot;$x_1$&quot;</span>, fontsize<span style="color: #666666">=20</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&quot;Similarity&quot;</span>, fontsize<span style="color: #666666">=14</span>)
plt<span style="color: #666666">.</span>annotate(<span style="color: #BA2121">r&#39;$\mathbf{x}$&#39;</span>,
xy<span style="color: #666666">=</span>(X1D[<span style="color: #666666">3</span>, <span style="color: #666666">0</span>], <span style="color: #666666">0</span>),
xytext<span style="color: #666666">=</span>(<span style="color: #666666">-0.5</span>, <span style="color: #666666">0.20</span>),
ha<span style="color: #666666">=</span><span style="color: #BA2121">&quot;center&quot;</span>,
arrowprops<span style="color: #666666">=</span><span style="color: #008000">dict</span>(facecolor<span style="color: #666666">=</span><span style="color: #BA2121">&#39;black&#39;</span>, shrink<span style="color: #666666">=0.1</span>),
fontsize<span style="color: #666666">=18</span>,
)
plt<span style="color: #666666">.</span>text(<span style="color: #666666">-2</span>, <span style="color: #666666">0.9</span>, <span style="color: #BA2121">&quot;$x_2$&quot;</span>, ha<span style="color: #666666">=</span><span style="color: #BA2121">&quot;center&quot;</span>, fontsize<span style="color: #666666">=20</span>)
plt<span style="color: #666666">.</span>text(<span style="color: #666666">1</span>, <span style="color: #666666">0.9</span>, <span style="color: #BA2121">&quot;$x_3$&quot;</span>, ha<span style="color: #666666">=</span><span style="color: #BA2121">&quot;center&quot;</span>, fontsize<span style="color: #666666">=20</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">-4.5</span>, <span style="color: #666666">4.5</span>, <span style="color: #666666">-0.1</span>, <span style="color: #666666">1.1</span>])
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">122</span>)
plt<span style="color: #666666">.</span>grid(<span style="color: #008000">True</span>, which<span style="color: #666666">=</span><span style="color: #BA2121">&#39;both&#39;</span>)
plt<span style="color: #666666">.</span>axhline(y<span style="color: #666666">=0</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">&#39;k&#39;</span>)
plt<span style="color: #666666">.</span>axvline(x<span style="color: #666666">=0</span>, color<span style="color: #666666">=</span><span style="color: #BA2121">&#39;k&#39;</span>)
plt<span style="color: #666666">.</span>plot(XK[:, <span style="color: #666666">0</span>][yk<span style="color: #666666">==0</span>], XK[:, <span style="color: #666666">1</span>][yk<span style="color: #666666">==0</span>], <span style="color: #BA2121">&quot;bs&quot;</span>)
plt<span style="color: #666666">.</span>plot(XK[:, <span style="color: #666666">0</span>][yk<span style="color: #666666">==1</span>], XK[:, <span style="color: #666666">1</span>][yk<span style="color: #666666">==1</span>], <span style="color: #BA2121">&quot;g^&quot;</span>)
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">r&quot;$x_2$&quot;</span>, fontsize<span style="color: #666666">=20</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">r&quot;$x_3$ &quot;</span>, fontsize<span style="color: #666666">=20</span>, rotation<span style="color: #666666">=0</span>)
plt<span style="color: #666666">.</span>annotate(<span style="color: #BA2121">r&#39;$\phi\left(\mathbf{x}\right)$&#39;</span>,
xy<span style="color: #666666">=</span>(XK[<span style="color: #666666">3</span>, <span style="color: #666666">0</span>], XK[<span style="color: #666666">3</span>, <span style="color: #666666">1</span>]),
xytext<span style="color: #666666">=</span>(<span style="color: #666666">0.65</span>, <span style="color: #666666">0.50</span>),
ha<span style="color: #666666">=</span><span style="color: #BA2121">&quot;center&quot;</span>,
arrowprops<span style="color: #666666">=</span><span style="color: #008000">dict</span>(facecolor<span style="color: #666666">=</span><span style="color: #BA2121">&#39;black&#39;</span>, shrink<span style="color: #666666">=0.1</span>),
fontsize<span style="color: #666666">=18</span>,
)
plt<span style="color: #666666">.</span>plot([<span style="color: #666666">-0.1</span>, <span style="color: #666666">1.1</span>], [<span style="color: #666666">0.57</span>, <span style="color: #666666">-0.1</span>], <span style="color: #BA2121">&quot;r--&quot;</span>, linewidth<span style="color: #666666">=3</span>)
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">-0.1</span>, <span style="color: #666666">1.1</span>, <span style="color: #666666">-0.1</span>, <span style="color: #666666">1.1</span>])
plt<span style="color: #666666">.</span>subplots_adjust(right<span style="color: #666666">=1</span>)
plt<span style="color: #666666">.</span>show()
x1_example <span style="color: #666666">=</span> X1D[<span style="color: #666666">3</span>, <span style="color: #666666">0</span>]
<span style="color: #008000; font-weight: bold">for</span> landmark <span style="color: #AA22FF; font-weight: bold">in</span> (<span style="color: #666666">-2</span>, <span style="color: #666666">1</span>):
k <span style="color: #666666">=</span> gaussian_rbf(np<span style="color: #666666">.</span>array([[x1_example]]), np<span style="color: #666666">.</span>array([[landmark]]), gamma)
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;Phi({}, {}) = {}&quot;</span><span style="color: #666666">.</span>format(x1_example, landmark, k))
rbf_kernel_svm_clf <span style="color: #666666">=</span> Pipeline([
(<span style="color: #BA2121">&quot;scaler&quot;</span>, StandardScaler()),
(<span style="color: #BA2121">&quot;svm_clf&quot;</span>, SVC(kernel<span style="color: #666666">=</span><span style="color: #BA2121">&quot;rbf&quot;</span>, gamma<span style="color: #666666">=5</span>, C<span style="color: #666666">=0.001</span>))
])
rbf_kernel_svm_clf<span style="color: #666666">.</span>fit(X, y)
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.svm</span> <span style="color: #008000; font-weight: bold">import</span> SVC
gamma1, gamma2 <span style="color: #666666">=</span> <span style="color: #666666">0.1</span>, <span style="color: #666666">5</span>
C1, C2 <span style="color: #666666">=</span> <span style="color: #666666">0.001</span>, <span style="color: #666666">1000</span>
hyperparams <span style="color: #666666">=</span> (gamma1, C1), (gamma1, C2), (gamma2, C1), (gamma2, C2)
svm_clfs <span style="color: #666666">=</span> []
<span style="color: #008000; font-weight: bold">for</span> gamma, C <span style="color: #AA22FF; font-weight: bold">in</span> hyperparams:
rbf_kernel_svm_clf <span style="color: #666666">=</span> Pipeline([
(<span style="color: #BA2121">&quot;scaler&quot;</span>, StandardScaler()),
(<span style="color: #BA2121">&quot;svm_clf&quot;</span>, SVC(kernel<span style="color: #666666">=</span><span style="color: #BA2121">&quot;rbf&quot;</span>, gamma<span style="color: #666666">=</span>gamma, C<span style="color: #666666">=</span>C))
])
rbf_kernel_svm_clf<span style="color: #666666">.</span>fit(X, y)
svm_clfs<span style="color: #666666">.</span>append(rbf_kernel_svm_clf)
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">11</span>, <span style="color: #666666">7</span>))
<span style="color: #008000; font-weight: bold">for</span> i, svm_clf <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">enumerate</span>(svm_clfs):
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">221</span> <span style="color: #666666">+</span> i)
plot_predictions(svm_clf, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
plot_dataset(X, y, [<span style="color: #666666">-1.5</span>, <span style="color: #666666">2.5</span>, <span style="color: #666666">-1</span>, <span style="color: #666666">1.5</span>])
gamma, C <span style="color: #666666">=</span> hyperparams[i]
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">r&quot;$\gamma = {}, C = {}$&quot;</span><span style="color: #666666">.</span>format(gamma, C), fontsize<span style="color: #666666">=16</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -193,6 +367,8 @@ This will make our life much easier. You don't need t write your own optimizer.
<li><a href="._svm-bs024.html">25</a></li>
<li><a href="._svm-bs025.html">26</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs027.html">28</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs024.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+54 -91
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,71 +155,28 @@ MathJax.Hub.Config({
<a name="part0024"></a>
<!-- !split -->
<h2 id="___sec23" class="anchor">A simple example </h2>
<h2 id="___sec23" class="anchor">Mathematical optimization of convex functions </h2>
<p>
We remind ourselves about the general problem we want to solve
A mathematical (quadratic) optimization problem, or just optimization problem, has the form
$$
\begin{align*}
&\mathrm{min}_{x}\hspace{0.2cm} \frac{1}{2}\boldsymbol{x}^T\boldsymbol{P}\boldsymbol{x}+\boldsymbol{q}^T\boldsymbol{x},\\ \nonumber
&\mathrm{subject\hspace{0.1cm} to} \hspace{0.2cm} \boldsymbol{G}\boldsymbol{x} \preceq \boldsymbol{h} \wedge \boldsymbol{A}\boldsymbol{x}=f.
&\mathrm{min}_{\lambda}\hspace{0.2cm} \frac{1}{2}\boldsymbol{\lambda}^T\boldsymbol{P}\boldsymbol{\lambda}+\boldsymbol{q}^T\boldsymbol{\lambda},\\ \nonumber
&\mathrm{subject\hspace{0.1cm}to} \hspace{0.2cm} \boldsymbol{G}\boldsymbol{\lambda} \preceq \boldsymbol{h} \wedge \boldsymbol{A}\boldsymbol{\lambda}=f.
\end{align*}
$$
<p>
Let us show how to perform the optmization using a simple case. Assume we want to optimize the following problem
$$
\begin{align*}
&\mathrm{min}_{x}\hspace{0.2cm} \frac{1}{2}x^2+5x+3y \\ \nonumber
&\mathrm{subject to} \\ \nonumber
&x, y \geq 0 \\ \nonumber
&x+3y \geq 15 \\ \nonumber
&2x+5y \leq 100 \\ \nonumber
&3x+4y \leq 80. \\ \nonumber
\end{align*}
$$
The minimization problem can be rewritten in terms of vectors and matrices as (with \( x \) and \( y \) being the unknowns)
$$
\frac{1}{2}\begin{bmatrix} x\\ y \end{bmatrix}^T \begin{bmatrix} 1 & 0\\ 0 & 0 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix}3\\ 4 \end{bmatrix}^T \begin{bmatrix}x \\ y \end{bmatrix}.
$$
Similarly, we can now set up the inequalities (we need to change \( \geq \) to \( \leq \) by multiplying with \( -1 \) on bot sides) as the following matrix-vector equation
$$
\begin{bmatrix} -1 & 0 \\ 0 & -1 \\ -1 & -3 \\ 2 & 5 \\ 3 & 4\end{bmatrix}\begin{bmatrix} x \\ y\end{bmatrix} \preceq \begin{bmatrix}0 \\ 0\\ -15 \\ 100 \\ 80\end{bmatrix}.
$$
We have collapsed all the inequalities into a single matrix \( \boldsymbol{G} \). We see also that our matrix
$$
\boldsymbol{P} =\begin{bmatrix} 1 & 0\\ 0 & 0 \end{bmatrix}
$$
is clearly positive semi-definite (all eigenvalues larger or equal zero).
Finally, the vector \( \boldsymbol{h} \) is defined as
$$
\boldsymbol{h} = \begin{bmatrix}0 \\ 0\\ -15 \\ 100 \\ 80\end{bmatrix}.
$$
subject to some constraints for say a selected set \( i=1,2,\dots, n \).
In our case we are optimizing with respect to the Lagrangian multipliers \( \lambda_i \), and the
vector \( \boldsymbol{\lambda}=[\lambda_1, \lambda_2,\dots, \lambda_n] \) is the optimization variable we are dealing with.
<p>
Since we don't have any equalities the matrix \( \boldsymbol{A} \) is set to zero
The following code solves the equations for us
<p>
In our case we are particularly interested in a class of optimization problems called convex optmization problems.
In our discussion on gradient descent methods we discussed at length the definition of a convex function.
<p>
Convex optimization problems play a central role in applied mathematics and we recommend strongly <a href="http://web.stanford.edu/~boyd/cvxbook/" target="_self">Boyd and Vandenberghe's text on the topics</a>.
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Import the necessary packages</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">cvxopt</span> <span style="color: #008000; font-weight: bold">import</span> matrix
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">cvxopt</span> <span style="color: #008000; font-weight: bold">import</span> solvers
P <span style="color: #666666">=</span> matrix(numpy<span style="color: #666666">.</span>diag([<span style="color: #666666">1</span>,<span style="color: #666666">0</span>]), tc<span style="color: #666666">=</span>d)
q <span style="color: #666666">=</span> matrix(numpy<span style="color: #666666">.</span>array([<span style="color: #666666">3</span>,<span style="color: #666666">4</span>]), tc<span style="color: #666666">=</span>d)
G <span style="color: #666666">=</span> matrix(numpy<span style="color: #666666">.</span>array([[<span style="color: #666666">-1</span>,<span style="color: #666666">0</span>],[<span style="color: #666666">0</span>,<span style="color: #666666">-1</span>],[<span style="color: #666666">-1</span>,<span style="color: #666666">-3</span>],[<span style="color: #666666">2</span>,<span style="color: #666666">5</span>],[<span style="color: #666666">3</span>,<span style="color: #666666">4</span>]]), tc<span style="color: #666666">=</span>d)
h <span style="color: #666666">=</span> matrix(numpy<span style="color: #666666">.</span>array([<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">-15</span>,<span style="color: #666666">100</span>,<span style="color: #666666">80</span>]), tc<span style="color: #666666">=</span>d)
<span style="color: #408080; font-style: italic"># Construct the QP, invoke solver</span>
sol <span style="color: #666666">=</span> solvers<span style="color: #666666">.</span>qp(P,q,G,h)
<span style="color: #408080; font-style: italic"># Extract optimal value and solution</span>
sol[x]
sol[primal objective]
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
@@ -234,6 +195,8 @@ sol[primal objective]
<li class="active"><a href="._svm-bs024.html">25</a></li>
<li><a href="._svm-bs025.html">26</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs027.html">28</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs025.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+60 -51
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,25 +155,28 @@ MathJax.Hub.Config({
<a name="part0025"></a>
<!-- !split -->
<h2 id="___sec24" class="anchor">Back to the more realistic cases </h2>
<h2 id="___sec24" class="anchor">How do we solve these problems? </h2>
<p>
We are now ready to return to our setup of the optmization problem for a more realistic case. Introducing the <b>slack</b> parameter \( C \) we have
$$
\frac{1}{2} \boldsymbol{\lambda}^T\begin{bmatrix} y_1y_1K(\boldsymbol{x}_1,\boldsymbol{x}_1) & y_1y_2K(\boldsymbol{x}_1,\boldsymbol{x}_2) & \dots & \dots & y_1y_nK(\boldsymbol{x}_1,\boldsymbol{x}_n) \\
y_2y_1K(\boldsymbol{x}_2,\boldsymbol{x}_1) & y_2y_2K(\boldsymbol{x}_2,\boldsymbol{x}_2) & \dots & \dots & y_1y_nK(\boldsymbol{x}_2,\boldsymbol{x}_n) \\
\dots & \dots & \dots & \dots & \dots \\
\dots & \dots & \dots & \dots & \dots \\
y_ny_1K(\boldsymbol{x}_n,\boldsymbol{x}_1) & y_ny_2K(\boldsymbol{x}_n\boldsymbol{x}_2) & \dots & \dots & y_ny_nK(\boldsymbol{x}_n,\boldsymbol{x}_n) \\
\end{bmatrix}\boldsymbol{\lambda}-\mathbb{I}\boldsymbol{\lambda},
$$
subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vectors \( \boldsymbol{\lambda} =[\lambda_1,\lambda_2,\dots,\lambda_n] \) and
\( \boldsymbol{y}=[y_1,y_2,\dots,y_n] \).
With the slack constants this leads to the additional constraint \( 0\leq \lambda_i \leq C \).
If we use Python as programming language and wish to venture beyond
<b>scikit-learn</b>, <b>tensorflow</b> and similar software which makes our
lives so much easier, we need to dive into the wonderful world of
quadratic programming. We can, if we wish, solve the minimization
problem using say standard gradient methods or conjugate gradient
methods. However, these methods tend to exhibit a rather slow
converge. So, welcome to the promised land of quadratic programming.
<p>
<b>code will be added</b>
The functions we need are contained in the quadratic programming package <b>CVXOPT</b> and we need to import it together with <b>numpy</b> as
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">cvxopt</span>
</pre></div>
<p>
This will make our life much easier. You don't need t write your own optimizer.
<p>
<p>
@@ -188,6 +195,8 @@ With the slack constants this leads to the additional constraint \( 0\leq \lamb
<li><a href="._svm-bs024.html">25</a></li>
<li class="active"><a href="._svm-bs025.html">26</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs027.html">28</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs026.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+104 -62
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -151,37 +155,72 @@ MathJax.Hub.Config({
<a name="part0026"></a>
<!-- !split -->
<h2 id="___sec25" class="anchor">Code Example </h2>
<h2 id="___sec25" class="anchor">A simple example </h2>
<p>
We remind ourselves about the general problem we want to solve
$$
\begin{align*}
&\mathrm{min}_{x}\hspace{0.2cm} \frac{1}{2}\boldsymbol{x}^T\boldsymbol{P}\boldsymbol{x}+\boldsymbol{q}^T\boldsymbol{x},\\ \nonumber
&\mathrm{subject\hspace{0.1cm} to} \hspace{0.2cm} \boldsymbol{G}\boldsymbol{x} \preceq \boldsymbol{h} \wedge \boldsymbol{A}\boldsymbol{x}=f.
\end{align*}
$$
<p>
Let us show how to perform the optmization using a simple case. Assume we want to optimize the following problem
$$
\begin{align*}
&\mathrm{min}_{x}\hspace{0.2cm} \frac{1}{2}x^2+5x+3y \\ \nonumber
&\mathrm{subject to} \\ \nonumber
&x, y \geq 0 \\ \nonumber
&x+3y \geq 15 \\ \nonumber
&2x+5y \leq 100 \\ \nonumber
&3x+4y \leq 80. \\ \nonumber
\end{align*}
$$
The minimization problem can be rewritten in terms of vectors and matrices as (with \( x \) and \( y \) being the unknowns)
$$
\frac{1}{2}\begin{bmatrix} x\\ y \end{bmatrix}^T \begin{bmatrix} 1 & 0\\ 0 & 0 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix}3\\ 4 \end{bmatrix}^T \begin{bmatrix}x \\ y \end{bmatrix}.
$$
Similarly, we can now set up the inequalities (we need to change \( \geq \) to \( \leq \) by multiplying with \( -1 \) on bot sides) as the following matrix-vector equation
$$
\begin{bmatrix} -1 & 0 \\ 0 & -1 \\ -1 & -3 \\ 2 & 5 \\ 3 & 4\end{bmatrix}\begin{bmatrix} x \\ y\end{bmatrix} \preceq \begin{bmatrix}0 \\ 0\\ -15 \\ 100 \\ 80\end{bmatrix}.
$$
We have collapsed all the inequalities into a single matrix \( \boldsymbol{G} \). We see also that our matrix
$$
\boldsymbol{P} =\begin{bmatrix} 1 & 0\\ 0 & 0 \end{bmatrix}
$$
is clearly positive semi-definite (all eigenvalues larger or equal zero).
Finally, the vector \( \boldsymbol{h} \) is defined as
$$
\boldsymbol{h} = \begin{bmatrix}0 \\ 0\\ -15 \\ 100 \\ 80\end{bmatrix}.
$$
<p>
Since we don't have any equalities the matrix \( \boldsymbol{A} \) is set to zero
The following code solves the equations for us
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">scipy.optimize</span> <span style="color: #008000; font-weight: bold">import</span> minimize
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">rosen</span>(x):
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">sum</span>(<span style="color: #666666">100.0*</span>(x[<span style="color: #666666">1</span>:]<span style="color: #666666">-</span>x[:<span style="color: #666666">-1</span>]<span style="color: #666666">**2.0</span>)<span style="color: #666666">**2.0</span> <span style="color: #666666">+</span> (<span style="color: #666666">1-</span>x[:<span style="color: #666666">-1</span>])<span style="color: #666666">**2.0</span>)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">rosen_der</span>(x):
xm <span style="color: #666666">=</span> x[<span style="color: #666666">1</span>:<span style="color: #666666">-1</span>]
xm_m1 <span style="color: #666666">=</span> x[:<span style="color: #666666">-2</span>]
xm_p1 <span style="color: #666666">=</span> x[<span style="color: #666666">2</span>:]
der <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros_like(x)
der[<span style="color: #666666">1</span>:<span style="color: #666666">-1</span>] <span style="color: #666666">=</span> <span style="color: #666666">200*</span>(xm<span style="color: #666666">-</span>xm_m1<span style="color: #666666">**2</span>) <span style="color: #666666">-</span> <span style="color: #666666">400*</span>(xm_p1 <span style="color: #666666">-</span> xm<span style="color: #666666">**2</span>)<span style="color: #666666">*</span>xm <span style="color: #666666">-</span> <span style="color: #666666">2*</span>(<span style="color: #666666">1-</span>xm)
der[<span style="color: #666666">0</span>] <span style="color: #666666">=</span> <span style="color: #666666">-400*</span>x[<span style="color: #666666">0</span>]<span style="color: #666666">*</span>(x[<span style="color: #666666">1</span>]<span style="color: #666666">-</span>x[<span style="color: #666666">0</span>]<span style="color: #666666">**2</span>) <span style="color: #666666">-</span> <span style="color: #666666">2*</span>(<span style="color: #666666">1-</span>x[<span style="color: #666666">0</span>])
der[<span style="color: #666666">-1</span>] <span style="color: #666666">=</span> <span style="color: #666666">200*</span>(x[<span style="color: #666666">-1</span>]<span style="color: #666666">-</span>x[<span style="color: #666666">-2</span>]<span style="color: #666666">**2</span>)
<span style="color: #008000; font-weight: bold">return</span> der
x0 <span style="color: #666666">=</span> np<span style="color: #666666">.</span>array([<span style="color: #666666">1.2</span>, <span style="color: #666666">0.1</span>, <span style="color: #666666">0.8</span>, <span style="color: #666666">1.9</span>, <span style="color: #666666">1.2</span>])
<span style="color: #008000; font-weight: bold">print</span>(x0)
res <span style="color: #666666">=</span> minimize(rosen, x0, method<span style="color: #666666">=</span><span style="color: #BA2121">&#39;nelder-mead&#39;</span>,options<span style="color: #666666">=</span>{<span style="color: #BA2121">&#39;xtol&#39;</span>: <span style="color: #666666">1e-8</span>, <span style="color: #BA2121">&#39;disp&#39;</span>: <span style="color: #008000">True</span>})
<span style="color: #008000; font-weight: bold">print</span>(x0)
res2 <span style="color: #666666">=</span> minimize(rosen, x0, method<span style="color: #666666">=</span><span style="color: #BA2121">&#39;BFGS&#39;</span>, jac<span style="color: #666666">=</span>rosen_der, options<span style="color: #666666">=</span>{<span style="color: #BA2121">&#39;disp&#39;</span>: <span style="color: #008000">True</span>})
<span style="color: #008000; font-weight: bold">print</span>(x0)
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #408080; font-style: italic"># Import the necessary packages</span>
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">cvxopt</span> <span style="color: #008000; font-weight: bold">import</span> matrix
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">cvxopt</span> <span style="color: #008000; font-weight: bold">import</span> solvers
P <span style="color: #666666">=</span> matrix(numpy<span style="color: #666666">.</span>diag([<span style="color: #666666">1</span>,<span style="color: #666666">0</span>]), tc<span style="color: #666666">=</span>d)
q <span style="color: #666666">=</span> matrix(numpy<span style="color: #666666">.</span>array([<span style="color: #666666">3</span>,<span style="color: #666666">4</span>]), tc<span style="color: #666666">=</span>d)
G <span style="color: #666666">=</span> matrix(numpy<span style="color: #666666">.</span>array([[<span style="color: #666666">-1</span>,<span style="color: #666666">0</span>],[<span style="color: #666666">0</span>,<span style="color: #666666">-1</span>],[<span style="color: #666666">-1</span>,<span style="color: #666666">-3</span>],[<span style="color: #666666">2</span>,<span style="color: #666666">5</span>],[<span style="color: #666666">3</span>,<span style="color: #666666">4</span>]]), tc<span style="color: #666666">=</span>d)
h <span style="color: #666666">=</span> matrix(numpy<span style="color: #666666">.</span>array([<span style="color: #666666">0</span>,<span style="color: #666666">0</span>,<span style="color: #666666">-15</span>,<span style="color: #666666">100</span>,<span style="color: #666666">80</span>]), tc<span style="color: #666666">=</span>d)
<span style="color: #408080; font-style: italic"># Construct the QP, invoke solver</span>
sol <span style="color: #666666">=</span> solvers<span style="color: #666666">.</span>qp(P,q,G,h)
<span style="color: #408080; font-style: italic"># Extract optimal value and solution</span>
sol[x]
sol[primal objective]
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
@@ -197,6 +236,9 @@ res2 <span style="color: #666666">=</span> minimize(rosen, x0, method<span style
<li><a href="._svm-bs024.html">25</a></li>
<li><a href="._svm-bs025.html">26</a></li>
<li class="active"><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs027.html">28</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs027.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+42 -38
View File
@@ -49,30 +49,32 @@ Automatically generated HTML file from DocOnce source
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -118,24 +120,26 @@ MathJax.Hub.Config({
<!-- navigation toc: --> <li><a href="._svm-bs006.html#___sec5" style="font-size: 80%;">Getting into the details</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs007.html#___sec6" style="font-size: 80%;">First attempt at a minimization approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs008.html#___sec7" style="font-size: 80%;">Solving the equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs009.html#___sec8" style="font-size: 80%;">Code Example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs010.html#___sec9" style="font-size: 80%;">Problems with the Simpler Approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs011.html#___sec10" style="font-size: 80%;">A better approach</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs012.html#___sec11" style="font-size: 80%;">A quick reminder on Lagrangian multipliers</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs013.html#___sec12" style="font-size: 80%;">Adding the muliplier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs014.html#___sec13" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs015.html#___sec14" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs016.html#___sec15" style="font-size: 80%;">The last steps</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs017.html#___sec16" style="font-size: 80%;">A soft classifier</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs018.html#___sec17" style="font-size: 80%;">Soft optmization problem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs019.html#___sec18" style="font-size: 80%;">Kernels and non-linearity</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs020.html#___sec19" style="font-size: 80%;">The equations</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs021.html#___sec20" style="font-size: 80%;">The problem to solve</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs022.html#___sec21" style="font-size: 80%;">Different kernels and Mercer's theorem</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs023.html#___sec22" style="font-size: 80%;">The moons example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs024.html#___sec23" style="font-size: 80%;">Mathematical optimization of convex functions</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs025.html#___sec24" style="font-size: 80%;">How do we solve these problems?</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs026.html#___sec25" style="font-size: 80%;">A simple example</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs027.html#___sec26" style="font-size: 80%;">Back to the more realistic cases</a></li>
<!-- navigation toc: --> <li><a href="._svm-bs028.html#___sec27" style="font-size: 80%;">Code Example</a></li>
</ul>
</li>
@@ -170,7 +174,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Nov 15, 2019</h4></center> <!-- date -->
<center><h4>Nov 16, 2019</h4></center> <!-- date -->
<br>
<p>
@@ -194,7 +198,7 @@ MathJax.Hub.Config({
<li><a href="._svm-bs008.html">9</a></li>
<li><a href="._svm-bs009.html">10</a></li>
<li><a href="">...</a></li>
<li><a href="._svm-bs026.html">27</a></li>
<li><a href="._svm-bs028.html">29</a></li>
<li><a href="._svm-bs001.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
+49 -24
View File
@@ -148,7 +148,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>&nbsp;<br>
<center><h4>Nov 15, 2019</h4></center> <!-- date -->
<center><h4>Nov 16, 2019</h4></center> <!-- date -->
<br>
<p>
@@ -448,7 +448,7 @@ $$
<h2 id="___sec7">Solving the equations </h2>
<p>
We can now use the Newton-Raphson method or gradient descent to solve the equations
We can now use the Newton-Raphson method or different variants of the gradient descent family (from plain gradient descent to various stochastic gradient descent approaches) to solve the equations
<p>&nbsp;<br>
$$
b \leftarrow b +\eta \frac{\partial C}{\partial b},
@@ -463,12 +463,34 @@ $$
<p>&nbsp;<br>
where \( \eta \) is our by now well-known learning rate.
</section>
<section>
<h2 id="___sec8">Code Example </h2>
<p>
The equations we discussed above can be coded rather easily (the
framework is rather similar to what we developed for logistic
regression). We use the standard moons example to set up our data
set. We want to find a line which separates the two classes
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%"><span></span>
</pre></div>
</section>
<section>
<h2 id="___sec9">Problems with the Simpler Approach </h2>
<p>
There are however problems with this approach, although it looks
pretty straightforward to implement. In case we separate our data into
two distinct classes, we may up with many possible lines, as indicated
in the figure and shown by running the following program. For small
pretty straightforward to implement. When running the above code, we see that we can easily end up with many diffeent lines which separate the two classes.
<p>
For small
gaps between the entries, we may also end up needing many iterations
before the solutions converge and if the data cannot be separated
properly into two distinct classes, we may not experience a converge
@@ -477,7 +499,7 @@ at all.
<section>
<h2 id="___sec8">A better approach </h2>
<h2 id="___sec10">A better approach </h2>
<p>
A better approach is rather to try to define a large margin between
@@ -519,12 +541,15 @@ $$
<p>&nbsp;<br>
<p>
We have thus defined our margin as the invers of the norm of \( \boldsymbol{w} \). We want to minimize the norm in order to have a as large as possible margin \( M \). Before we proceed, we need to remind ourselves about Lagrangian multipliers.
We have thus defined our margin as the invers of the norm of
\( \boldsymbol{w} \). We want to minimize the norm in order to have a as large as
possible margin \( M \). Before we proceed, we need to remind ourselves
about Lagrangian multipliers.
</section>
<section>
<h2 id="___sec9">A quick reminder on Lagrangian multipliers </h2>
<h2 id="___sec11">A quick reminder on Lagrangian multipliers </h2>
<p>
Consider a function of three independent variables \( f(x,y,z) \) . For the function \( f \) to be an
@@ -586,7 +611,7 @@ Then \( dz \) is no longer arbitrary.
<section>
<h2 id="___sec10">Adding the muliplier </h2>
<h2 id="___sec12">Adding the muliplier </h2>
<p>
However, we can add to
@@ -641,7 +666,7 @@ $$
<section>
<h2 id="___sec11">Setting up the problem </h2>
<h2 id="___sec13">Setting up the problem </h2>
In order to solve the above problem, we define the following Lagrangian function to be minimized
<p>&nbsp;<br>
$$
@@ -693,7 +718,7 @@ When \( \lambda_i > 0 \), the vectors \( \boldsymbol{x}_i \) are called support
<section>
<h2 id="___sec12">The problem to solve </h2>
<h2 id="___sec14">The problem to solve </h2>
<p>
We can rewrite
@@ -721,7 +746,7 @@ subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vec
<section>
<h2 id="___sec13">The last steps </h2>
<h2 id="___sec15">The last steps </h2>
<p>
Solving the above problem, yields the values of \( \lambda_i \).
@@ -765,7 +790,7 @@ Below we discuss how to find the optimal values of \( \lambda_i \). Before we pr
<section>
<h2 id="___sec14">A soft classifier </h2>
<h2 id="___sec16">A soft classifier </h2>
<p>
Till now, the margin is strictly defined by the support vectors. This defines what is called a hard classifier, that is the margins are well defined.
@@ -804,7 +829,7 @@ misclassifications.
<section>
<h2 id="___sec15">Soft optmization problem </h2>
<h2 id="___sec17">Soft optmization problem </h2>
<p>
This has in turn the consequences that we change our optmization problem to finding the minimum of
@@ -876,7 +901,7 @@ $$
<section>
<h2 id="___sec16">Kernels and non-linearity </h2>
<h2 id="___sec18">Kernels and non-linearity </h2>
<p>
The cases we have studied till were all characterized by two classes
@@ -950,7 +975,7 @@ plt.show()
<section>
<h2 id="___sec17">The equations </h2>
<h2 id="___sec19">The equations </h2>
<p>
Suppose we define a polynomial transformation of degree two only (we continue to live in a plane with \( x_i \) and \( y_i \) as variables)
@@ -1002,7 +1027,7 @@ the trouble of performing the transformation
<section>
<h2 id="___sec18">The problem to solve </h2>
<h2 id="___sec20">The problem to solve </h2>
Using our definition of the kernel We can rewrite again the Lagrangian
<p>&nbsp;<br>
$$
@@ -1044,7 +1069,7 @@ Given a kernel \( K \) and the targets \( y_i \) this matrix is easy to set up.
<section>
<h2 id="___sec19">Different kernels and Mercer's theorem </h2>
<h2 id="___sec21">Different kernels and Mercer's theorem </h2>
<p>
There are several popular kernels being used. These are
@@ -1078,7 +1103,7 @@ well in practice.
<section>
<h2 id="___sec20">The moons example </h2>
<h2 id="___sec22">The moons example </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
@@ -1275,7 +1300,7 @@ plt.show()
<section>
<h2 id="___sec21">Mathematical optimization of convex functions </h2>
<h2 id="___sec23">Mathematical optimization of convex functions </h2>
<p>
A mathematical (quadratic) optimization problem, or just optimization problem, has the form
@@ -1302,7 +1327,7 @@ Convex optimization problems play a central role in applied mathematics and we r
<section>
<h2 id="___sec22">How do we solve these problems? </h2>
<h2 id="___sec24">How do we solve these problems? </h2>
<p>
If we use Python as programming language and wish to venture beyond
@@ -1328,7 +1353,7 @@ This will make our life much easier. You don't need t write your own optimizer.
<section>
<h2 id="___sec23">A simple example </h2>
<h2 id="___sec25">A simple example </h2>
<p>
We remind ourselves about the general problem we want to solve
@@ -1409,7 +1434,7 @@ sol[<span style="color: #a61717; background-color: #e3d2d2"></span>primal obj
<section>
<h2 id="___sec24">Back to the more realistic cases </h2>
<h2 id="___sec26">Back to the more realistic cases </h2>
<p>
We are now ready to return to our setup of the optmization problem for a more realistic case. Introducing the <b>slack</b> parameter \( C \) we have
@@ -1434,7 +1459,7 @@ With the slack constants this leads to the additional constraint \( 0\leq \lamb
<section>
<h2 id="___sec25">Code Example </h2>
<h2 id="___sec27">Code Example </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
+68 -42
View File
@@ -43,30 +43,32 @@ div { text-align: justify; text-justify: inter-word; }
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -108,7 +110,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Nov 15, 2019</h4></center> <!-- date -->
<center><h4>Nov 16, 2019</h4></center> <!-- date -->
<br>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
@@ -377,7 +379,7 @@ $$
<h2 id="___sec7">Solving the equations </h2>
<p>
We can now use the Newton-Raphson method or gradient descent to solve the equations
We can now use the Newton-Raphson method or different variants of the gradient descent family (from plain gradient descent to various stochastic gradient descent approaches) to solve the equations
$$
b \leftarrow b +\eta \frac{\partial C}{\partial b},
$$
@@ -389,11 +391,32 @@ $$
where \( \eta \) is our by now well-known learning rate.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec8">Code Example </h2>
<p>
The equations we discussed above can be coded rather easily (the
framework is rather similar to what we developed for logistic
regression). We use the standard moons example to set up our data
set. We want to find a line which separates the two classes
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%"><span></span>
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec9">Problems with the Simpler Approach </h2>
<p>
There are however problems with this approach, although it looks
pretty straightforward to implement. In case we separate our data into
two distinct classes, we may up with many possible lines, as indicated
in the figure and shown by running the following program. For small
pretty straightforward to implement. When running the above code, we see that we can easily end up with many diffeent lines which separate the two classes.
<p>
For small
gaps between the entries, we may also end up needing many iterations
before the solutions converge and if the data cannot be separated
properly into two distinct classes, we may not experience a converge
@@ -402,7 +425,7 @@ at all.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec8">A better approach </h2>
<h2 id="___sec10">A better approach </h2>
<p>
A better approach is rather to try to define a large margin between
@@ -436,12 +459,15 @@ y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq 1 \hspace{0.1cm}\forall i.
$$
<p>
We have thus defined our margin as the invers of the norm of \( \boldsymbol{w} \). We want to minimize the norm in order to have a as large as possible margin \( M \). Before we proceed, we need to remind ourselves about Lagrangian multipliers.
We have thus defined our margin as the invers of the norm of
\( \boldsymbol{w} \). We want to minimize the norm in order to have a as large as
possible margin \( M \). Before we proceed, we need to remind ourselves
about Lagrangian multipliers.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec9">A quick reminder on Lagrangian multipliers </h2>
<h2 id="___sec11">A quick reminder on Lagrangian multipliers </h2>
<p>
Consider a function of three independent variables \( f(x,y,z) \) . For the function \( f \) to be an
@@ -491,7 +517,7 @@ Then \( dz \) is no longer arbitrary.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec10">Adding the muliplier </h2>
<h2 id="___sec12">Adding the muliplier </h2>
<p>
However, we can add to
@@ -534,7 +560,7 @@ $$
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec11">Setting up the problem </h2>
<h2 id="___sec13">Setting up the problem </h2>
In order to solve the above problem, we define the following Lagrangian function to be minimized
$$
{\cal L}(\lambda,b,\boldsymbol{w})=\frac{1}{2}\boldsymbol{w}^T\boldsymbol{w}-\sum_{i=1}^n\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)-1\right],
@@ -575,7 +601,7 @@ When \( \lambda_i > 0 \), the vectors \( \boldsymbol{x}_i \) are called support
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec12">The problem to solve </h2>
<h2 id="___sec14">The problem to solve </h2>
<p>
We can rewrite
@@ -599,7 +625,7 @@ subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vec
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec13">The last steps </h2>
<h2 id="___sec15">The last steps </h2>
<p>
Solving the above problem, yields the values of \( \lambda_i \).
@@ -633,7 +659,7 @@ Below we discuss how to find the optimal values of \( \lambda_i \). Before we pr
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec14">A soft classifier </h2>
<h2 id="___sec16">A soft classifier </h2>
<p>
Till now, the margin is strictly defined by the support vectors. This defines what is called a hard classifier, that is the margins are well defined.
@@ -668,7 +694,7 @@ misclassifications.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec15">Soft optmization problem </h2>
<h2 id="___sec17">Soft optmization problem </h2>
<p>
This has in turn the consequences that we change our optmization problem to finding the minimum of
@@ -722,7 +748,7 @@ $$
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec16">Kernels and non-linearity </h2>
<h2 id="___sec18">Kernels and non-linearity </h2>
<p>
The cases we have studied till were all characterized by two classes
@@ -795,7 +821,7 @@ plt.show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec17">The equations </h2>
<h2 id="___sec19">The equations </h2>
<p>
Suppose we define a polynomial transformation of degree two only (we continue to live in a plane with \( x_i \) and \( y_i \) as variables)
@@ -837,7 +863,7 @@ the trouble of performing the transformation
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec18">The problem to solve </h2>
<h2 id="___sec20">The problem to solve </h2>
Using our definition of the kernel We can rewrite again the Lagrangian
$$
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{z}_j,
@@ -873,7 +899,7 @@ Given a kernel \( K \) and the targets \( y_i \) this matrix is easy to set up.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec19">Different kernels and Mercer's theorem </h2>
<h2 id="___sec21">Different kernels and Mercer's theorem </h2>
<p>
There are several popular kernels being used. These are
@@ -904,7 +930,7 @@ well in practice.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec20">The moons example </h2>
<h2 id="___sec22">The moons example </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
@@ -1100,7 +1126,7 @@ plt.show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec21">Mathematical optimization of convex functions </h2>
<h2 id="___sec23">Mathematical optimization of convex functions </h2>
<p>
A mathematical (quadratic) optimization problem, or just optimization problem, has the form
@@ -1125,7 +1151,7 @@ Convex optimization problems play a central role in applied mathematics and we r
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec22">How do we solve these problems? </h2>
<h2 id="___sec24">How do we solve these problems? </h2>
<p>
If we use Python as programming language and wish to venture beyond
@@ -1151,7 +1177,7 @@ This will make our life much easier. You don't need t write your own optimizer.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec23">A simple example </h2>
<h2 id="___sec25">A simple example </h2>
<p>
We remind ourselves about the general problem we want to solve
@@ -1219,7 +1245,7 @@ sol[<span style="color: #a61717; background-color: #e3d2d2"></span>primal obj
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec24">Back to the more realistic cases </h2>
<h2 id="___sec26">Back to the more realistic cases </h2>
<p>
We are now ready to return to our setup of the optmization problem for a more realistic case. Introducing the <b>slack</b> parameter \( C \) we have
@@ -1242,7 +1268,7 @@ With the slack constants this leads to the additional constraint \( 0\leq \lamb
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec25">Code Example </h2>
<h2 id="___sec27">Code Example </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
+68 -42
View File
@@ -48,30 +48,32 @@ div { text-align: justify; text-justify: inter-word; }
('Getting into the details', 2, None, '___sec5'),
('First attempt at a minimization approach', 2, None, '___sec6'),
('Solving the equations', 2, None, '___sec7'),
('A better approach', 2, None, '___sec8'),
('Code Example', 2, None, '___sec8'),
('Problems with the Simpler Approach', 2, None, '___sec9'),
('A better approach', 2, None, '___sec10'),
('A quick reminder on Lagrangian multipliers',
2,
None,
'___sec9'),
('Adding the muliplier', 2, None, '___sec10'),
('Setting up the problem', 2, None, '___sec11'),
('The problem to solve', 2, None, '___sec12'),
('The last steps', 2, None, '___sec13'),
('A soft classifier', 2, None, '___sec14'),
('Soft optmization problem', 2, None, '___sec15'),
('Kernels and non-linearity', 2, None, '___sec16'),
('The equations', 2, None, '___sec17'),
('The problem to solve', 2, None, '___sec18'),
("Different kernels and Mercer's theorem", 2, None, '___sec19'),
('The moons example', 2, None, '___sec20'),
'___sec11'),
('Adding the muliplier', 2, None, '___sec12'),
('Setting up the problem', 2, None, '___sec13'),
('The problem to solve', 2, None, '___sec14'),
('The last steps', 2, None, '___sec15'),
('A soft classifier', 2, None, '___sec16'),
('Soft optmization problem', 2, None, '___sec17'),
('Kernels and non-linearity', 2, None, '___sec18'),
('The equations', 2, None, '___sec19'),
('The problem to solve', 2, None, '___sec20'),
("Different kernels and Mercer's theorem", 2, None, '___sec21'),
('The moons example', 2, None, '___sec22'),
('Mathematical optimization of convex functions',
2,
None,
'___sec21'),
('How do we solve these problems?', 2, None, '___sec22'),
('A simple example', 2, None, '___sec23'),
('Back to the more realistic cases', 2, None, '___sec24'),
('Code Example', 2, None, '___sec25')]}
'___sec23'),
('How do we solve these problems?', 2, None, '___sec24'),
('A simple example', 2, None, '___sec25'),
('Back to the more realistic cases', 2, None, '___sec26'),
('Code Example', 2, None, '___sec27')]}
end of tocinfo -->
<body>
@@ -113,7 +115,7 @@ MathJax.Hub.Config({
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
<br>
<p>
<center><h4>Nov 15, 2019</h4></center> <!-- date -->
<center><h4>Nov 16, 2019</h4></center> <!-- date -->
<br>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
@@ -382,7 +384,7 @@ $$
<h2 id="___sec7">Solving the equations </h2>
<p>
We can now use the Newton-Raphson method or gradient descent to solve the equations
We can now use the Newton-Raphson method or different variants of the gradient descent family (from plain gradient descent to various stochastic gradient descent approaches) to solve the equations
$$
b \leftarrow b +\eta \frac{\partial C}{\partial b},
$$
@@ -394,11 +396,32 @@ $$
where \( \eta \) is our by now well-known learning rate.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec8">Code Example </h2>
<p>
The equations we discussed above can be coded rather easily (the
framework is rather similar to what we developed for logistic
regression). We use the standard moons example to set up our data
set. We want to find a line which separates the two classes
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span>
</pre></div>
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec9">Problems with the Simpler Approach </h2>
<p>
There are however problems with this approach, although it looks
pretty straightforward to implement. In case we separate our data into
two distinct classes, we may up with many possible lines, as indicated
in the figure and shown by running the following program. For small
pretty straightforward to implement. When running the above code, we see that we can easily end up with many diffeent lines which separate the two classes.
<p>
For small
gaps between the entries, we may also end up needing many iterations
before the solutions converge and if the data cannot be separated
properly into two distinct classes, we may not experience a converge
@@ -407,7 +430,7 @@ at all.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec8">A better approach </h2>
<h2 id="___sec10">A better approach </h2>
<p>
A better approach is rather to try to define a large margin between
@@ -441,12 +464,15 @@ y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b) \geq 1 \hspace{0.1cm}\forall i.
$$
<p>
We have thus defined our margin as the invers of the norm of \( \boldsymbol{w} \). We want to minimize the norm in order to have a as large as possible margin \( M \). Before we proceed, we need to remind ourselves about Lagrangian multipliers.
We have thus defined our margin as the invers of the norm of
\( \boldsymbol{w} \). We want to minimize the norm in order to have a as large as
possible margin \( M \). Before we proceed, we need to remind ourselves
about Lagrangian multipliers.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec9">A quick reminder on Lagrangian multipliers </h2>
<h2 id="___sec11">A quick reminder on Lagrangian multipliers </h2>
<p>
Consider a function of three independent variables \( f(x,y,z) \) . For the function \( f \) to be an
@@ -496,7 +522,7 @@ Then \( dz \) is no longer arbitrary.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec10">Adding the muliplier </h2>
<h2 id="___sec12">Adding the muliplier </h2>
<p>
However, we can add to
@@ -539,7 +565,7 @@ $$
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec11">Setting up the problem </h2>
<h2 id="___sec13">Setting up the problem </h2>
In order to solve the above problem, we define the following Lagrangian function to be minimized
$$
{\cal L}(\lambda,b,\boldsymbol{w})=\frac{1}{2}\boldsymbol{w}^T\boldsymbol{w}-\sum_{i=1}^n\lambda_i\left[y_i(\boldsymbol{w}^T\boldsymbol{x}_i+b)-1\right],
@@ -580,7 +606,7 @@ When \( \lambda_i > 0 \), the vectors \( \boldsymbol{x}_i \) are called support
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec12">The problem to solve </h2>
<h2 id="___sec14">The problem to solve </h2>
<p>
We can rewrite
@@ -604,7 +630,7 @@ subject to \( \boldsymbol{y}^T\boldsymbol{\lambda}=0 \). Here we defined the vec
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec13">The last steps </h2>
<h2 id="___sec15">The last steps </h2>
<p>
Solving the above problem, yields the values of \( \lambda_i \).
@@ -638,7 +664,7 @@ Below we discuss how to find the optimal values of \( \lambda_i \). Before we pr
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec14">A soft classifier </h2>
<h2 id="___sec16">A soft classifier </h2>
<p>
Till now, the margin is strictly defined by the support vectors. This defines what is called a hard classifier, that is the margins are well defined.
@@ -673,7 +699,7 @@ misclassifications.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec15">Soft optmization problem </h2>
<h2 id="___sec17">Soft optmization problem </h2>
<p>
This has in turn the consequences that we change our optmization problem to finding the minimum of
@@ -727,7 +753,7 @@ $$
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec16">Kernels and non-linearity </h2>
<h2 id="___sec18">Kernels and non-linearity </h2>
<p>
The cases we have studied till were all characterized by two classes
@@ -800,7 +826,7 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec17">The equations </h2>
<h2 id="___sec19">The equations </h2>
<p>
Suppose we define a polynomial transformation of degree two only (we continue to live in a plane with \( x_i \) and \( y_i \) as variables)
@@ -842,7 +868,7 @@ the trouble of performing the transformation
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec18">The problem to solve </h2>
<h2 id="___sec20">The problem to solve </h2>
Using our definition of the kernel We can rewrite again the Lagrangian
$$
{\cal L}=\sum_i\lambda_i-\frac{1}{2}\sum_{ij}^n\lambda_i\lambda_jy_iy_j\boldsymbol{x}_i^T\boldsymbol{z}_j,
@@ -878,7 +904,7 @@ Given a kernel \( K \) and the targets \( y_i \) this matrix is easy to set up.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec19">Different kernels and Mercer's theorem </h2>
<h2 id="___sec21">Different kernels and Mercer's theorem </h2>
<p>
There are several popular kernels being used. These are
@@ -909,7 +935,7 @@ well in practice.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec20">The moons example </h2>
<h2 id="___sec22">The moons example </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
@@ -1105,7 +1131,7 @@ plt<span style="color: #666666">.</span>show()
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec21">Mathematical optimization of convex functions </h2>
<h2 id="___sec23">Mathematical optimization of convex functions </h2>
<p>
A mathematical (quadratic) optimization problem, or just optimization problem, has the form
@@ -1130,7 +1156,7 @@ Convex optimization problems play a central role in applied mathematics and we r
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec22">How do we solve these problems? </h2>
<h2 id="___sec24">How do we solve these problems? </h2>
<p>
If we use Python as programming language and wish to venture beyond
@@ -1156,7 +1182,7 @@ This will make our life much easier. You don't need t write your own optimizer.
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec23">A simple example </h2>
<h2 id="___sec25">A simple example </h2>
<p>
We remind ourselves about the general problem we want to solve
@@ -1224,7 +1250,7 @@ sol[primal objective]
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec24">Back to the more realistic cases </h2>
<h2 id="___sec26">Back to the more realistic cases </h2>
<p>
We are now ready to return to our setup of the optmization problem for a more realistic case. Introducing the <b>slack</b> parameter \( C \) we have
@@ -1247,7 +1273,7 @@ With the slack constants this leads to the additional constraint \( 0\leq \lamb
<p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="___sec25">Code Example </h2>
<h2 id="___sec27">Code Example </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
Binary file not shown.
+26 -6
View File
@@ -10,7 +10,7 @@
"<!-- Author: --> \n",
"**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n",
"\n",
"Date: **Nov 15, 2019**\n",
"Date: **Nov 16, 2019**\n",
"\n",
"Copyright 1999-2019, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
"\n",
@@ -385,7 +385,7 @@
"source": [
"## Solving the equations\n",
"\n",
"We can now use the Newton-Raphson method or gradient descent to solve the equations"
"We can now use the Newton-Raphson method or different variants of the gradient descent family (from plain gradient descent to various stochastic gradient descent approaches) to solve the equations"
]
},
{
@@ -419,10 +419,27 @@
"source": [
"where $\\eta$ is our by now well-known learning rate. \n",
"\n",
"\n",
"## Code Example\n",
"\n",
"The equations we discussed above can be coded rather easily (the\n",
"framework is rather similar to what we developed for logistic\n",
"regression). We use the standard moons example to set up our data\n",
"set. We want to find a line which separates the two classes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problems with the Simpler Approach\n",
"\n",
"\n",
"There are however problems with this approach, although it looks\n",
"pretty straightforward to implement. In case we separate our data into\n",
"two distinct classes, we may up with many possible lines, as indicated\n",
"in the figure and shown by running the following program. For small\n",
"pretty straightforward to implement. When running the above code, we see that we can easily end up with many diffeent lines which separate the two classes.\n",
"\n",
"\n",
"For small\n",
"gaps between the entries, we may also end up needing many iterations\n",
"before the solutions converge and if the data cannot be separated\n",
"properly into two distinct classes, we may not experience a converge\n",
@@ -501,7 +518,10 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We have thus defined our margin as the invers of the norm of $\\boldsymbol{w}$. We want to minimize the norm in order to have a as large as possible margin $M$. Before we proceed, we need to remind ourselves about Lagrangian multipliers. \n",
"We have thus defined our margin as the invers of the norm of\n",
"$\\boldsymbol{w}$. We want to minimize the norm in order to have a as large as\n",
"possible margin $M$. Before we proceed, we need to remind ourselves\n",
"about Lagrangian multipliers.\n",
"\n",
"## A quick reminder on Lagrangian multipliers\n",
"\n",
Binary file not shown.
+25 -10
View File
@@ -110,11 +110,6 @@ plt.show()
!ec
@@ -260,7 +255,7 @@ and
!split
===== Solving the equations =====
We can now use the Newton-Raphson method or gradient descent to solve the equations
We can now use the Newton-Raphson method or different variants of the gradient descent family (from plain gradient descent to various stochastic gradient descent approaches) to solve the equations
!bt
\[
b \leftarrow b +\eta \frac{\partial C}{\partial b},
@@ -274,10 +269,27 @@ and
!et
where $\eta$ is our by now well-known learning rate.
!split
===== Code Example =====
The equations we discussed above can be coded rather easily (the
framework is rather similar to what we developed for logistic
regression). We use the standard moons example to set up our data
set. We want to find a line which separates the two classes
!bc pycod
!ec
!split
===== Problems with the Simpler Approach =====
There are however problems with this approach, although it looks
pretty straightforward to implement. In case we separate our data into
two distinct classes, we may up with many possible lines, as indicated
in the figure and shown by running the following program. For small
pretty straightforward to implement. When running the above code, we see that we can easily end up with many diffeent lines which separate the two classes.
For small
gaps between the entries, we may also end up needing many iterations
before the solutions converge and if the data cannot be separated
properly into two distinct classes, we may not experience a converge
@@ -319,7 +331,10 @@ y_i(\bm{w}^T\bm{x}_i+b) \geq 1 \hspace{0.1cm}\forall i.
\]
!et
We have thus defined our margin as the invers of the norm of $\bm{w}$. We want to minimize the norm in order to have a as large as possible margin $M$. Before we proceed, we need to remind ourselves about Lagrangian multipliers.
We have thus defined our margin as the invers of the norm of
$\bm{w}$. We want to minimize the norm in order to have a as large as
possible margin $M$. Before we proceed, we need to remind ourselves
about Lagrangian multipliers.
!split
===== A quick reminder on Lagrangian multipliers =====