-
Notifications
You must be signed in to change notification settings - Fork 2
/
ngc.py
79 lines (69 loc) · 1.47 KB
/
ngc.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
import numpy as np
import pandas as pd
K = 10 # internal layer
epsilon = 0.01
MaxItr = 10
path = 'Train_data_cleaned-2.0.1.csv'
df = pd.get_dummies(pd.read_csv(path)).values
N, D = df.shape
D -= 2
X = df[:, 0:D]
Y = df[:, D:]
print(X.shape)
print(X[:5, :])
print(Y.shape)
print(Y[:5, :])
'''
path = 'Train_data_cleaned-2.0.2.csv'
df = pd.read_csv(path)
Y = df.values[:, -1]
df = df.drop(columns=['class'])
data = pd.get_dummies(df)
X = data.values
N, D = X.shape
num = int(N * 0.8)
X_train = X[0:num, :]
Y_train = Y[0:num]
X_val = X[num:, :]
Y_val = Y[num:]
print(X_train.shape)
print(X_train[:5, :])
print(Y_train.shape)
print(Y_train[:5])
'''
def train(X, Y, MaxItr):
N, D = X.shape
W = np.random.random_sample((D, K)) * 2 - 1
v = np.random.random_sample(K) * 2 - 1
for i in range(MaxItr):
G = np.zeros((D, K))
g = np.zeros(K)
a = np.zeros(K)
h = np.zeros(K)
for j in range(N):
for k in range(K):
a[k] = np.dot(W[:,k], X[j])
h[k] = np.tanh(a[k])
y = np.dot(v, h)
e = y - Y[j]
g = g - e * h
for k in range(K):
G[:,k] = G[:,k] - e * v[k] * (1 - np.tanh(a[k]) ** 2) * X[j]
W = W - epsilon * G
v = v - epsilon * g
return W, v
def test(X, Y, W, v):
res = np.matmul(np.tanh(np.matmul(X, W)), v)
print(res.shape)
print(res)
print('--------')
print(np.amax(res))
print(np.amin(res))
print(Y.shape)
return
#W, v = train(X_train, Y_train, MaxItr)
#print(W.shape)
#print(v.shape)
#print(X_val.shape)
#print("---------")
#test(X_val, Y_val, W, v)