ml-finance-python

python scripts for finance machine learning

git clone https://9o.is/git/ml-finance-python.git

perceptron.py

(2679B)


      1 from __future__ import print_function, division
      2 import math
      3 import numpy as np
      4 
      5 # Import helper functions
      6 from mlfromscratch.utils import train_test_split, to_categorical, normalize, accuracy_score
      7 from mlfromscratch.deep_learning.activation_functions import Sigmoid, ReLU, SoftPlus, LeakyReLU, TanH, ELU
      8 from mlfromscratch.deep_learning.loss_functions import CrossEntropy, SquareLoss
      9 from mlfromscratch.utils import Plot
     10 from mlfromscratch.utils.misc import bar_widgets
     11 import progressbar
     12 
     13 class Perceptron():
     14     """The Perceptron. One layer neural network classifier.
     15 
     16     Parameters:
     17     -----------
     18     n_iterations: float
     19         The number of training iterations the algorithm will tune the weights for.
     20     activation_function: class
     21         The activation that shall be used for each neuron.
     22         Possible choices: Sigmoid, ExpLU, ReLU, LeakyReLU, SoftPlus, TanH
     23     loss: class
     24         The loss function used to assess the model's performance.
     25         Possible choices: SquareLoss, CrossEntropy
     26     learning_rate: float
     27         The step length that will be used when updating the weights.
     28     """
     29     def __init__(self, n_iterations=20000, activation_function=Sigmoid, loss=SquareLoss, learning_rate=0.01):
     30         self.n_iterations = n_iterations
     31         self.learning_rate = learning_rate
     32         self.loss = loss()
     33         self.activation_func = activation_function()
     34         self.progressbar = progressbar.ProgressBar(widgets=bar_widgets)
     35 
     36     def fit(self, X, y):
     37         n_samples, n_features = np.shape(X)
     38         _, n_outputs = np.shape(y)
     39 
     40         # Initialize weights between [-1/sqrt(N), 1/sqrt(N)]
     41         limit = 1 / math.sqrt(n_features)
     42         self.W = np.random.uniform(-limit, limit, (n_features, n_outputs))
     43         self.w0 = np.zeros((1, n_outputs))
     44 
     45         for i in self.progressbar(range(self.n_iterations)):
     46             # Calculate outputs
     47             linear_output = X.dot(self.W) + self.w0
     48             y_pred = self.activation_func(linear_output)
     49             # Calculate the loss gradient w.r.t the input of the activation function
     50             error_gradient = self.loss.gradient(y, y_pred) * self.activation_func.gradient(linear_output)
     51             # Calculate the gradient of the loss with respect to each weight
     52             grad_wrt_w = X.T.dot(error_gradient)
     53             grad_wrt_w0 = np.sum(error_gradient, axis=0, keepdims=True)
     54             # Update weights
     55             self.W  -= self.learning_rate * grad_wrt_w
     56             self.w0 -= self.learning_rate  * grad_wrt_w0
     57 
     58     # Use the trained model to predict labels of X
     59     def predict(self, X):
     60         y_pred = self.activation_func(X.dot(self.W) + self.w0)
     61         return y_pred