#!/usr/bin/env python
# coding: utf-8
# # Data Driven Modeling
# ### (Theme of this semester: CODING AS LITERACY)
#
# ### PhD seminar series at Chair for Computer Aided Architectural Design (CAAD), ETH Zurich
#
#
# [Vahid Moosavi](https://vahidmoosavi.com/)
#
#
#
# # 17th Session
#
# 02 May 2017
#
# # Recurrent Neural Nets and Dynamical Systems
# ### To be discussed
# * ** Dynamical Systems**
# * **Weather**
# * **Traffic**
# * **Energy Networks**
# * **Water Flow**
# * **Wind**
# * **Stock Market**
# * **Atmospheric Science (e.g. tele-connection)**
# * **Some Experiments**
#
#
#
# # Unrolled RNN
# - **To linearize in time: Multiple copies of the same cell**
#
# ![](Images/RNN-unrolled.png)
# source: http://colah.github.io/posts/2015-08-Understa/nding-LSTMs/
#
# In[1]:
import warnings
warnings.filterwarnings("ignore")
import datetime
import pandas as pd
# import pandas.io.data
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
import sys
import sompylib.sompy as SOM# from pandas import Series, DataFrame
from ipywidgets import interact, HTML, FloatSlider
import tensorflow as tf
get_ipython().run_line_magic('matplotlib', 'inline')
# # Sine Waves
# In[14]:
# Two signals have the same frequencies but with a shift in time
N = 500
t = np.arange(N)
a = np.random.rand(4)*.61
x = np.sin(a[0]*(t)) #+ np.cos(a[1]*(t)) #+ np.cos(a[2]*(t))#+np.cos(a[3]*(t))+ .1*np.random.rand(N)
plt.plot(x)
# In[15]:
N = 1000
t = np.arange(N)
Waves = []
for i in range(2000):
a = np.random.rand(4)*.6
x = np.sin(a[0]*(t)) #+ np.cos(a[1]*(t)) #+ np.cos(a[2]*(t))#+np.cos(a[3]*(t))+ .1*np.random.rand(N)
Waves.append(x[:,np.newaxis])
Waves = np.asarray(Waves)
Waves.shape
# In[16]:
import random
time_lag = 20
train_test_row=1000
train_data = []
test_data= []
for r in range(train_test_row):
for t in range(0,Waves.shape[1]-time_lag-1):
train_data.append(Waves[r,range(t,t+time_lag+1),:])
train_data = np.asarray(train_data)
random.shuffle(train_data)
# train_data = np.transpose(train_data,[1,0,2]) #time,batch,inputdim
for r in range(train_test_row,train_test_row+1000):
for t in range(0,Waves.shape[1]-time_lag-1):
test_data.append(Waves[r,range(t,t+time_lag+1),:])
test_data = np.asarray(test_data)
# random.shuffle(test_data)
# test_data = np.transpose(test_data,[1,0,2]) #time,batch,inputdim
# In[17]:
print train_data.shape
print test_data.shape
# In[18]:
import warnings
warnings.filterwarnings("ignore")
import numpy as np
import random
from random import shuffle
import tensorflow as tf
NUM_EXAMPLES = 20000
INPUT_SIZE = 1 # 1 bits per timestep
RNN_HIDDEN = 20
OUTPUT_SIZE = 1 # 1 bit per timestep
TINY = 1e-6 # to avoid NaNs in logs
LEARNING_RATE = 0.01
tf.reset_default_graph()
test_input = test_data[:NUM_EXAMPLES,:time_lag,:]
test_output = test_data[:NUM_EXAMPLES,time_lag,:]
train_input = train_data[:NUM_EXAMPLES,:time_lag,:]
train_output = train_data[:NUM_EXAMPLES,time_lag,:]
print "test and training data loaded"
data = tf.placeholder(tf.float32, [None, time_lag,INPUT_SIZE]) #Number of examples, number of input, dimension of each input
target = tf.placeholder(tf.float32, [None, OUTPUT_SIZE])
num_hidden = 24
num_layers=1
cell = tf.nn.rnn_cell.LSTMCell(num_hidden,state_is_tuple=True)
cell = tf.nn.rnn_cell.MultiRNNCell([cell] * num_layers, state_is_tuple=True)
val, _ = tf.nn.dynamic_rnn(cell, data, dtype=tf.float32,)
val = tf.transpose(val, [1, 0, 2])
last = tf.gather(val, int(val.get_shape()[0]) - 1)
weight = tf.Variable(tf.truncated_normal([num_hidden, int(target.get_shape()[1])]))
bias = tf.Variable(tf.constant(0.1, shape=[target.get_shape()[1]]))
# prediction = tf.nn.softmax(tf.matmul(last, weight) + bias)
prediction = tf.matmul(last, weight) + bias
error = tf.reduce_sum(tf.pow(target-prediction, 2))
# error = tf.reduce_mean(error)
# cross_entropy = -tf.reduce_sum(target * tf.log(tf.clip_by_value(prediction,1e-10,1.0)))
optimizer = tf.train.AdamOptimizer()
minimize = optimizer.minimize(error)
# mistakes = tf.not_equal(tf.argmax(target, 1), tf.argmax(prediction, 1))
# error = tf.reduce_mean(tf.cast(mistakes, tf.float32))
accuracy = tf.abs(1-(target -prediction)/target)*100
# In[19]:
print test_input.shape,test_output.shape, train_input.shape,train_output.shape
# In[20]:
init_op = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init_op)
batch_size = 100
no_of_batches = int(len(train_input)) / batch_size
epoch = 50
for i in range(epoch):
ptr = 0
for j in range(no_of_batches):
inp, out = train_input[ptr:ptr+batch_size], train_output[ptr:ptr+batch_size]
ptr+=batch_size
sess.run(minimize,{data: inp, target: out})
if i%2 ==0:
incorrect = sess.run(error,{data: inp, target: out})
print "Epoch {} error: {}".format(i,incorrect*100)
# In[21]:
test_preds = sess.run(prediction,{data: test_input, target: test_output})
# In[22]:
# for i in range(1):
plt.subplot(111)
plt.plot(test_preds[:,0],test_output[:,0],'.')
# ## One Step Ahead Prediction
# In[23]:
fig = plt.figure(figsize=(15,5))
plt.plot(test_output[:150,0],'.-')
plt.plot(test_preds[:150,0],'or')
# ## Several Step ahead prediction
# ## Run sequenctially and replace the vectors with predictions
# In[28]:
N = 1000
t = np.arange(N)
a = np.random.rand(4)*.6
test_wave = np.sin(a[0]*(t)) #+ np.cos(a[1]*(t)) #+ np.cos(a[2]*(t))#+np.cos(a[3]*(t))+ .1*np.random.rand(N)
test_wave_rnn = []
for t in range(0,test_wave.shape[0]-time_lag-1):
test_wave_rnn.append(test_wave[range(t,t+time_lag+1)])
test_wave_rnn = np.asarray(test_wave_rnn)
test_wave_rnn.shape
# In[29]:
r = 0
inp = test_input[r:r+1]
inp = test_wave_rnn[:1,:time_lag][:,:,np.newaxis]
preds = []
for step in range(1,500):
pred = sess.run(prediction,{data:inp})
preds.append(pred[0])
pred_len = len(preds)
if pred_len