tensorflow或numpy中矩阵的特殊平铺
考虑T(w x h x d)的三维张量 目标是通过以一种独特的方式沿三维平铺来创建一个R(w x h x K)张量,其中K=d x K 张量应在第三维k次中重复每个切片,这意味着:tensorflow或numpy中矩阵的特殊平铺,numpy,tensorflow,array-broadcasting,tiling,Numpy,Tensorflow,Array Broadcasting,Tiling,考虑T(w x h x d)的三维张量 目标是通过以一种独特的方式沿三维平铺来创建一个R(w x h x K)张量,其中K=d x K 张量应在第三维k次中重复每个切片,这意味着: T[:,:,0]=R[:,:,0:k] and T[:,:,1]=R[:,:,k:2*k] 标准平铺有一个细微的区别,它给出了T[:,:,0]=R[:,:,::k],在三维空间中每第k个点重复一次。使用np。沿着该轴重复- np.repeat(T,k,axis=2) 样本运行- In [688]: # Setu
T[:,:,0]=R[:,:,0:k] and T[:,:,1]=R[:,:,k:2*k]
标准平铺有一个细微的区别,它给出了
T[:,:,0]=R[:,:,::k]
,在三维空间中每第k个点重复一次。使用np。沿着该轴重复-
np.repeat(T,k,axis=2)
样本运行-
In [688]: # Setup
...: w,h,d = 2,3,4
...: k = 2
...: T = np.random.randint(0,9,(w,h,d))
...:
...: # Original approach
...: R = np.zeros((w,h,d*k),dtype=T.dtype)
...: for i in range(4):
...: R[:,:,i*k:(i+1)*k] = T[:,:,i][...,None]
...:
In [692]: T
Out[692]:
array([[[4, 5, 6, 4],
[5, 4, 4, 3],
[8, 0, 0, 8]],
[[7, 3, 8, 0],
[8, 7, 0, 8],
[3, 6, 8, 5]]])
In [690]: R
Out[690]:
array([[[4, 4, 5, 5, 6, 6, 4, 4],
[5, 5, 4, 4, 4, 4, 3, 3],
[8, 8, 0, 0, 0, 0, 8, 8]],
[[7, 7, 3, 3, 8, 8, 0, 0],
[8, 8, 7, 7, 0, 0, 8, 8],
[3, 3, 6, 6, 8, 8, 5, 5]]])
In [691]: np.allclose(R, np.repeat(T,k,axis=2))
Out[691]: True
或者使用np.tile
和重塑
-
np.tile(T[...,None],k).reshape(w,h,-1)
不需要重塑np。repeat
接受一个axis
参数。@user2357112是的,太复杂了。谢谢,谢谢。刚刚遇到了np。重复。然而,在tensorflow中没有对应的same@user2527599怎么样?@user2527599那么,发布的解决方案对你有用吗?那么,发布的解决方案对你有用吗?