└── simple_seq2seq_autoencoder.py /simple_seq2seq_autoencoder.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | 4 | # Parameters 5 | learning_rate = 0.0001 6 | training_epochs = 10000 7 | display_step = 100 8 | 9 | # Network Parameters 10 | # the size of the hidden state for the lstm (notice the lstm uses 2x of this amount so actually lstm will have state of size 2) 11 | size = 10 12 | # 2 different sequences total 13 | batch_size = 2 14 | # the maximum steps for both sequences is 5 15 | n_steps = 5 16 | # each element/frame of the sequence has dimension of 3 17 | frame_dim = 3 18 | 19 | initializer = tf.random_uniform_initializer(-1, 1) 20 | 21 | # the sequences, has n steps of maximum size 22 | seq_input = tf.placeholder(tf.float32, [n_steps, batch_size, frame_dim]) 23 | # what timesteps we want to stop at, notice it's different for each batch hence dimension of [batch] 24 | # early_stop = tf.placeholder(tf.int32, [batch_size]) 25 | 26 | # inputs for rnn needs to be a list, each item/frame being a timestep. 27 | # we need to split our input into each timestep, and reshape it because split keeps dims by default 28 | encoder_inputs = [tf.reshape(seq_input, [-1, frame_dim])] 29 | # if encoder input is "X, Y, Z", then decoder input is "0, X, Y, Z". Therefore, the decoder size 30 | # and target size equal encoder size plus 1. For simplicity, here I droped the last one. 31 | decoder_inputs = ([tf.zeros_like(encoder_inputs[0], name="GO")] + encoder_inputs[:-1]) 32 | targets = encoder_inputs 33 | weights = [tf.ones_like(targets_t, dtype=tf.float32) for targets_t in targets] 34 | 35 | # basic LSTM seq2seq model 36 | cell = tf.nn.rnn_cell.BasicLSTMCell(size) 37 | _, enc_state = tf.nn.rnn(cell, encoder_inputs, dtype=tf.float32) 38 | cell = tf.nn.rnn_cell.OutputProjectionWrapper(cell, frame_dim) 39 | dec_outputs, dec_state = tf.nn.seq2seq.rnn_decoder(decoder_inputs, enc_state, cell) 40 | 41 | # e_stop = np.array([1, 1]) 42 | 43 | # flatten the prediction and target to compute squared error loss 44 | y_true = [tf.reshape(encoder_input, [-1]) for encoder_input in encoder_inputs] 45 | y_pred = [tf.reshape(dec_output, [-1]) for dec_output in dec_outputs] 46 | 47 | # Define loss and optimizer, minimize the squared error 48 | loss = 0 49 | for i in range(len(y_true)): 50 | loss += tf.reduce_sum(tf.square(tf.sub(y_pred[i], y_true[i]))) 51 | optimizer = tf.train.RMSPropOptimizer(learning_rate).minimize(loss) 52 | 53 | # Initializing the variables 54 | init = tf.initialize_all_variables() 55 | 56 | # Launch the graph 57 | with tf.Session() as sess: 58 | sess.run(init) 59 | # Training cycle 60 | for epoch in range(training_epochs): 61 | # rand = np.random.rand(n_steps, batch_size, frame_dim).astype('float32') 62 | x = np.arange(n_steps * batch_size * frame_dim) 63 | x = x.reshape((n_steps, batch_size, frame_dim)) 64 | feed = {seq_input: x} 65 | # Fit training using batch data 66 | _, cost_value = sess.run([optimizer, loss], feed_dict=feed) 67 | # Display logs per epoch step 68 | if epoch % display_step == 0: 69 | # print sess.run(decoder_inputs, feed_dict=feed) 70 | print "logits" 71 | a = sess.run(y_pred, feed_dict=feed) 72 | print a 73 | print "labels" 74 | b = sess.run(y_true, feed_dict=feed) 75 | print b 76 | 77 | print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(cost_value)) 78 | 79 | print("Optimization Finished!") --------------------------------------------------------------------------------