-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultilayerNNsimple.py
126 lines (88 loc) · 2.76 KB
/
multilayerNNsimple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#importing libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn.preprocessing import normalize
import sklearn
import sklearn.datasets
import sklearn.linear_model
%matplotlib inline
np.random.seed(1)
#load dataset
dataset = pd.read_csv("/Users/samar/Documents/pythonwd/special-potato/AXISBANK.csv")
#Clean dataset
#adding t1 value or the next day value
dataset["t1"] = dataset["4. close"].shift(-1)
#removing last NaN - because nobody likes naan!
dataset = dataset[0:(dataset.shape[0]-1)]
#adding p1 as change between t1 and t0
dataset["p1"] = (dataset["t1"] - dataset["4. close"])
#changing p1 to 1 if change was +ve and 0 if - ve
dataset["p1"] = np.where(dataset["p1"] > 0, 1,0)
#cleaning out dates and dataframe
X = dataset[["1. open","2. high", "3. low" ,"4. close", "5. volume"]]
def normalize(df):
G = preprocessing.StandardScaler().fit(df)
ndf_mean = df- G.mean_
ndf = ndf_mean/G.var_
return ndf
X = normalize(X)
split = (int(X.shape[0]*(2/3)))
train_set_x = X[:split]
train_set_x = train_set_x.transpose()
train_set_x = train_set_x.values
test_set_x = X[split:]
test_set_x = test_set_x.transpose()
test_set_x = test_set_x.values
# similarly making y matrix with (1, m) size
Y = dataset[["p1"]]
train_set_y = Y[:split]
train_set_y = train_set_y.transpose()
train_set_y = train_set_y.values
test_set_y = Y[split:]
test_set_y = test_set_y.transpose()
test_set_y = test_set_y.values
np.shape(test_set_x)
X = train_set_x
Y = train_set_y
m = Y.shape[1]
def sigmoid(z):
s = 1 / (1 + np.exp(-z))
return s
n_x = X.shape[0]
#siz of output layer
n_y = Y.shape[0]
#size of hidden layer
n_h1= 4
n_h2 = 3
learning_rate = 0.1
W1 = np.random.randn(n_h1, n_x) * 0.01
b1 = np.zeros((n_h1,1))
W2 = np.random.randn(n_h2,n_h1) * 0.01
b2 = np.zeros((n_h2,1))
W3 = np.random.randn(n_y,n_h2) * 0.01
b3 = np.zeros((n_y,1))
for i in range(0, 10000):
Z1 = np.dot(W1,X) + b1
A1 = np.tanh(Z1)
Z2 = np.dot(W2,A1) + b2
A2 = np.tanh(Z2)
Z3 = np.dot(W3,A2) + b3
A3 = sigmoid(Z3)
logprobs = np.multiply(np.log(A3), Y) + np.multiply((1 - Y), np.log(1 - A3))
cost = - np.sum(logprobs) / m
dZ3 = A3 - Y
dW3 = (1 / m) * np.dot(dZ3, A2.T)
db3 = (1 / m) * np.sum(dZ3, axis=1, keepdims=True)
dZ2 = np.multiply(np.dot(W3.T, dZ3), 1 - np.power(A2, 2))
dW2 = (1 / m) * np.dot(dZ2, A1.T)
db2 = (1 / m) * np.sum(dZ2, axis=1, keepdims=True)
dZ1 = np.multiply(np.dot(W2.T, dZ2), 1 - np.power(A1, 2))
dW1 = (1 / m) * np.dot(dZ1, X.T)
db1 = (1 / m) * np.sum(dZ1, axis=1, keepdims=True)
W1 = W1 - learning_rate * dW1
b1 = b1 - learning_rate * db1
W2 = W2 - learning_rate * dW2
b2 = b2 - learning_rate * db2
print(cost)