-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedforward.py
206 lines (174 loc) · 5.55 KB
/
feedforward.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import tensorflow as tf
import sklearn
from sklearn.datasets import load_boston
from sklearn.cross_validation import train_test_split
from sklearn.utils import shuffle
from sklearn import preprocessing
import numpy
import random
import pandas as pd
import math
d = pd.read_csv('santander/train.csv')
X_train_list=[]
Y_train_list=[]
X_validate_list=[]
Y_validate_list=[]
X_train_0_list=[]
X_train_1_list=[]
X_validate_0_list=[]
X_validate_1_list=[]
Y_train_0_list=[]
Y_train_1_list=[]
Y_validate_0_list=[]
Y_validate_1_list=[]
Ws=[]
Bs=[]
data_list=[]
for row in d.itertuples():
#X_train_list.append(list(row[1:371]))
#Y_train_list.append(list(row[372]))
data_list.append(list(row[1:]))
random.shuffle(data_list)
datasize=len(data_list)
training_data = data_list[:datasize*60/100]
validate_data = data_list[datasize*60/100:]
for innerlist in training_data:
#print len(innerlist)
#X_train_list.append(innerlist[:369])
if innerlist[370]==0:
X_train_0_list.append(innerlist[:369])
#Y_train_list.append([1,0])
Y_train_0_list.append([1,0])
else:
X_train_1_list.append(innerlist[:369])
#Y_train_list.append([0,1])
Y_train_1_list.append([0,1])
for innerlist in validate_data:
X_validate_list.append(innerlist[:369])
if innerlist[370]==0:
#X_train_0_list.append(innerlist[:369])
Y_validate_list.append([1,0])
#Y_validate_0_list.append([1,0])
else:
#X_train_1_list.append(innerlist[:369])
Y_validate_list.append([0,1])
#Y_validate_1_list.append([0,1])
#scaler = preprocessing.StandardScaler()
#X_train = scaler.fit_transform(X_train_list)
#X_validate = scaler.fit_transform(X_validate_list)
#print ("Scaled")
#X_train_list_2=X_train_list
#Y_train_list_2=Y_train_list
#X_train_list=[]
#X_validate_list=[]
#X_train_list_2 = X_train.tolist()
#X_validate_list_2 = X_validate.tolist()
x=tf.placeholder(tf.float32, [None, 369])
X_train=[]
X_validate=[]
data_list=[]
d=[]
training_data=[]
validate_data=[]
layer_sizes=[369,2]
next_layer_input=x
for dim in layer_sizes:
input_dim = int(next_layer_input.get_shape()[1])
# Initialize W using random values in interval [-1/sqrt(n) , 1/sqrt(n)]
#W = tf.Variable(tf.random_uniform([input_dim, dim],-1.0 / math.sqrt(input_dim), 1.0 / math.sqrt(input_dim)))
W = tf.Variable(tf.random_normal([input_dim, dim],mean=0.0, stddev=1.0, dtype=tf.float32))
#W = tf.Variable(tf.zeros([input_dim,dim]))
# Initialize b to zero
b = tf.Variable(tf.zeros([dim]))
Ws.append(W)
Bs.append(b)
#output = tf.nn.tanh(tf.matmul(next_layer_input,W) + b)
if (dim==2):
output = tf.nn.softmax(tf.matmul(next_layer_input,W) + b)
#tf.nn.softmax_cross_entropy_with_logits(tf.matmul(next_layer_input,W) + b)
else:
#output = tf.nn.relu(tf.matmul(next_layer_input,W) + b)
output = tf.matmul(next_layer_input,W) + b
# the input into the next layer is the output of this layer
next_layer_input = output
#W = tf.Variable(tf.zeros([369,2]))
#b = tf.Variable(tf.zeros([2]))
#y = tf.nn.softmax(tf.matmul(x,W) + b)
y=next_layer_input
y_ = tf.placeholder(tf.float32, [None, 2])
#cross_entropy = -tf.reduce_sum(y_ * tf.log(y+1e-9))
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
#sse = tf.reduce_sum( tf.pow((y_ - y),2))
sse = tf.reduce_sum( (y_ - y)*(y_ - y))
mse = tf.reduce_mean( tf.pow((y_ - y),2))
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
correct_predition = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_predition, "float"))
index_0_list = range(len(X_train_0_list))
index_1_list = range(len(X_train_1_list))
for i in range(500):
#1000000 3489.25
#batch_xs, batch_ys = shuffle(X_train_list, Y_train_list, random_state=i)
#for (x,y) in zip(batch_xs, batch_ys):
#sess.run(train_step, feed_dict={x: x, y_: y})
random.shuffle(index_0_list)
random.shuffle(index_1_list)
tempx=[]
tempy=[]
##print index_list
count=0
for j in index_0_list:
if count > 1000:
break
tempx.append(X_train_0_list[j])
tempy.append(Y_train_0_list[j])
count+=1
count=0
for j in index_1_list:
if count > 1000:
break
tempx.append(X_train_1_list[j])
tempy.append(Y_train_1_list[j])
count+=1
sess.run(train_step, feed_dict={x: tempx, y_: tempy})
if i%10 == 0:
print sess.run(accuracy, feed_dict={x: tempx, y_: tempy})
#print sess.run(sse, feed_dict={x: tempx, y_: tempy})
#sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
#sess.run(train_step, feed_dict={x: X_train[i], y_: Y_train2[i]})
print "Done training"
#print sess.run(sse, feed_dict={x: X_test, y_: Y_test2})
#print "W:"
#print sess.run(Ws)
#print "b:"
#print sess.run(Bs)
#print sess.run(accuracy, feed_dict={x: X_validate_list, y_: Y_validate_list})
#print "y:"
#print sess.run(y, feed_dict={x: X_validate_list})
#print "y_:"
#print sess.run(y_, feed_dict={y_: Y_validate_list})
#print sess.run(y, feed_dict={x: X_validate_list})
#print Y_validate_list
test= pd.read_csv('santander/test.csv')
data_list=[]
for row in test.itertuples():
#X_train_list.append(list(row[1:371]))
#Y_train_list.append(list(row[372]))
data_list.append(list(row[1:]))
prediction = tf.argmax(y,1)
X_test_list=[]
for innerlist in data_list:
X_test_list.append(innerlist[:369])
print "y"
result = pd.DataFrame()
result['ID']=test['ID']
#test_kaggle = test[features]
#test_kaggle_r = [sess.run(prediction, feed_dict={x: [X_test_list[i]]}) for i in range(len(X_test_list))]
test_kaggle_r = [sess.run(prediction, feed_dict={x: X_test_list})]
#test_labels = [1 if x>29 else 0 for x in test_kaggle_r]
#print 'sum of test labels',sum(test_labels)
result['TARGET']=test_kaggle_r
result.to_csv('submission.csv')