ml-finance-python

python scripts for finance machine learning

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

recurrent_neural_network.py

(3225B)


      1 from __future__ import print_function
      2 import matplotlib.pyplot as plt
      3 import numpy as np
      4 
      5 from mlfromscratch.deep_learning import NeuralNetwork
      6 from mlfromscratch.utils import train_test_split, to_categorical, normalize, Plot
      7 from mlfromscratch.utils import get_random_subsets, shuffle_data, accuracy_score
      8 from mlfromscratch.deep_learning.optimizers import StochasticGradientDescent, Adam, RMSprop, Adagrad, Adadelta
      9 from mlfromscratch.deep_learning.loss_functions import CrossEntropy
     10 from mlfromscratch.utils.misc import bar_widgets
     11 from mlfromscratch.deep_learning.layers import RNN, Activation
     12 
     13 
     14 def main():
     15 
     16     optimizer = Adam()
     17 
     18     def gen_mult_ser(nums):
     19         """ Method which generates multiplication series """
     20         X = np.zeros([nums, 10, 61], dtype=float)
     21         y = np.zeros([nums, 10, 61], dtype=float)
     22         for i in range(nums):
     23             start = np.random.randint(2, 7)
     24             mult_ser = np.linspace(start, start*10, num=10, dtype=int)
     25             X[i] = to_categorical(mult_ser, n_col=61)
     26             y[i] = np.roll(X[i], -1, axis=0)
     27         y[:, -1, 1] = 1 # Mark endpoint as 1
     28         return X, y
     29 
     30 
     31     def gen_num_seq(nums):
     32         """ Method which generates sequence of numbers """
     33         X = np.zeros([nums, 10, 20], dtype=float)
     34         y = np.zeros([nums, 10, 20], dtype=float)
     35         for i in range(nums):
     36             start = np.random.randint(0, 10)
     37             num_seq = np.arange(start, start+10)
     38             X[i] = to_categorical(num_seq, n_col=20)
     39             y[i] = np.roll(X[i], -1, axis=0)
     40         y[:, -1, 1] = 1 # Mark endpoint as 1
     41         return X, y
     42 
     43     X, y = gen_mult_ser(3000)
     44     X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
     45 
     46     # Model definition
     47     clf = NeuralNetwork(optimizer=optimizer,
     48                         loss=CrossEntropy)
     49     clf.add(RNN(10, activation="tanh", bptt_trunc=5, input_shape=(10, 61)))
     50     clf.add(Activation('softmax'))
     51     clf.summary("RNN")
     52 
     53     # Print a problem instance and the correct solution
     54     tmp_X = np.argmax(X_train[0], axis=1)
     55     tmp_y = np.argmax(y_train[0], axis=1)
     56     print ("Number Series Problem:")
     57     print ("X = [" + " ".join(tmp_X.astype("str")) + "]")
     58     print ("y = [" + " ".join(tmp_y.astype("str")) + "]")
     59     print ()
     60 
     61     train_err, _ = clf.fit(X_train, y_train, n_epochs=500, batch_size=512)
     62 
     63     # Predict labels of the test data
     64     y_pred = np.argmax(clf.predict(X_test), axis=2)
     65     y_test = np.argmax(y_test, axis=2)
     66 
     67     print ()
     68     print ("Results:")
     69     for i in range(5):
     70         # Print a problem instance and the correct solution
     71         tmp_X = np.argmax(X_test[i], axis=1)
     72         tmp_y1 = y_test[i]
     73         tmp_y2 = y_pred[i]
     74         print ("X      = [" + " ".join(tmp_X.astype("str")) + "]")
     75         print ("y_true = [" + " ".join(tmp_y1.astype("str")) + "]")
     76         print ("y_pred = [" + " ".join(tmp_y2.astype("str")) + "]")
     77         print ()
     78     
     79     accuracy = np.mean(accuracy_score(y_test, y_pred))
     80     print ("Accuracy:", accuracy)
     81 
     82     training = plt.plot(range(500), train_err, label="Training Error")
     83     plt.title("Error Plot")
     84     plt.ylabel('Training Error')
     85     plt.xlabel('Iterations')
     86     plt.show()
     87 
     88 if __name__ == "__main__":
     89     main()