-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoiReg.py
169 lines (150 loc) · 5.35 KB
/
PoiReg.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
import numpy as np
from MyKeras import GenTrainData
import sys
#import statsmodels.discrete.discrete_model as poi
#import statsmodels.tools.tools as st_tool
import sklearn.linear_model as lr
from sklearn.preprocessing import Binarizer as bn
from keras.models import Sequential
from keras.layers.core import Activation, Dense
from keras.layers.recurrent import LSTM
from keras.optimizers import SGD
from keras.optimizers import RMSprop
Seq_Size = 10
def Train(X,Y):
#nodelist = np.genfromtxt("int.txt")
#T = int(sys.argv[1])
#for i in nodelist:
#reg = np.genfromtxt('reg_'+str(int(i))+'.txt')
poi_model = lr.LinearRegression(fit_intercept=True)
poi_model.fit(X,Y)
print 'params ',poi_model.get_params()
print 'coeff ',poi_model.coef_
print 'intercept ',poi_model.intercept_
reg = np.c_[X, np.ones(X.shape[0])]
#meas = np.genfromtxt('meas_15_'+str(int(i))+'.txt')
#print 'lable ',i
#print 'before reg',reg
#reg = st_tool.add_constant(reg,prepend=False)
#print 'after reg',reg
#raw_input()
#P1 = poi.Poisson(Y[1:100],reg[1:100,:])
#lim = 1
#bd = ((-lim,lim),(-lim,lim),(-lim,lim),(-lim,lim))
#F = P1.fit(method='lbfgs',bounds=bd)
#F = P1.fit(method='bfgs',disp=True)
#print F.summary()
#print F.params
#print F.mle_retvals
#np.savetxt('regx1_'+str(int(i))+'.txt',F.params)
#P2 = poi.Poisson(meas[1:T,1],reg)
#F = P2.fit(method='lbfgs',bounds=bd)
#np.savetxt('regx2_'+str(int(i))+'.txt',F.params)
#P3 = poi.Poisson(meas[1:T,2],reg)
#F = P3.fit(method='lbfgs',bounds=bd)
#np.savetxt('regx3_'+str(int(i))+'.txt',F.params)
return poi_model
def DLDense(X,Y,Xtest,Ytest):
model = Sequential()
#model.add(Dense(Seq_Size,input_shape=(Seq_Size,), activation='linear'))
model.add(Dense(Seq_Size,input_shape=(Seq_Size,),activation='linear',use_bias=True,
bias_initializer='zeros',kernel_initializer='zeros'))
#model.add(Dense(Seq_Size, activation='linear'))
model.add(Dense(1, activation='sigmoid',use_bias=True))
rms = RMSprop(lr=0.01,decay=1e-5)
sgd = SGD(lr=1e-3, decay=1e-8, momentum=0,nesterov=False)
Xperm = np.random.permutation(X.shape[0])
X = X[Xperm,:]
Y = Y[Xperm]
model.compile(loss='binary_crossentropy', optimizer=rms)
history = model.fit(X, Y, epochs=500, batch_size=3000, verbose=1)
np.savetxt('loss.txt',history.history['loss'])
print 'all weights ',model.get_weights()
#print 'weights ',model.get_weights()[0]
yhat = model.predict(Xtest)
ybinary = np.zeros(yhat.shape,dtype=int)
for k in range(0,yhat.shape[0]):
if yhat[k,0]>=.5:
ybinary[k,0] =1
else :
ybinary[k,0] = 0
np.savetxt('yhat.txt',yhat)
np.savetxt('ybinary.txt',ybinary,fmt='%d')
print 'er ',np.sum(np.abs(ybinary[:,0]-Ytest))
def DLRNN(X,Y,Xtest,Ytest):
model = Sequential()
model.add(LSTM(1,input_shape=(Seq_Size,1),activation='linear',
return_sequences=False,use_bias=True
,bias_initializer='random_normal'
,kernel_initializer='random_normal'
))
rms = RMSprop(lr=0.01,decay=1e-5)
sgd = SGD(lr=1e-3, decay=1e-8, momentum=0,nesterov=False)
Xperm = np.random.permutation(X.shape[0])
X = X[Xperm,:]
Y = Y[Xperm]
model.compile(loss='binary_crossentropy', optimizer=rms)
history = model.fit(X, Y, epochs=300, batch_size=3000, verbose=1)
np.savetxt('loss.txt',history.history['loss'])
print 'all weights ',model.get_weights()
#print 'weights ',model.get_weights()[0]
yhat = model.predict(Xtest)
ybinary = np.zeros(yhat.shape,dtype=int)
for k in range(0,yhat.shape[0]):
if yhat[k,0]>=.5:
ybinary[k,0] =1
else :
ybinary[k,0] = 0
np.savetxt('yhat.txt',yhat)
np.savetxt('ybinary.txt',ybinary,fmt='%d')
print 'er ',np.sum(np.abs(ybinary[:,0]-Ytest))
def GetCountsForRNN(X_temp,Xtest_temp):
X = np.zeros((X_temp.shape[0],Seq_Size,1))
for i in range(0,X_temp.shape[0]):
X[i,0:Seq_Size,0] = X_temp[i,:,0]
#X[i,Seq_Size:2*Seq_Size] = X_temp[i,:,1]
#print X[i,:]
X_test = np.zeros((Xtest_temp.shape[0],Seq_Size,1))
for i in range(0,Xtest_temp.shape[0]):
X_test[i,0:Seq_Size,0] = Xtest_temp[i,:,0]
#X_test[i,Seq_Size:2*Seq_Size] = X_temp[i,:,1]
return X,X_test
def GetCountsForDense(X_temp,Xtest_temp):
X = np.zeros((X_temp.shape[0],Seq_Size))
for i in range(0,X_temp.shape[0]):
X[i,0:Seq_Size] = X_temp[i,:,0]
#X[i,Seq_Size:2*Seq_Size] = X_temp[i,:,1]
#print X[i,:]
X_test = np.zeros((Xtest_temp.shape[0],Seq_Size))
for i in range(0,Xtest_temp.shape[0]):
X_test[i,0:Seq_Size] = Xtest_temp[i,:,0]
#X_test[i,Seq_Size:2*Seq_Size] = X_temp[i,:,1]
return X,X_test
def main():
X_temp,Y,Xtest_temp,Ytest,ymean = GenTrainData()
X = np.zeros((X_temp.shape[0],Seq_Size*2))
for i in range(0,X_temp.shape[0]):
X[i,0:Seq_Size] = X_temp[i,:,0]
X[i,Seq_Size:2*Seq_Size] = X_temp[i,:,1]
#print X[i,:]
X_test = np.zeros((Xtest_temp.shape[0],Seq_Size*2))
for i in range(0,Xtest_temp.shape[0]):
X_test[i,0:Seq_Size] = Xtest_temp[i,:,0]
X_test[i,Seq_Size:2*Seq_Size] = Xtest_temp[i,:,1]
##print Y+ymean
X,X_test = GetCountsForRNN(X_temp,Xtest_temp)
#X,X_test = GetCountsForDense(X_temp,Xtest_temp)
#poi_model = Train(X,Y)
#yhat = poi_model.predict(X_test)
#print poi_model.coef_
#print 'Regression yhat shape ',yhat.shape
#for i in range(0,yhat.shape[0]):
# print np.dot(poi_model.coef_,X_test[i,:]),Ytest[i]
# print np.multiply(poi_model.coef_,X_test[i,:])
# print poi_model.coef_,X_test[i,:]
#np.savetxt('yhat.txt',yhat)
#print np.sum(np.abs(yhat-Ytest))
#DLDense(X,Y,X_test,Ytest)
DLRNN(X,Y,X_test,Ytest)
if __name__== "__main__":
main()