In [46]:
import numpy as np
import tensorflow as tf

In [ ]:
# https://github.com/guillaume-chevalier/seq2seq-signal-prediction

In [49]:
voc = 10
n_steps = 14
n_input = 2
n_output = 1

In [54]:
def get_batch(batch_size):
    x = np.random.randint(voc, size=[n_steps, batch_size, n_input])
    y = np.flip(x, axis=0)
    y = np.sum(y, axis=2).reshape((n_steps, batch_size, n_output))
    return x, y

x, y = get_batch(100)
'''
print(x.shape)
print(y.shape)
print(x[:, 0, :])
print(y[:, 0])
#print(y[0])
'''


Out[54]:
'\nprint(x.shape)\nprint(y.shape)\nprint(x[:, 0, :])\nprint(y[:, 0])\n#print(y[0])\n'

In [55]:
n_hidden = 10
layers_stacked_count = 2  # Number of stacked recurrent cells, on the neural depth axis. 

tf.reset_default_graph()

sess = tf.InteractiveSession()

enc_inp = [tf.placeholder(tf.float32, [None, n_input]) for i in range(n_steps)]
expect = [tf.placeholder(tf.float32, [None, n_output]) for i in range(n_steps)]
dec_inp = [ tf.zeros_like(enc_inp[0], dtype=tf.float32, name="GO") ] + enc_inp[:-1]

cells = [tf.contrib.rnn.BasicLSTMCell(n_hidden) for i in range(layers_stacked_count)]
cell = tf.contrib.rnn.MultiRNNCell(cells)

dec_outputs, dec_states = tf.contrib.legacy_seq2seq.basic_rnn_seq2seq(enc_inp, dec_inp, cell)

w_out = tf.Variable(tf.random_normal([n_hidden, n_output]))
b_out = tf.Variable(tf.random_normal([n_output]))
reshaped_outputs = [(tf.matmul(i, w_out) + b_out) for i in dec_outputs]

diff = tf.squared_difference(expect, reshaped_outputs)
individual_loss = tf.reduce_sum(diff, axis=0)
loss = tf.reduce_mean(individual_loss, axis=0)

optimiser = tf.train.AdamOptimizer(1e-3)
training_op = optimiser.minimize(loss)

init_op = tf.global_variables_initializer()

In [56]:
sess = tf.InteractiveSession()

init_op.run()

for e in range(10):
    for i in range(100):
        batch_x, batch_y = get_batch(50)
        feed_dict = {enc_inp[t]:batch_x[t] for t in range(len(enc_inp))}
        feed_dict.update({expect[t]: batch_y[t] for t in range(len(expect))})
        training_op.run(feed_dict=feed_dict)
        
    print(loss.eval(feed_dict=feed_dict))


[ 258.29586792]
[ 255.52160645]
[ 230.65081787]
[ 248.21842957]
[ 240.53517151]
[ 215.2906189]
[ 192.46194458]
[ 190.83810425]
[ 184.46020508]
[ 189.09442139]

In [65]:
test_x, test_y = get_batch(1)
print(test_y.reshape((-1)))
print('---------')
feed_dict = {enc_inp[t]:test_x[t] for t in range(len(enc_inp))}
pred = [int(i.eval(feed_dict=feed_dict)) for i in reshaped_outputs]
print(np.array(pred))


[ 9  7  4 14  8  9 11  6 13  8 10 11  8  0]
---------
[ 8  6  5 13  7  9 10  6 12  7 10 10  6  1]

In [64]:
for e in range(20):
    for i in range(100):
        batch_x, batch_y = get_batch(50)
        feed_dict = {enc_inp[t]:batch_x[t] for t in range(len(enc_inp))}
        feed_dict.update({expect[t]: batch_y[t] for t in range(len(expect))})
        training_op.run(feed_dict=feed_dict)
        
    print(loss.eval(feed_dict=feed_dict))


[ 55.52490997]
[ 45.62505341]
[ 50.34844208]
[ 49.02774429]
[ 45.09157562]
[ 53.64335632]
[ 42.15190125]
[ 45.09565735]
[ 40.51711655]
[ 39.62434006]
[ 42.93576813]
[ 36.71058655]
[ 30.19380569]
[ 30.20914459]
[ 30.45394897]
[ 28.07727051]
[ 26.55237579]
[ 24.03799057]
[ 23.85711479]
[ 26.63663101]

In [ ]: