This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
NewerOlder