-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn.py
executable file
·175 lines (131 loc) · 4.73 KB
/
learn.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
import sys
import os, shutil
import getopt, pylab, mdp
from scipy.io import loadmat
from mdp import numx, utils
from mdp.utils import mult
import numpy as np
import Oger
import pickle
#def guess(input, reservoir, readout, dirname):
def guess(input, reservoir, dirname):
#print input.shape
"""
pylab.plot(input)
pylab.show()
pylab.figure()
"""
try:
beta = np.loadtxt(dirname + os.sep + 'beta.mat')
except:
return 0 #19
x = reservoir.execute(input)
#m = readout._execute(x)
#m = mult(x, readout.beta)
m = mult(x, beta)
# find maximum place of m
mcs = np.zeros(m.shape[1])
for i in range(m.shape[1]):
mc = sum(m[:,i]) / m.shape[1]
mcs[i] = mc
return mcs.argmax()
def main(argv):
inputfile = None
number = None
try:
opts, args = getopt.getopt(argv,"hi:n:")
except getopt.GetoptError:
print 'learn.py -i <inputfile>'
print 'eg. for test the reservoir: python learn.py -i /var/www/isolated/octave/two'
print 'eg. to make the reservoir learn: python learn.py -i /var/www/isolated/octave/two -n 0'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'learn.py -i <inputfile>'
print 'eg. for test the reservoir: python learn.py -i /var/www/isolated/octave/two'
print 'eg. to make the reservoir learn: python learn.py -i /var/www/isolated/octave/two -n 0'
sys.exit()
elif opt in ("-i"):
inputfile = arg
elif opt in ("-n"):
number = arg
#print number
#sys.exit()
#print 'Input file is: ', inputfile
dirname= inputfile.rsplit(os.sep,1)[0]
#print 'dirname is: ', dirname
#sys.exit()
if inputfile is None:
print 'no input file'
sys.exit()
test_sample_file = inputfile
if number is None:
cwd = os.getcwd()
os.chdir('octave')
#cmd = "octave -q wav2mat.m " + test_dir + os.sep + test_sample_file + " 8000 128 > null"
cmd = "octave -q wav2mat.m " + test_sample_file + " 8000 128 > null"
#print 'cmd is: ', cmd
os.system(cmd)
os.chdir(cwd)
content = loadmat(test_sample_file + ".mat")
test_input = content['spec'].T
input_dim = test_input.shape[1]
#readout = Oger.nodes.RidgeRegressionNode(use_pinv=True, input_dim=100)
try:
pinput = open(dirname + os.sep + 'reservoir.pkl', 'rb')
reservoir = pickle.load(pinput)
pinput.close()
except IOError:
reservoir = Oger.nodes.LeakyReservoirNode(input_dim=input_dim, output_dim=100, input_scaling=.1, leak_rate=.3)
poutput = open(dirname + os.sep + 'reservoir.pkl', 'wb')
pickle.dump(reservoir, poutput)
poutput.close()
if number is None:
#gn = guess(test_input, reservoir, readout, dirname);
gn = guess(test_input, reservoir, dirname);
print gn
sys.exit()
#shutil.copy(test_sample_file + ".mat", test_sample_file + "_" + numba + ".mat")
x = reservoir.execute(test_input)
teacher_inputs = [test_input]
teacher_outputs = [-1 * mdp.numx.ones([teacher_inputs[-1].shape[0], 10])]
teacher_outputs[-1][:, number] = 1
"""
readout._check_train_args(x, *teacher_outputs)
readout.train(x, *teacher_outputs)
readout._stop_training()
"""
#print 'dirname: ', dirname + os.sep + 'xtx.mat'
#if readout._xTx is None:
try:
xTx = np.loadtxt(dirname + os.sep + 'xtx.mat')
xTy = np.loadtxt(dirname + os.sep + 'xty.mat')
except IOError:
input_dim = 100
#readout._set_output_dim(10)
output_dim = 10
#readout._dtype
dtype = "float64"
x_size = input_dim
#readout._xTx = numx.zeros((x_size, x_size), dtype)
xTx = numx.zeros((x_size, x_size), dtype)
#readout._xTy = numx.zeros((x_size, output_dim), dtype)
xTy = numx.zeros((x_size, output_dim), dtype)
# update internal variables
#readout._xTx += mult(x.T, x)
xTx += mult(x.T, x)
#readout._xTy += mult(x.T, *teacher_outputs)
xTy += mult(x.T, *teacher_outputs)
# calculate beta
#inv_xTx = utils.inv(readout._xTx)
inv_xTx = utils.inv(xTx)
#readout.beta = mult(inv_xTx, readout._xTy)
beta = mult(inv_xTx, xTy)
# save everything to file
np.savetxt(dirname + os.sep + 'xtx.mat' , xTx)
np.savetxt(dirname + os.sep + 'xty.mat' , xTy)
#np.savetxt(dirname + os.sep + 'invxtx.mat', inv_xTx)
np.savetxt(dirname + os.sep + 'beta.mat' , beta)
print 99
if __name__ == "__main__":
main(sys.argv[1:])