Inverse function of numpy as_strided

人盡茶涼 提交于 2021-01-28 03:41:28

问题


I have a 4-tensor x. The 6-tensor y is computed as follows:

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 in general can be any shape as long as the first and last dimensions match those of x (this is just a concrete example).

My question is, using y (and the x.shape and x.strides tuples), is it possible to recover the original tensor x, using as_strided again, reshape, sum, etc.? Note: I am not actually planning on applying said process to y itself; rather I want to perform the procedure on a tensor with the same shape as y.


回答1:


Well y is simply a view into x, with different shape and strides. As such, recovering x from y is simply changing back the shape and strides. So, given those (assuming those are saved before the x to y conversion), it would be simply -

x = np.lib.stride_tricks.as_strided(y, x.shape, x.strides)


来源:https://stackoverflow.com/questions/62447526/inverse-function-of-numpy-as-strided

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!