-
Notifications
You must be signed in to change notification settings - Fork 4
/
rgp_on_sines.py
249 lines (213 loc) · 13 KB
/
rgp_on_sines.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import pylab
import numpy
import numpy.random
import gatedAutoencoder
import rgp3layer
import theano
import theano.tensor as T
from theano.tensor.shared_randomstreams import RandomStreams
from scipy.io import wavfile
numpy_rng = numpy.random.RandomState(1)
theano_rng = RandomStreams(1)
def dispims(M, height, width, border=0, bordercolor=0.0, layout=None, **kwargs):
from pylab import cm, ceil
numimages = M.shape[1]
if layout is None:
n0 = int(numpy.ceil(numpy.sqrt(numimages)))
n1 = int(numpy.ceil(numpy.sqrt(numimages)))
else:
n0, n1 = layout
im = bordercolor * numpy.ones(((height+border)*n0+border,(width+border)*n1+border),dtype='<f8')
for i in range(n0):
for j in range(n1):
if i*n1+j < M.shape[1]:
im[i*(height+border)+border:(i+1)*(height+border)+border,
j*(width+border)+border :(j+1)*(width+border)+border] = numpy.vstack((
numpy.hstack((numpy.reshape(M[:,i*n1+j],(height, width)),
bordercolor*numpy.ones((height,border),dtype=float))),
bordercolor*numpy.ones((border,width+border),dtype=float)
))
pylab.imshow(im, cmap=cm.gray, interpolation='nearest', **kwargs)
pylab.draw(); pylab.show()
class GraddescentMinibatch(object):
""" Gradient descent trainer class. """
def __init__(self, model, data, batchsize, learningrate, momentum=0.9, normalizefilters=False, rng=None, verbose=True):
self.model = model
self.data = data
self.learningrate = theano.shared(numpy.array(learningrate).astype("float32"))
self.verbose = verbose
self.batchsize = batchsize
self.numbatches = self.data.get_value().shape[0] / batchsize
self.momentum = momentum
self.normalizefilters = normalizefilters
if rng is None:
self.rng = numpy.random.RandomState(1)
else:
self.rng = rng
self.epochcount = 0
self.index = T.lscalar()
self.incs = dict([(p, theano.shared(value=numpy.zeros(p.get_value().shape,
dtype=theano.config.floatX), name='inc_'+p.name)) for p in self.model.params])
self.inc_updates = {}
self.updates = {}
self.n = T.scalar('n')
self.noop = 0.0 * self.n
for _param, _grad in zip(self.model.params, self.model._grads):
self.inc_updates[self.incs[_param]] = self.momentum * self.incs[_param] - self.learningrate * _grad
self.updates[_param] = _param + self.incs[_param]
self._updateincs = theano.function([self.index], self.model._cost,
updates = self.inc_updates,
givens = {self.model.inputs:self.data[self.index*self.batchsize:(self.index+1)*self.batchsize]})
self._trainmodel = theano.function([self.n], self.noop, updates = self.updates)
def step(self):
cost = 0.0
stepcount = 0.0
for batch_index in self.rng.permutation(self.numbatches-1):
stepcount += 1.0
cost = (1.0-1.0/stepcount)*cost + (1.0/stepcount)*self._updateincs(batch_index)
self._trainmodel(0)
if self.normalizefilters:
self.model.normalizefilters()
self.epochcount += 1
if self.verbose:
print 'epoch: %d, cost: %f' % (self.epochcount, cost)
def reset_incs(self):
for p in self.model.params:
self.incs[p].set_value(numpy.zeros(p.get_value().shape, dtype=theano.config.floatX))
print 'making data...'
seq_len = 160
frame_len = 10
numframes = seq_len / frame_len
numtrain = 50000
numtest = 10000
#all_features_numpy = 1000*numpy.array([numpy.sin(numpy.linspace(o, o+2*numpy.pi, seq_len)*m) for m, o in zip(numpy_rng.rand(numtrain+numtest)*580+220, numpy_rng.rand(numtrain+numtest)*2*numpy.pi)]).astype("float32")
all_features_numpy = 1000*numpy.array([numpy.sin(numpy.linspace(o, o+2*numpy.pi, seq_len)*m) for m, o in zip(numpy_rng.rand(numtrain+numtest)*29+1, numpy_rng.rand(numtrain+numtest)*2*numpy.pi)]).astype("float32")
train_features_numpy = all_features_numpy[:numtrain]
test_features = all_features_numpy[numtrain:]
del all_features_numpy
data_mean = train_features_numpy.mean()
train_features_numpy -= data_mean
data_std = train_features_numpy.std()
train_features_numpy /= data_std
train_features_numpy = train_features_numpy[numpy.random.permutation(numtrain)]
train_features_theano = theano.shared(train_features_numpy)
test_features -= data_mean
test_features /= data_std
test_feature_beginnings = test_features[:,:frame_len*3]
print '... done'
print 'pretraining velocity model ...'
pretrainmodel_velocity = gatedAutoencoder.FactoredGatedAutoencoder(
numvisX=frame_len,
numvisY=frame_len,
numfac=200,
nummap=100,
output_type='real',
corruption_type='zeromask',
corruption_level=0.3,
numpy_rng=numpy_rng,
theano_rng=theano_rng)
pretrain_features_velocity_numpy = numpy.concatenate([train_features_numpy[i, 2*j*frame_len:2*(j+1)*frame_len][None,:] for j in range(seq_len/(frame_len*2)) for i in range(numtrain)],0)
pretrain_features_velocity_numpy = pretrain_features_velocity_numpy[numpy.random.permutation(pretrain_features_velocity_numpy.shape[0])]
pretrain_features_velocity_theano = theano.shared(pretrain_features_velocity_numpy)
pretrainer_velocity = GraddescentMinibatch(pretrainmodel_velocity, pretrain_features_velocity_theano, batchsize=100, learningrate=0.01)
for epoch in xrange(10):
pretrainer_velocity.step()
print '... done'
print 'pretraining acceleration model ...'
pretrainmodel_acceleration = gatedAutoencoder.FactoredGatedAutoencoder(
numvisX=pretrainmodel_velocity.nummap,
numvisY=pretrainmodel_velocity.nummap,
numfac=100,
nummap=50,
output_type='real',
corruption_type='zeromask',
corruption_level=0.3,
numpy_rng=numpy_rng,
theano_rng=theano_rng)
pretrain_features_acceleration_numpy = numpy.concatenate((pretrainmodel_velocity.mappings(train_features_numpy[:, :2*frame_len]),
pretrainmodel_velocity.mappings(train_features_numpy[:, 1*frame_len:3*frame_len])),1)
pretrain_features_acceleration_theano = theano.shared(pretrain_features_acceleration_numpy)
pretrainer_acceleration = GraddescentMinibatch(pretrainmodel_acceleration, pretrain_features_acceleration_theano, batchsize=100, learningrate=0.01)
for epoch in xrange(10):
pretrainer_acceleration.step()
#pylab.imshow(pretrainmodel_acceleration.mappings(pretrain_features_acceleration_numpy[:200]))
#pylab.show(); pylab.draw()
print '... done'
print 'training sequence model ...'
#model = gatedAutoencoder_multistep.FactoredGatedAutoencoder(numvis=frame_len,
# numfac=pretrainmodel_velocity.numfac,
# numvel=pretrainmodel_velocity.nummap,
# numvelfac=pretrainmodel_acceleration.numfac,
# numacc=pretrainmodel_acceleration.nummap,
# numframes=numframes,
# vis_corruption_type="gaussian", vis_corruption_level=0.0,
# acc_corruption_level=0.2,
# numpy_rng=numpy_rng,
# theano_rng=theano_rng)
model = gatedAutoencoder3layer_notebookunits_bottomupregularization.FactoredGatedAutoencoder(
numvis=frame_len,
numnote=100,
numfac=pretrainmodel_velocity.numfac,
numvel=pretrainmodel_velocity.nummap,
numvelfac=pretrainmodel_acceleration.numfac,
numacc=pretrainmodel_acceleration.nummap,
numaccfac=10,
numjolt=10,
vis_corruption_level=0.2,
vel_corruption_level=0.0,
acc_corruption_level=0.0,
jolt_corruption_level=0.0,
numframes_to_train=8,
numframes_to_predict=15,
gradblocks=[],
coststart=4,
numpy_rng=numpy_rng,
theano_rng=theano_rng)
model.wx_left.set_value(numpy.concatenate((pretrainmodel_velocity.wxf.get_value()*0.5, numpy_rng.randn(model.numnote,model.numfac).astype("float32")*0.01),0))
model.wx_right.set_value(numpy.concatenate((pretrainmodel_velocity.wyf.get_value()*0.5, numpy_rng.randn(model.numnote,model.numfac).astype("float32")*0.01),0))
model.wv.set_value(pretrainmodel_velocity.whf.get_value().T)
model.wvf_left.set_value(pretrainmodel_acceleration.wxf.get_value())
model.wvf_right.set_value(pretrainmodel_acceleration.wyf.get_value())
model.wa.set_value(pretrainmodel_acceleration.whf.get_value().T)
model.ba.set_value(pretrainmodel_acceleration.bmap.get_value())
model.bv.set_value(pretrainmodel_velocity.bmap.get_value())
model.bx.set_value(numpy.concatenate((pretrainmodel_velocity.bvisX.get_value(),numpy.zeros((model.numnote),dtype="float32"))))
# TRAIN MODEL
trainer = GraddescentMinibatch(model, train_features_theano, batchsize=400, learningrate=0.01)
for epoch in xrange(1000):
trainer.step()
#if epoch % 50 == 0:
# trainer.set_learningrate(trainer.learningrate*0.95)
# pylab.clf()
# pylab.plot(test_features[0])
# pylab.plot(model.predict(test_feature_beginnings[[0]], numframes, False).flatten())
# pylab.plot(test_feature_beginnings[0])
# pylab.show(); pylab.draw()
print '... done'
##generate some training predictions
#for trainingcase in range(20):
# generated = model.predict(train_features_numpy[[trainingcase], :seq_len*3], 10, True).flatten()
# pylab.subplot(10, 2, trainingcase+1)
# pylab.plot(train_features_numpy[trainingcase])
# pylab.plot(generated)
# if trainingcase==0:
# legend(["training example", "prediction"])
# wavfile.write("prediction"+str(trainingcase)+"trainedwithgaussiancorruption.wav", 16000, numpy.int16(generated * data_std + data_mean))
# wavfile.write("original"+str(trainingcase)+"trainedwithgaussiancorruption.wav", 16000, numpy.int16(train_features_numpy[trainingcase]*data_std+data_mean))
#
#pylab.savefig("train_predictions_trainedwithgaussiancorruption.png")
#pylab.clf()
#
##generate some test predictions
#test_predictions = model.predict(test_feature_beginnings, 10, True)
#for testcase in range(20):
# pylab.subplot(10, 2, testcase+1)
# pylab.plot(test_features[testcase])
# #pylab.plot(test_predictions[testcase][:len(test_features[testcase])])
# pylab.plot(test_predictions[testcase])
# if testcase==0:
# legend(["test example", "prediction"])
# wavfile.write("prediction_test"+str(testcase)+"trainedwithgaussiancorruption.wav", 16000, numpy.int16(test_predictions[testcase]*data_std+data_mean))
# wavfile.write("original_test"+str(testcase)+"trainedwithgaussiancorruption.wav", 16000, numpy.int16(test_features[testcase]*data_std+data_mean))
#
#pylab.savefig("test_predictions_trainedwithgaussiancorruption.png")