gpt4 book ai didi

reshape - Theano reshape

转载 作者:行者123 更新时间:2023-12-02 13:46:15 30 4
gpt4 key购买 nike

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

    [batch_size, stack1_size, stack2_size, height, width]

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

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

这样所有的堆栈将被组合在一起成为所有 channel 的一个堆栈。我不确定 reshape 是否会为我做到这一点。我发现,如果像素在中间的维度上混合,则 reshape 似乎不会按字典顺序排列像素。我一直在尝试结合使用 dimshufflereshapeconcatenate 来实现这一目标,但没有成功。我希望得到一些帮助。

谢谢。

最佳答案

Theano reshape工作方式就像 numpy reshape及其默认顺序,即'C':

‘C’ means to read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest.

下面的示例显示,通过 numpy 或 Theano reshape 后,图像像素保持相同的顺序。

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()

关于reshape - Theano reshape ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31798815/

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