-
Notifications
You must be signed in to change notification settings - Fork 0
/
poker_hands.py
185 lines (136 loc) · 4.15 KB
/
poker_hands.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import math
import pickle
import numpy as np
def getFeatureMatrix(file_path):
data = np.genfromtxt(file_path, delimiter=',', skip_header=1,usecols=range(0,10), dtype='int', max_rows=100000)
data= data
np.c_[data, np.ones(data.shape[0])]
return data
def getLabels(file_path):
data = np.genfromtxt(file_path, delimiter=',', skip_header=1,usecols=range(10,11), dtype='int', max_rows=100000)
return data
def sigmoid_(x):
return 1/(1+np.exp(-x))
def d_sigmoid_(x):
return sigmoid(x)*(1-sigmoid(x))
def softmax(x):
sum = 0
for i in x:
sum += math.exp(-i)
return np.exp(-x)/sum
sigmoid = np.vectorize(sigmoid_)
d_sigmoid = np.vectorize(d_sigmoid_)
def activation(input_values, input_weights):
sum1 = np.transpose(input_weights).dot(input_values)
# #print("sum1", sum1, "\n")
t = (max(sum1)-min(sum1))
if(t != 0):
sum1 = (sum1-min(sum1))/t
# #print("sum1 ",sum1.shape) #20*1
return sigmoid(sum1), sum1
def train(featureMatrix, labels, learning_rate):
# initialize w
input_layer, hidden_layer1, hidden_layer2, output_layer = 10,20,20,10
w1 = np.random.uniform(size=input_layer*hidden_layer1)
w1 = np.split(w1,input_layer)
w1 = np.array(w1)
w2 = np.random.uniform(size=hidden_layer1*hidden_layer2)
w2 = np.split(w2,hidden_layer1)
w2 = np.array(w2)
w3 = np.random.uniform(size=hidden_layer2*output_layer)
# w2 = np.ones(hidden_layer*output_layer)
w3 = np.split(w3,hidden_layer2)
w3 = np.array(w3)
#print("w1", w1, "\n")
#print("w2", w2, "\n")
for p in range(0, 10):
for idx, x1 in enumerate(featureMatrix):
print(idx)
x2, sum1 = activation(x1, w1)
x3, sum2 = activation(x2, w2)
x4, sum3 = activation(x3, w3)
x4 = softmax(sum3)
#print("softmax", x3, "\n")
y = np.argmax(x4)
sig4 = sigmoid(sum3)
#print("sigmoid(sum2)",sig2)
if(y != labels[idx]):
y_ = np.zeros(output_layer)
y_[labels[idx]] = 1
d_E3 = -np.divide(y_, sig4) + np.divide((1-y_),(1-sig4))
#print("d_E2", d_E2, "\n")
d_sig3 = d_sigmoid(sum3)
#print("d_sig2", d_sig2, "\n")
d_sum3 = x3
t = np.multiply(d_E3, d_sig3)
del_E3=[]
for i in (d_sum3):
del_E3.append(i*t)
del_E3 = np.array(del_E3)
#print("del_E2 ",del_E2, "\n")
# #print("w2 ",w2.shape)
w3 = w3 - learning_rate*del_E3
# print("w2 ",w2)
d_E2 = w3.dot(np.multiply(d_E3, d_sig3))
#print("d_E1 ",d_E1, "\n")
d_sig2 = d_sigmoid(sum2)
d_sum2 = x2
#print("d_sum1 ",d_sum1, "\n")
t=np.multiply(d_E2, d_sig2)
del_E2=[]
for i in (d_sum2):
del_E2.append(i*t)
del_E2 = np.array(del_E2)
#print("del_E1", del_E1, "\n")
# #print("w1 ",w1.shape)
w2 = w2 - learning_rate*del_E2
# print("w1 ",w1)
d_E1 = w2.dot(np.multiply(d_E2, d_sig2))
#print("d_E1 ",d_E1, "\n")
d_sig1 = d_sigmoid(sum1)
d_sum1 = x1
#print("d_sum1 ",d_sum1, "\n")
t=np.multiply(d_E1, d_sig1)
del_E1=[]
for i in (d_sum1):
del_E1.append(i*t)
del_E1 = np.array(del_E1)
#print("del_E1", del_E1, "\n")
# #print("w1 ",w1.shape)
w1 = w1 - learning_rate*del_E1
return w1, w2, w3
def test(featureMatrix, labels, w1, w2, w3):
count = 0
f = open('output.csv', 'w')
print(w1,"\n", w2,"\n", w3)
print("\n\n\n\n===========Testing===============\n\n\n\n")
for idx, x1 in enumerate(featureMatrix):
x2, sum1 = activation(x1, w1)
x3, sum2 = activation(x2, w2)
x4, sum3 = activation(x3, w3)
x4 = softmax(sum3)
# print("softmax ", x3, "\n")
y = np.argmax(x4)
# if(y == labels[idx]):
# count = count + 1
f.write(str(idx))
f.write(",")
f.write(np.array2string(y))
f.write("\n")
# print(count/featureMatrix.shape(0)*100)
def main():
learning_rate = 0.01
file_name = 'train.csv'
featureMatrix = getFeatureMatrix(file_name)
labels = getLabels(file_name)
w1,w2,w3 = train(featureMatrix, labels, learning_rate)
# weights1 = open('w1', 'w')
# pickle.dump(",".join(str(i) for i in j for j in w1),weights1)
# weights2 = open('w2','w')
# pickle.dump(",".join(str(i) for i in j for j in w2),weights2)
file_name = 'train.csv'
featureMatrix = getFeatureMatrix(file_name)
accuracy = test(featureMatrix, labels, w1,w2,w3)
#print(accuracy)
if __name__ == "__main__":
main()