【问题标题】:Tensorflow Sliding Window TransformationTensorFlow 滑动窗口变换
【发布时间】:2017-03-10 12:39:13
【问题描述】:

我想使用 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,如以下张量:

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

我认为通过一些转变,我可以得到我想要的。你有什么想法吗?

我生成上述平铺结果​​的代码很简单,如下所示。但是每个元素将是一个表示瓶颈(来自 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])

【问题讨论】:

  • s=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]d = [s[i:i+4] for i in range(len(s)) if len(s[i:i+4])> 3]
  • @Hesham 您目前使用什么代码来生成您正在展示的这些输出?
  • @dantiston 我已经编辑了我的问题。
  • @dsgdfg 谢谢。但是用 Pyhton 列表做这件事很简单,问题是如何在 TF 图上表示?

标签: python tensorflow


【解决方案1】:

tf.map_fnmap的tensorflow版本

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)

【讨论】:

    【解决方案2】:

    尝试使用:

        from tensorflow.python.ops.signal import shape_ops
        framed_signals = shape_ops.frame(signal, win_len, win_step)
    

    【讨论】:

      猜你喜欢
      • 2022-11-27
      • 2017-07-01
      • 2018-11-09
      • 2012-03-05
      • 2012-08-08
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      相关资源
      最近更新 更多