2017-08-31 82 views
0

我正在寻找方法将浮点值的可变长度序列嵌入到固定大小的向量中。所述输入格式如下:TensorFlow:如何将浮点序列嵌入固定大小的向量?

[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]

每一行是一个可变长度sequnece,具有最大长度60在一个sequece每个单元为4的浮点值的元组。我已经将零填充为所有序列长度相同。

下面的架构似乎解决了我的问题,如果我使用输出作为输入相同,我需要在中心思想矢量作为序列的嵌入。

在张量流程中,我发现了两种候选方法tf.contrib.legacy_seq2seq.basic_rnn_seq2seqtf.contrib.legacy_seq2seq.embedding_rnn_seq2seq

但是,这些方法似乎被用来解决NLP问题,输入必须是词的离散值。

那么,是否还有其他功能来解决我的问题?

+0

你的意思是你需要一个[复发层(https://www.tensorflow.org/api_guides/python/nn#Recurrent_Neural_Networks),如[LSTM(HTTPS: //www.tensorflow.org/api_docs/python/tf/contrib/rnn/LSTMCell)或[GRU](https://www.tensorflow.org/api_docs/python/tf/contrib/rnn/GRUCell)?这通常用于生成一个依赖于可变数量的固定尺寸输入的矢量(实际上,您的图片似乎具有LSTM单位)。 – jdehesa

回答

0

我已经找到了解决我的问题,使用以下结构,

下面进行编码的LSTMs层系列X1,X2, ...,XN。最后一个输出,即绿色输出,被复制到与上述解码LSTM层的输入相同的计数。该tensorflow代码如下

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) 
1

要将序列编码为固定长度的矢量,您通常使用递归神经网络(RNN)或卷积神经网络(CNN)。

如果您使用循环神经网络,您可以使用上一时间步的输出(序列中的最后一个元素)。这对应于你问题中的思想向量。看看tf.dynamic_rnndynamic_rnn要求您指定要使用的RNN单元的类型。 tf.contrib.rnn.LSTMCelltf.contrib.rnn.GRUCell是最常见的。

如果你想使用CNN你需要使用1维卷积。你需要tf.layers.conv1dtf.layers.max_pooling1d

+0

我想用神经网络将可变长度序列嵌入到固定长度的向量中。我已经将序列拼贴到相同的最大长度 – bourneli

+0

我看到了,我更新了我的答案。希望这可以帮助。 – GeertH

+0

谢谢你们一切 – bourneli

2

所有你需要的只是一个RNN,而不是seq2seq模型,因为seq2seq带有一个额外的解码器,在你的情况下是不必要的。

一个例子的代码:

import numpy as np 
import tensorflow as tf 
from tensorflow.contrib import rnn 

input_size = 4 
max_length = 60 
hidden_size=64 
output_size = 4 

x = tf.placeholder(tf.float32, shape=[None, max_length, input_size], name='x') 
seqlen = tf.placeholder(tf.int64, shape=[None], name='seqlen') 

lstm_cell = rnn.BasicLSTMCell(hidden_size, forget_bias=1.0) 

outputs, states = tf.nn.dynamic_rnn(cell=lstm_cell, inputs=x, sequence_length=seqlen, dtype=tf.float32) 


encoded_states = states[-1] 

W = tf.get_variable(
     name='W', 
     shape=[hidden_size, output_size], 
     dtype=tf.float32, 
     initializer=tf.random_normal_initializer()) 
b = tf.get_variable(
     name='b', 
     shape=[output_size], 
     dtype=tf.float32, 
     initializer=tf.random_normal_initializer()) 

z = tf.matmul(encoded_states, W) + b 
results = tf.sigmoid(z) 

########################### 
## cost computing and training components goes here 
# e.g. 
# targets = tf.placeholder(tf.float32, shape=[None, input_size], name='targets') 
# cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=targets, logits=z)) 
# optimizer = tf.train.AdamOptimizer(learning_rate=0.1).minimize(cost) 
############################### 

init = tf.global_variables_initializer() 



batch_size = 4 
data_in = np.zeros((batch_size, max_length, input_size), dtype='float32') 
data_in[0, :4, :] = np.random.rand(4, input_size) 
data_in[1, :6, :] = np.random.rand(6, input_size) 
data_in[2, :20, :] = np.random.rand(20, input_size) 
data_in[3, :, :] = np.random.rand(60, input_size) 
data_len = np.asarray([4, 6, 20, 60], dtype='int64') 



with tf.Session() as sess: 
    sess.run(init) 
    ######################### 
    # training process goes here 
    ######################### 
    res = sess.run(results, 
      feed_dict={ 
       x: data_in, 
       seqlen: data_len}) 

print(res) 
+0

谢谢你们一样 – bourneli