Created
March 6, 2018 21:25
-
-
Save soulslicer/478ba9b0699e0c95c2771a9271c83787 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import sys | |
# sys.path.insert(0, 'python') | |
# import caffe | |
# caffe.set_mode_cpu() | |
# solver = caffe.SGDSolver('solver.prototxt') | |
# solver.net.params['lstm1'][2].data[15:30]=5 | |
# import numpy as np | |
# a = np.arange(0,32,0.01) | |
# d = 0.5*np.sin(2*a) - 0.05 * np.cos( 17*a + 0.8 ) + 0.05 * np.sin( 25 * a + 10 ) - 0.02 * np.cos( 45 * a + 0.3) | |
# d = d / max(np.max(d), -np.min(d)) | |
# d = d - np.mean(d) | |
# niter=5000 | |
# train_loss = np.zeros(niter) | |
# solver.net.params['lstm1'][2].data[15:30]=5 | |
# solver.net.blobs['clip'].data[...] = 1 | |
# for i in range(niter) : | |
# seq_idx = i % (len(d) / 320) | |
# solver.net.blobs['clip'].data[0] = seq_idx > 0 | |
# solver.net.blobs['label'].data[:,0] = d[ seq_idx * 320 : (seq_idx+1) * 320 ] | |
# solver.step(1) | |
# train_loss[i] = solver.net.blobs['loss'].data | |
# print i | |
# import matplotlib.pyplot as plt | |
# #%matplotlib inline | |
# plt.plot(np.arange(niter), train_loss) | |
# plt.show() | |
#!/usr/bin/env python | |
# Based on the "Clockwork RNN" paper at | |
# http://jmlr.org/proceedings/papers/v32/koutnik14.pdf | |
#'For LSTM it was also crucial to initialize the bias of the | |
# forget gates to a high value (5.0 in this case) to encourage | |
# the long-term memory' | |
import numpy as np | |
import sys | |
sys.path.append('../../python') | |
import caffe | |
import matplotlib.pyplot as plt | |
def gen_signal(seq_len): | |
a = np.arange(0, seq_len/10, 0.01) | |
s = 0.5 * np.sin(2*a) | |
#0.05 * np.cos( 17*a + 0.8 ) + \ | |
#0.05 * np.sin( 25 * a + 10 ) - \ | |
#0.02 * np.cos( 45 * a + 0.3) | |
s = s / max(np.max(s), -np.min(s)) | |
s = s - np.mean(s) | |
print s | |
print "length of d {}".format(len(s)) | |
return s | |
caffe.set_mode_gpu() | |
caffe.set_device(0) | |
# set_trace() | |
solver = caffe.SGDSolver('solver.prototxt') | |
print "blob names {}".format(solver.net.blobs.keys()) | |
print "blob shapes {}".format([(k,v.data.shape) for k, v in solver.net.blobs.items()]) | |
print "param names {}".format(solver.net.params.keys()) | |
print "param shape 0 {}".format(solver.net.params['lstm'][0].data.shape) | |
print "param shape 1 {}".format(solver.net.params['lstm'][1].data.shape) | |
print "param shape 2 {}".format(solver.net.params['lstm'][2].data.shape) | |
# set_trace() | |
# Set the bias of the forget gate to 5.0 as explained in the clockwork RNN paper | |
#solver.net.params['lstm'][1].data[15:30] = 5.0 | |
solver.net.params['lstm'][1].data[15:30] = 5.0 | |
seq_len = np.shape(solver.net.blobs['data'].data)[0] | |
streams_len = np.shape(solver.net.blobs['data'].data)[1] | |
print "sequence length: {}".format (seq_len) | |
print "streams length: {}".format (streams_len) | |
solver.net.blobs['clip'].data.reshape(seq_len, 1) | |
solver.net.blobs['label'].data.reshape(seq_len, 1) | |
solver.net.blobs['clip'].data[...] = 1 | |
sig = gen_signal(seq_len) | |
print "signal length: {}".format (len(sig)) | |
# # print sig | |
plt.plot(sig) | |
plt.show() | |
#plt.ion() | |
# Train | |
niter = 5000 | |
iter = 0 | |
train_loss = np.zeros(niter) | |
while iter < niter : | |
seq_idx = iter % (len(sig) / seq_len) # Goes from 0 to 9, Signal resets | |
# print "seq_idx val: {} clip value: {}".format(seq_idx, (seq_idx > 0)) | |
solver.net.blobs['clip'].data[0] = seq_idx > 0 | |
# solver.net.blobs['label'].data[:,0] = d[ seq_idx * seq_len : (seq_idx+1) * seq_len ] | |
solver.net.blobs['label'].data[:,0] = sig[seq_idx * seq_len : (seq_idx + 1) * seq_len] | |
solver.net.blobs['data'].data[:,0,0] = sig[seq_idx * seq_len : (seq_idx + 1) * seq_len] | |
#print sig[seq_idx * seq_len : (seq_idx + 1) * seq_len].shape | |
# print solver.net.blobs['label'].data | |
solver.step(1) | |
train_loss[iter] = solver.net.blobs['loss'].data | |
iter += 1 | |
# plt.gcf().clear() | |
# plt.plot(sig,'r') | |
# plt.plot(sig[seq_idx * seq_len : (seq_idx + 1) * seq_len], 'g') | |
# plt.pause(0.05) | |
print train_loss[iter-1] | |
plt.plot(np.arange(niter), train_loss) | |
plt.show() | |
solver.net.save("lstm.caffemodel") | |
# # test | |
# # set_trace() | |
test_net = caffe.Net('lstm_deploy.prototxt', 'lstm.caffemodel', caffe.TEST) | |
test_net.blobs['data'].data.reshape(2, 1, 1) | |
test_net.blobs['clip'].data.reshape(2, 1) | |
#test_net.blobs['label'].data.reshape(2, 1) | |
test_net.blobs['clip'].data[...] = 1 | |
test_net.reshape() | |
preds = np.zeros(len(sig)) | |
# print test_net.blobs['data'].data | |
# stop | |
for i in range(len(sig)): | |
test_net.blobs['clip'].data[0] = i > 0 | |
preds[i] = test_net.forward()['ip'][0][0] | |
#test_net.blobs['data'].data[0] = preds[i] | |
print test_net.forward()['ip'] | |
print "{} {}".format(sig[i], preds[i]) | |
plt.plot(sig, 'r') | |
plt.plot(preds, 'g') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment