【发布时间】:2023-03-30 17:03:01
【问题描述】:
我正在尝试为 tensorflow 主题“不平整”numpy 数组。我需要取一个 NxN 矩阵,例如 27x27,然后逐行取所有行元素(每次 27)并将其重塑为 3x3x3 映射(使用 27 列我将得到 3x3x3 x 27maps),我做到了以下函数:
def unflat_pca(flated_patches, depth=3, verbose=False):
# tensor with shape [components_width, components_height]
p_width = flated_patches.shape[0]
p_height = flated_patches.shape[1]
# Utilizo 3x3 por la ventana de la convolucion
res = np.empty((3,3, depth, p_width))
for one_map in range(p_width):
map_unflat = np.empty((3,3, depth))
current_indx = 0
for d in range(depth):
# flated_patches matriz cuadrada de pca (PxP)
map_unflat[:,:,d] = flated_patches[one_map, current_indx:(current_indx+(3*3))].reshape(3,3)
current_indx += 3*3
res[:,:, d, one_map] = map_unflat[:,:,d]
if verbose:
print("\n-- unflat_pca function --")
print("The initial shape was: " + str(flated_patches.shape))
print("The output shape is: " + str(res.shape) + "\n")
return res #[width, height, depth, pca_maps]
然后当我尝试测试函数时,我通过一个易于跟踪的数组(0,1,2...)来尝试观察函数是否正常工作...
utest = unflat_pca(np.arange(0, 27*27).reshape(27,27), verbose=True)
我明白了
-- unflat_pca 函数-- 最初的形状是:(27, 27) 输出形状为:(3, 3, 3, 27)
完美!但是现在,当我检查结果时,例如使用 utest[:,:,:,0],我希望同一个数组中的所有数字都为 1,2,3.... 但得到了
array([[[ 0., 9., 18.],
[ 1., 10., 19.],
[ 2., 11., 20.]],
[[ 3., 12., 21.],
[ 4., 13., 22.],
[ 5., 14., 23.]],
[[ 6., 15., 24.],
[ 7., 16., 25.],
[ 8., 17., 26.]]])
但是,如果我只检查第一个频道,我会得到预期的结果。
> array([[ 0., 1., 2.],
[ 3., 4., 5.],
[ 6., 7., 8.]])
我很困惑,因为后来我使用了未展开的地图并且得到了不好的结果,我认为这是由于我获得的数字不正确(按列?!)的第一个结果。你可以帮帮我吗?对不起我的英语:P
PS: utest[:,:,:,0] 的期望值 -> 3x3x3 有序映射(宽度、高度、深度):
array([[[ 0., 1., 2.],
[ 3., 4., 5.],
[ 6., 7., 8.]],
[[ 9., 10., 11.],
[ 12., 13., 14.],
[ 15., 16., 17.]],
[[ 18., 19., 20.],
[ 21., 22., 23.],
[ 24., 25., 26.]]])
PS2:纸质第一行示例:First row result
【问题讨论】:
-
你能提供
utest必须看起来如何的例子吗,至少utest[:,:,:,:3]
标签: python arrays numpy tensorflow