gpt4 book ai didi

python - Tensorflow 滑动窗口转换

转载 作者:太空宇宙 更新时间:2023-11-04 07:33:13 24 4
gpt4 key购买 nike

我想使用 Tensorflow 对 RNN 应用程序进行滑动窗口转换。

对于大小为 4 的窗口,通过 Tensorflow 简单 reshape ,我们可以转换以下张量:

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

最佳答案

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)

关于python - Tensorflow 滑动窗口转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42718649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com