ml-finance-python
python scripts for finance machine learning
git clone https://9o.is/git/ml-finance-python.git
neural_network.py
(4750B)
1 from __future__ import print_function, division
2 from terminaltables import AsciiTable
3 import numpy as np
4 import progressbar
5 from mlfromscratch.utils import batch_iterator
6 from mlfromscratch.utils.misc import bar_widgets
7
8
9 class NeuralNetwork():
10 """Neural Network. Deep Learning base model.
11
12 Parameters:
13 -----------
14 optimizer: class
15 The weight optimizer that will be used to tune the weights in order of minimizing
16 the loss.
17 loss: class
18 Loss function used to measure the model's performance. SquareLoss or CrossEntropy.
19 validation: tuple
20 A tuple containing validation data and labels (X, y)
21 """
22 def __init__(self, optimizer, loss, validation_data=None):
23 self.optimizer = optimizer
24 self.layers = []
25 self.errors = {"training": [], "validation": []}
26 self.loss_function = loss()
27 self.progressbar = progressbar.ProgressBar(widgets=bar_widgets)
28
29 self.val_set = None
30 if validation_data:
31 X, y = validation_data
32 self.val_set = {"X": X, "y": y}
33
34 def set_trainable(self, trainable):
35 """ Method which enables freezing of the weights of the network's layers. """
36 for layer in self.layers:
37 layer.trainable = trainable
38
39 def add(self, layer):
40 """ Method which adds a layer to the neural network """
41 # If this is not the first layer added then set the input shape
42 # to the output shape of the last added layer
43 if self.layers:
44 layer.set_input_shape(shape=self.layers[-1].output_shape())
45
46 # If the layer has weights that needs to be initialized
47 if hasattr(layer, 'initialize'):
48 layer.initialize(optimizer=self.optimizer)
49
50 # Add layer to the network
51 self.layers.append(layer)
52
53 def test_on_batch(self, X, y):
54 """ Evaluates the model over a single batch of samples """
55 y_pred = self._forward_pass(X, training=False)
56 loss = np.mean(self.loss_function.loss(y, y_pred))
57 acc = self.loss_function.acc(y, y_pred)
58
59 return loss, acc
60
61 def train_on_batch(self, X, y):
62 """ Single gradient update over one batch of samples """
63 y_pred = self._forward_pass(X)
64 loss = np.mean(self.loss_function.loss(y, y_pred))
65 acc = self.loss_function.acc(y, y_pred)
66 # Calculate the gradient of the loss function wrt y_pred
67 loss_grad = self.loss_function.gradient(y, y_pred)
68 # Backpropagate. Update weights
69 self._backward_pass(loss_grad=loss_grad)
70
71 return loss, acc
72
73 def fit(self, X, y, n_epochs, batch_size):
74 """ Trains the model for a fixed number of epochs """
75 for _ in self.progressbar(range(n_epochs)):
76
77 batch_error = []
78 for X_batch, y_batch in batch_iterator(X, y, batch_size=batch_size):
79 loss, _ = self.train_on_batch(X_batch, y_batch)
80 batch_error.append(loss)
81
82 self.errors["training"].append(np.mean(batch_error))
83
84 if self.val_set is not None:
85 val_loss, _ = self.test_on_batch(self.val_set["X"], self.val_set["y"])
86 self.errors["validation"].append(val_loss)
87
88 return self.errors["training"], self.errors["validation"]
89
90 def _forward_pass(self, X, training=True):
91 """ Calculate the output of the NN """
92 layer_output = X
93 for layer in self.layers:
94 layer_output = layer.forward_pass(layer_output, training)
95
96 return layer_output
97
98 def _backward_pass(self, loss_grad):
99 """ Propagate the gradient 'backwards' and update the weights in each layer """
100 for layer in reversed(self.layers):
101 loss_grad = layer.backward_pass(loss_grad)
102
103 def summary(self, name="Model Summary"):
104 # Print model name
105 print (AsciiTable([[name]]).table)
106 # Network input shape (first layer's input shape)
107 print ("Input Shape: %s" % str(self.layers[0].input_shape))
108 # Iterate through network and get each layer's configuration
109 table_data = [["Layer Type", "Parameters", "Output Shape"]]
110 tot_params = 0
111 for layer in self.layers:
112 layer_name = layer.layer_name()
113 params = layer.parameters()
114 out_shape = layer.output_shape()
115 table_data.append([layer_name, str(params), str(out_shape)])
116 tot_params += params
117 # Print network configuration table
118 print (AsciiTable(table_data).table)
119 print ("Total Parameters: %d\n" % tot_params)
120
121 def predict(self, X):
122 """ Use the trained model to predict labels of X """
123 return self._forward_pass(X, training=False)