【问题标题】:Theano ReshapingTheano 重塑
【发布时间】:2015-10-26 05:46:04
【问题描述】:

我无法清楚地理解theanoreshape。我有一个形状的图像矩阵:

    [batch_size, stack1_size, stack2_size, height, width]

,其中有 stack2_size 的图像堆栈,每个都有 stack1_size 的通道。我现在想将它们转换成以下形状:

    [batch_size, stack1_size*stack2_size, 1 , height, width]

这样所有的堆栈将组合在一起成为所有通道的堆栈。我不确定 reshape 是否会为我做到这一点。我看到如果它们在中间的尺寸中混合,那么 reshape 似乎不会按字典顺序排列像素。我一直试图通过dimshufflereshapeconcatenate 的组合来实现这一点,但无济于事。我将不胜感激。

谢谢。

【问题讨论】:

  • 这个操作完全正常,我已经用过很多次了。假设重塑操作的 C 排序,一切都应该是可以解释的。如果您对其行为方式有特定问题,则需要通过提供一个工作示例并说明您认为错误的地方来揭露它。
  • 例如,对于上述设置,我可以使用x.reshape((batch_size,tack1_size*stack2_size, 1 , height, width)) 吗?如果是这样,那会改变词典编排吗?

标签: reshape theano


【解决方案1】:

Theano reshapenumpy reshape 一样工作,默认为 order,即 'C'

‘C’表示使用类 C 的索引顺序读取/写入元素,其中 最后一个轴索引变化最快,回到第一个轴索引 变化最慢。

这是一个示例,显示图像像素在通过 numpy 或 Theano 重塑后保持相同的顺序。

import numpy
import theano
import theano.tensor


def main():
    batch_size = 2
    stack1_size = 3
    stack2_size = 4
    height = 5
    width = 6
    data = numpy.arange(batch_size * stack1_size * stack2_size * height * width).reshape(
        (batch_size, stack1_size, stack2_size, height, width))
    reshaped_data = data.reshape([batch_size, stack1_size * stack2_size, 1, height, width])
    print data[0, 0, 0]
    print reshaped_data[0, 0, 0]

    x = theano.tensor.TensorType('int64', (False,) * 5)()
    reshaped_x = x.reshape((x.shape[0], x.shape[1] * x.shape[2], 1, x.shape[3], x.shape[4]))
    f = theano.function(inputs=[x], outputs=reshaped_x)
    print f(data)[0, 0, 0]


main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-03
    • 2016-07-18
    • 2016-04-22
    • 1970-01-01
    • 2020-07-11
    • 2018-09-12
    • 2017-07-26
    • 2021-08-31
    相关资源
    最近更新 更多