🌐
scikit-learn
scikit-learn.org › stable › modules › sgd.html
1.5. Stochastic Gradient Descent — scikit-learn 1.8.0 documentation
The class sklearn.linear_model.SGDOneClassSVM implements an online linear version of the One-Class SVM using a stochastic gradient descent.
🌐
YouTube
youtube.com › watch
7.3.5. Gradient Descent for Support Vector Machine Classifier - YouTube
Hi! I will be conducting one-on-one discussion with all channel members. Checkout the perks and Join membership if interested: https://www.youtube.com/channe...
Published   November 3, 2021
🌐
GitHub
github.com › qandeelabbassi › python-svm-sgd
GitHub - qandeelabbassi/python-svm-sgd: Python implementation of stochastic sub-gradient descent algorithm for SVM from scratch
Python implementation of stochastic gradient descent algorithm for SVM from scratch
Starred by 37 users
Forked by 22 users
Languages   Python 100.0% | Python 100.0%
🌐
MaviccPRP@web.studio
maviccprp.github.io › a-support-vector-machine-in-just-a-few-lines-of-python-code
A Support Vector Machine in just a few Lines of Python Code
April 3, 2017 - As for the perceptron, we use python 3 and numpy. The SVM will learn using the stochastic gradient descent algorithm (SGD). SGD minimizes a function by following the gradients of the cost function.
🌐
Medium
fordcombs.medium.com › svm-from-scratch-step-by-step-in-python-f1e2d5b9c5be
SVM from scratch: step by step in Python | by Ford Combs | Medium
May 23, 2020 - SVM from scratch: step by step in Python How to build a support vector machine using the Pegasos algorithm for stochastic gradient descent. All of the code can be found here …
🌐
Kaggle
kaggle.com › code › residentmario › support-vector-machines-and-stoch-gradient-descent
Support vector machines and stoch gradient descent
Checking your browser before accessing www.kaggle.com · Click here if you are not automatically redirected after 5 seconds
🌐
Medium
medium.com › data-science › implementing-svm-from-scratch-784e4ad0bc6a
Implementing Support Vector Machine From Scratch | by Marvin Lanhenke | TDS Archive | Medium
May 1, 2022 - Perform gradient descent for n iterations, which involves the computation of the gradients and updating the weights and biases accordingly. ... Since the third step consists of multiple actions, we will break it down into several helper functions. The algorithm will be implemented in a single class with just Python and Numpy.
🌐
GitHub
github.com › rickysu123 › Support-Vector-Machine
GitHub - rickysu123/Support-Vector-Machine: Implementation of Support Vector Machine using Stochastic Gradient Descent
These gradients are then implemented into our weights and bias to fit the data better. ************ Instructions *** python SVM.py {test file} ************ Results ******* My best result was from a 0.45 learning rate, 0.0127 capacity C, and 4 epochs yielding an accuracy of 85.1125% on the dev ...
Author   rickysu123
Find elsewhere
🌐
YouTube
youtube.com › watch
Support Vector Machine SVM with Gradient Descent Step by Step Numerical Example + Python - YouTube
🚀 Welcome to this comprehensive tutorial on Support Vector Machines (SVM) Training with Gradient Descent! 🚀In this video, we’ll break down the step-by-step...
Published   March 8, 2025
🌐
MIT CSAIL
people.csail.mit.edu › dsontag › courses › ml16 › slides › lecture5.pdf pdf
Support vector machines (SVMs) Lecture 5 David Sontag
So5 margin SVM · Subgradient · (for non-­‐differenNable funcNons) (Sub)gradient descent of SVM objecNve
🌐
University of Utah
users.cs.utah.edu › ~zhe › pdf › lec-19-2-svm-sgd-upload.pdf pdf
1 Support Vector Machines: Training with Stochastic Gradient Descent
Outline: Training SVM by optimization · 1. Review of convex functions and gradient descent · 2. Stochastic gradient descent · 3. Gradient descent vs stochastic gradient descent · 4. Sub-derivatives of the hinge loss · 5. Stochastic sub-gradient descent for SVM ·
🌐
University of Oxford
robots.ox.ac.uk › ~florian › teaching › classification.html
Practical 2: Scalable Methods for Classification
You can test this with python run_test.py TestObj_Logistic_Gradient. Once the tests pass, you can use a torch vectorized implementation as documented here. With a cross-entropy loss, the objective function is smooth, therefore we can use gradient descent (GD) with a constant step-size.
🌐
CODEBUG
sijanb.com.np › posts › implementation-of-stochastic-subgradient-descent-for-support-vector-machine-using-python
Implementation of stochastic subgradient descent for support vector machine using Python | CODEBUG
May 26, 2019 - def stochastic_subgrad_descent(data, initial_values, B, C, T=1): """ :data: Pandas data frame :initial_values: initialization for w and b :B: sample size for random data selection :C: hyperparameter, tradeoff between hard margin and hinge loss :T: # of iterations """ w, b = initial_values for t in range(1, T+1): # randomly select B data points training_sample = data.sample(B) # set learning rate learning_rate = 1/t # prepare inputs in the form [[h1, w1], [h2, w2], ....] x = training_sample[['height', 'weight']].values # prepare labels in the form [1, -1, 1, 1, - 1 ......] y = training_sample['gender'].values sub_grads = subgradients(x,y, w, b, C) # update weights w = w - learning_rate * sub_grads[0] # update bias b = b - learning_rate * sub_grads[1] return (w,b)
🌐
GitHub
github.com › MarRist › SVM-with-Stochastic-Gradient-Descent
GitHub - MarRist/SVM-with-Stochastic-Gradient-Descent: This repository contains projects that were written for Machine Learning course at University of Toronto
This is an implementation of Stochastic Gradient Descent with momentum β and learning rate α. The implemented algorithm is then used to approximately optimize the SVM objective.
Starred by 2 users
Forked by 2 users
Languages   Python 100.0% | Python 100.0%
🌐
scikit-learn
scikit-learn.org › stable › modules › generated › sklearn.linear_model.SGDClassifier.html
SGDClassifier — scikit-learn 1.8.0 documentation
This estimator implements regularized ... (SGD) learning: the gradient of the loss is estimated each sample at a time and the model is updated along the way with a decreasing strength schedule (aka learning rate)....
🌐
Quora
quora.com › Can-an-SVM-use-gradient-descent-to-minimize-its-margin-instead-of-using-a-lagrange
Can an SVM use gradient descent to minimize its margin instead of using a lagrange? - Quora
They are used for different purposes. * Gradient descent, in its vanilla form, minimizes an unconstrained optimization problem. To handle constraints, you can use some modifications like projected gradient...
Top answer
1 of 1
4

When SVMs and SGD can't be combined

SVMs are often used in combination with the kernel trick, which enables classification of non-linearly separable data. This answer explains why you wouldn't use stochastic gradient descent to solve a kernelised SVM: https://stats.stackexchange.com/questions/215524/is-gradient-descent-possible-for-kernelized-svms-if-so-why-do-people-use-quadr

Linear SVMs

If we stick to Linear SVMs, then we can run an experiment using sklearn, as it provides wrappers over libsvm (SVC), liblinear (LinearSVC) and also offers the SGDClassifier. Recommend reading the linked documentation of libsvm and liblinear to understand what is happening under the hood.

Comparison on example dataset

Below is a comparison of computational performance and accuracy over a randomly generated dataset (which may not be representative of your problem). You should alter the problem to fit your requirements.

import time
import numpy as np
import matplotlib.pyplot as plt

from sklearn.svm import SVC, LinearSVC
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import train_test_split

# Randomly generated dataset
# Linear function + noise
np.random.seed(0)
X = np.random.normal(size=(50000, 10))
coefs = np.random.normal(size=10)
epsilon = np.random.normal(size=50000)
y = (X @ coefs + epsilon) > 0

# Classifiers to compare
algos = {
    'LibSVM': {
        'model': SVC(),
        'max_n': 4000,
        'time': [],
        'error': []
    },
    'LibLinear': {
        'model': LinearSVC(dual=False),
        'max_n': np.inf,
        'time': [],
        'error': []
    },
    'SGD': {
        'model': SGDClassifier(max_iter=1000, tol=1e-3),
        'max_n': np.inf,
        'time': [],
        'error': []
    }
}

splits = list(range(100, 1000, 100)) + \
         list(range(1500, 5000, 500)) + \
         list(range(6000, 50000, 1000))
for i in splits:
    X_train, X_test, y_train, y_test = train_test_split(X, y,
                                                        test_size=1-i/50000,
                                                        random_state=0)
    for k, v in algos.items():
        if i < v['max_n']:
            model = v['model']
            t0 = time.time()
            model.fit(X_train, y_train)
            t1 = time.time()
            v['time'].append(t1 - t0)
            preds = model.predict(X_test)
            e = (preds != y_test).sum() / len(y_test)
            v['error'].append(e)

Plotting the results, we see that the traditional libsvm solver cannot be used on large n, while the liblinear and SGD implementations scale well computationally.

plt.figure()
for k, v in algos.items():
    plt.plot(splits[:len(v['time'])], v['time'], label='{} time'.format(k))
plt.legend()
plt.semilogx()
plt.title('Time comparison')
plt.show()

Plotting the error, we see that SGD is worse than LibSVM for the same training set, but if you have a large training set this becomes a minor point. The liblinear algorithm performs best on this dataset:

plt.figure()
for k, v in algos.items():
    plt.plot(splits[:len(v['error'])], v['error'], label='{} error'.format(k))
plt.legend()
plt.semilogx()
plt.title('Error comparison')
plt.xlabel('Number of training examples')
plt.ylabel('Error')
plt.show()

🌐
scikit-learn
scikit-learn.org › stable › auto_examples › linear_model › plot_sgdocsvm_vs_ocsvm.html
One-Class SVM versus One-Class SVM using Stochastic Gradient Descent — scikit-learn 1.8.0 documentation
import matplotlib import matplotlib.lines as mlines import matplotlib.pyplot as plt import numpy as np from sklearn.kernel_approximation import Nystroem from sklearn.linear_model import SGDOneClassSVM from sklearn.pipeline import make_pipeline from sklearn.svm import OneClassSVM font = {"weight": "normal", "size": 15} matplotlib.rc("font", **font) random_state = 42 rng = np.random.RandomState(random_state) # Generate train data X = 0.3 * rng.randn(500, 2) X_train = np.r_[X + 2, X - 2] # Generate some regular novel observations X = 0.3 * rng.randn(20, 2) X_test = np.r_[X + 2, X - 2] # Generate