Skip to content

Instantly share code, notes, and snippets.

@romeokienzler
Created September 5, 2016 14:12
Show Gist options
  • Save romeokienzler/2dc8899eb67f836bb6bf00ce9e9d75ff to your computer and use it in GitHub Desktop.
Save romeokienzler/2dc8899eb67f836bb6bf00ce9e9d75ff to your computer and use it in GitHub Desktop.
import urllib2
import numpy as np
#response = urllib2.urlopen("file:///Users/romeokienzler/Downloads/lorenz.csv")
response = urllib2.urlopen("https://pmqsimulator-romeokienzler-2310.mybluemix.net/data")
data = response.read()
data = data.split("\n")
data.pop()
table = map(lambda s : s.split(';'), data)
x = np.array(map(lambda a: a[1], table))
y = np.array(map(lambda a: a[2], table))
z = np.array(map(lambda a: a[3], table))
x = map(lambda x: float(x), x)
y = map(lambda x: float(x), y)
z = map(lambda x: float(x), z)
xfft = np.fft.fft(x).real
yfft = np.fft.fft(y).real
zfft = np.fft.fft(z).real
train = np.concatenate((xfft, yfft, zfft), axis=0)
train.shape = (3, 3000)
print xfft[0]
print xfft[1]
print train[0,0]
print train[0,1]
# In[174]:
def build_autoencoder(input_var=None):
l_in = InputLayer(shape=(3, 3000), input_var=input_var)
l_hid = DenseLayer(
l_in, num_units=2,
nonlinearity=rectify,
W=lasagne.init.GlorotUniform())
l_out = DenseLayer(
l_hid, num_units=3000,
nonlinearity=softmax)
return l_out
# In[175]:
import theano
import theano.tensor as T
import lasagne
from lasagne.updates import rmsprop
from lasagne.layers import DenseLayer, InputLayer
from lasagne.nonlinearities import rectify, softmax
from lasagne.objectives import squared_error
# Creating the Theano variables
input_var = T.dmatrix('inputs')
target_var = T.dmatrix('targets')
# Building the Theano expressions on these variables
network = build_autoencoder(input_var)
prediction = lasagne.layers.get_output(network)
loss = squared_error(prediction, target_var)
loss = loss.mean()
params = lasagne.layers.get_all_params(network, trainable=True)
updates = rmsprop(loss, params, learning_rate=0.001)
# Compiling the graph by declaring the Theano functions
train_fn = theano.function([input_var, target_var],
loss, updates=updates)
predict_fn = theano.function([input_var, target_var],
loss)
# For loop that goes each time through the hole training
# and validation data
print("Starting training...")
for epoch in range(10):
# Going over the training data
train_err = 0
train_batches = 0
train_err += train_fn(train, train)
train_batches += 1
print("training loss:\t\t{:.6f}".format(train_err / train_batches))
print predict_fn(train,train)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment