我正在尝试在张量流中创建一个递归神经网络。网络的输入是一系列向量。所有输入的序列长度都不同。我想用一批输入来做这件事。
任何人都可以帮我解决这个问题的确切方法吗?我已经阅读了tensorflow网站上的教程,但我仍然不清楚。
答案 0 :(得分:1)
您可以使用已定义的rnn函数here
其中一个参数是sequence_length
sequence_length:指定输入中每个序列的长度。 int32或int64向量(张量)大小[batch_size]。值为[0,T)。
以下是如何实现完整循环
# x, state, sequence_length are placeholders
outputs, final_state = tf.nn.rnn(lstm_cell, x, state, sequence_length = sequence_lengths)
# add softmax layer, define loss, training method, etc
...
# code for one epoch
iterations = total_data_length / batch_size
max_sequence_length = max(all_possible_sequence_lengths)
cur_state = initial_state
for i in range(iterations):
# x is of dimension [max_sequence_length, batch_size, input_size]
# sequence_lengths is of dimension [batch_size]
x_data, sequence_data, y_data = mini_batch(batch_size)
feed_dict = {k: v for k, v in zip(x, x_data)}
feed_dict.append(sequence_lengths: sequence_data, ...)
outs, cur_state, _ = session.run([outputs, final_state, train], feed_dict)
由于以下几个原因,这种方法对我来说有点混乱:
答案 1 :(得分:0)
这取决于您的数据集,但您可以这样做: