我想使用Tensorflow为RNN应用程序进行滑动窗口转换。
对于窗口大小为4,使用Tensorflow简单整形,我们可以转换以下张量:
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
为:
[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]]
但是我希望它能像下面的张量那样大步前进:
[[1,2,3,4],[2,3,4,5],[3,4,5,6],[7,8,9,10],...,[17,18,19,20]]
使用Tensorflow平铺我可以得到:
[[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]]
我认为通过一些转变,我可以得到我想要的东西。你有什么想法吗?
生成上述平铺结果的代码很简单,如下所示。但是每个元素都是1D张量,表示瓶颈(来自CNN的特征向量),而不是上面例子中的数字。
model.logits, model.end_points = inception_v3.inception_v3(model.X_Norm, num_classes=nbrOfOutputNeurons, is_training=is_training)
model.bottleneck = slim.flatten(model.end_points['PreLogits']) # The ouput before FC
x = tf.reshape(model.bottleneck, [1, -1, bottleneck_tensor_size])
x = tf.tile(x, [rnn_time_steps, 1, 1])
答案 0 :(得分:6)
tf.map_fn
是map
x = tf.range(1, 21, dtype=tf.int32)
xm = tf.map_fn(lambda i: x[i:i+4], tf.range(20-4+1), dtype=tf.int32)
with tf.Session() as session:
session.run(tf.global_variables_initializer())
x, xm = session.run([x, xm])
print(x)
print(xm)
答案 1 :(得分:0)
尝试使用:
from tensorflow.python.ops.signal import shape_ops
framed_signals = shape_ops.frame(signal, win_len, win_step)