Search code examples
tensorflowembeddingautoencoder

TensorFlow: How to embed float sequences to fixed size vectors?


I am looking methods to embed variable length sequences with float values to fixed size vectors. The input formats as following:

[f1,f2,f3,f4]->[f1,f2,f3,f4]->[f1,f2,f3,f4]-> ... -> [f1,f2,f3,f4] [f1,f2,f3,f4]->[f1,f2,f3,f4]->[f1,f2,f3,f4]->[f1,f2,f3,f4]-> ... -> [f1,f2,f3,f4] ... [f1,f2,f3,f4]-> ... -> ->[f1,f2,f3,f4]

Each line is a variable length sequnece, with max length 60. Each unit in one sequece is a tuple of 4 float values. I have already paded zeros to fill all sequences to the same length.

The following architecture seems solve my problem if I use the output as the same as input, I need the thought vector in the center as the embedding for the sequences.

In tensorflow, I have found tow candidate methods tf.contrib.legacy_seq2seq.basic_rnn_seq2seq and tf.contrib.legacy_seq2seq.embedding_rnn_seq2seq.

However, these tow methos seems to be used to solve NLP problem, and the input must be discrete value for words.

So, is there another functions to solve my problems?


Solution

  • I have found a solution to my problem, using the following architecture,

    ,

    The LSTMs layer below encode the series x1,x2,...,xn. The last output, the green one, is duplicated to the same count as the input for the decoding LSTM layers above. The tensorflow code is as following

    series_input = tf.placeholder(tf.float32, [None, conf.max_series, conf.series_feature_num])
    print("Encode input Shape", series_input.get_shape())
    
    # encoding layer
    encode_cell = tf.contrib.rnn.MultiRNNCell(
      [tf.contrib.rnn.BasicLSTMCell(conf.rnn_hidden_num, reuse=False) for _ in range(conf.rnn_layer_num)]
    )
    encode_output, _ = tf.nn.dynamic_rnn(encode_cell, series_input, dtype=tf.float32, scope='encode')
    print("Encode output Shape", encode_output.get_shape())
    
    # last output
    encode_output = tf.transpose(encode_output, [1, 0, 2])
    last = tf.gather(encode_output, int(encode_output.get_shape()[0]) - 1)
    
    # duplite the last output of the encoding layer
    decoder_input = tf.stack([last for _ in range(conf.max_series)], axis=1)
    print("Decoder input shape", decoder_input.get_shape())
    
    # decoding layer
    decode_cell = tf.contrib.rnn.MultiRNNCell(
      [tf.contrib.rnn.BasicLSTMCell(conf.series_feature_num, reuse=False) for _ in range(conf.rnn_layer_num)]
    )
    decode_output, _ = tf.nn.dynamic_rnn(decode_cell, decoder_input, dtype=tf.float32, scope='decode')
    print("Decode output", decode_output.get_shape())
    
    # Loss Function
    loss = tf.losses.mean_squared_error(labels=series_input, predictions=decode_output)
    print("Loss", loss)