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]:
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))
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))
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))
In [ ]: