Skip to content

Instantly share code, notes, and snippets.

View emilemathieu's full-sized avatar

Mathieue emilemathieu

View GitHub Profile
class Module(object):
""" Base class for neural network's layers
"""
def forward(self, X):
""" Apply the layer function to the input data
Parameters
----------
X : array-like, shape = [n_samples, depth_in, height_in, width_in]
Returns
-------
def _compute_intercept(self, alpha, yg):
indices = (alpha < self.C) * (alpha > 0)
return np.mean(yg[indices])
def _compute_weights(self, X, y):
iteration = 0
n_samples = X.shape[0]
alpha = np.zeros(n_samples) # Initialise coefficients to 0 w
g = np.ones(n_samples) # Initialise gradients to 1
import numpy as np
class Kernel(object):
"""Collection of usual kernels"""
@staticmethod
def linear():
def f(x, y):
return np.inner(x, y)
return f
class binary_classification(object):
def __init__(self, kernel, C=1.0, max_iter=1000, tol=0.001):
self.kernel = kernel # K(x_i, x_j) = <phi(x_i), phi(x_j)>
self.C = C # penalty coefficient
self.max_iter = max_iter # maximum number of iterations for solver
self.tol = tol # tolerance for the solver
def fit(self, X, y):
# Compute coefficients of the dual problem
lagrange_multipliers, intercept = self._compute_weights(X, y)