In [1]:
# -*- coding: UTF-8 -*-
#%load_ext autoreload
%reload_ext autoreload
%autoreload 2
In [2]:
from __future__ import division
import tensorflow as tf
from os import path
import numpy as np
import pandas as pd
import csv
from sklearn.model_selection import StratifiedShuffleSplit
from time import time
from matplotlib import pyplot as plt
import seaborn as sns
from mylibs.jupyter_notebook_helper import show_graph
from tensorflow.contrib import rnn
from tensorflow.contrib import learn
import shutil
from tensorflow.contrib.learn.python.learn import learn_runner
from IPython.display import Image
from IPython.core.display import HTML
from mylibs.tf_helper import getDefaultGPUconfig
from nn_io.binary_shifter_data_provider import BinaryShifterDataProvider
from models.shift_func import ShiftFunc
In [3]:
dtype = tf.float32
seed = 16011984
random_state = np.random.RandomState(seed=seed)
config = getDefaultGPUconfig()
%matplotlib inline
In [4]:
def plot(loss_list, predictions_series, batchX, batchY):
plt.subplot(2, 3, 1)
plt.cla()
plt.plot(loss_list)
for batch_series_idx in range(5):
one_hot_output_series = np.array(predictions_series)[:, batch_series_idx, :]
single_output_series = np.array([(1 if out[0] < 0.5 else 0) for out in one_hot_output_series])
plt.subplot(2, 3, batch_series_idx + 2)
plt.cla()
plt.axis([0, truncated_backprop_len, 0, 2])
left_offset = range(truncated_backprop_len)
plt.bar(left_offset, batchX[batch_series_idx, :], width=1, color="blue")
plt.bar(left_offset, batchY[batch_series_idx, :] * 0.5, width=1, color="red")
plt.bar(left_offset, single_output_series * 0.3, width=1, color="green")
plt.draw()
plt.pause(0.0001)
In [5]:
num_epochs = 10
num_instances = 100
total_series_length = 600
num_features = 1 #just one here, the function we are predicting is one-dimensional
truncated_backprop_len = 15
state_size = 4
num_classes = 2 #we only have ones and zeros in our series
echo_step = 3 #this is only for this problem where we are shifting the generated sequence
batch_size = 5
In [6]:
num_batches = total_series_length // batch_size // truncated_backprop_len
print total_series_length / batch_size / truncated_backprop_len
num_batches
Out[6]:
In [7]:
# dp = BinaryShifterDataProvider(N_instances=num_instances,
# total_series_length=total_series_length,
# echo_step=echo_step,
# truncated_backprop_len=truncated_backprop_len)
In [8]:
model = ShiftFunc(rng=random_state, dtype=dtype, config=config, batch_size=batch_size,
truncated_backprop_len=truncated_backprop_len, state_size=state_size)
In [9]:
graph = model.getGraph(batch_size=batch_size, num_classes=num_classes)
In [10]:
#show_graph(graph)
In [11]:
stats = model.run(num_instances=num_instances,epochs=num_epochs,
total_series_length=total_series_length,
truncated_backprop_len=truncated_backprop_len, echo_step=echo_step)
In [26]:
with tf.Session(graph=graph, config=config) as sess:
#sess.run(tf.initialize_all_variables())
sess.run(tf.global_variables_initializer())
#interactive mode
plt.ion()
#initialize the figure
plt.figure()
#show the graph
plt.show()
loss_list = []
#init the hidden state #TODO why do it on every epoch ???
_current_state = np.zeros((batch_size, state_size))
for epoch in xrange(num_epochs):
xx, yy = generateData()
for batch in xrange(num_batches): #iterating our data provider
start_ind = batch * truncated_backprop_len
end_ind = start_ind + truncated_backprop_len
batchX = xx[:, start_ind:end_ind]
batchY = yy[:, start_ind:end_ind]
_total_loss, _train_step, _predictions_series, _current_state = sess.run([
total_loss, train_step, predictions_series, last_state
], feed_dict= {
batchX_placeholder: batchX,
batchY_placeholder: batchY,
init_state: _current_state,
}) #note that the _current_state gets updated and then feeded back in
loss_list.append(_total_loss)
if batch % 100 == 0:
print "Step {} Loss {}".format(batch, _total_loss)
plot(loss_list, _predictions_series, batchX, batchY)
plt.ioff()
plt.show()
In [28]:
aa = np.array([[0, 1],
[2, 3],
[4, 5]])
aa
Out[28]:
In [31]:
np.ravel(aa, order='F')
Out[31]:
In [32]:
np.reshape(aa, (2, 3), order='F')
Out[32]:
In [34]:
np.reshape(np.ravel(aa, order='F'), (2, 3))
Out[34]:
In [ ]: