我有一个 4 张量 x
。 6 张量 y
计算如下:
x = np.random.randn(64, 28, 28, 1)
strided_shape = 64, 26, 26, 3, 3, 1
y = numpy.lib.stride_tricks.as_strided(x, strided_shape, strides=(x.strides[0], x.strides[1], x.strides[2], x.strides[1], x.strides[2], x.strides[3]))
strided_shape
一般可以是任何形状,只要第一个和最后一个维度与 x
匹配(这只是一个具体示例)。
我的问题是,使用 y
(以及 x.shape
和 x.strides
元组)是否有可能恢复原始数据张量x
,再次使用as_strided
,reshape
,sum
等?注意:我实际上并不打算将上述过程应用于 y
本身;相反,我想在形状与 y
相同的张量上执行该过程。
最佳答案
y
只是 x
的一个 View ,具有不同的形状和步幅。因此,从 y
恢复 x
只是简单地改变形状和步幅。因此,鉴于这些(假设那些在 x
到 y
转换之前保存),它会很简单 -
x = np.lib.stride_tricks.as_strided(y, x.shape, x.strides)
关于python - numpy as_strided 的反函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62447526/