这是一种
NumPystrides用于向量化创建
output_x
-
nrows = input_x.shape[0] - window_size + 1p,q = input_x.shapem,n = input_x.stridesstrided = np.lib.stride_tricks.as_stridedout = strided(input_x,shape=(nrows,window_size,q),strides=(m,m,n))
样品运行-
In [83]: input_xOut[83]: array([[ 0.73089384, 0.98555845, 0.59818726], [ 0.08763718, 0.30853945, 0.77390923], [ 0.88835985, 0.90506367, 0.06204614], [ 0.21791334, 0.77523643, 0.47313278], [ 0.93324799, 0.61507976, 0.40587073], [ 0.49462016, 0.00400835, 0.66401908]])In [84]: window_size = 4In [85]: outOut[85]: array([[[ 0.73089384, 0.98555845, 0.59818726], [ 0.08763718, 0.30853945, 0.77390923], [ 0.88835985, 0.90506367, 0.06204614], [ 0.21791334, 0.77523643, 0.47313278]], [[ 0.08763718, 0.30853945, 0.77390923], [ 0.88835985, 0.90506367, 0.06204614], [ 0.21791334, 0.77523643, 0.47313278], [ 0.93324799, 0.61507976, 0.40587073]], [[ 0.88835985, 0.90506367, 0.06204614], [ 0.21791334, 0.77523643, 0.47313278], [ 0.93324799, 0.61507976, 0.40587073], [ 0.49462016, 0.00400835, 0.66401908]]])
这创建了输入数组的视图,因此在内存方面,我们正在提高效率。在大多数情况下,通过进一步的 *** 作,这也应转化为性能上的好处。让我们验证一下它的视图确实是-
In [86]: np.may_share_memory(out,input_x)Out[86]: True # Doesn't guarantee, but is sufficient in most cases
验证的另一种可靠方法是在其中设置一些值
output并检查输入-
In [87]: out[0] = 0In [88]: input_xOut[88]: array([[ 0. , 0. , 0. ], [ 0. , 0. , 0. ], [ 0. , 0. , 0. ], [ 0. , 0. , 0. ], [ 0.93324799, 0.61507976, 0.40587073], [ 0.49462016, 0.00400835, 0.66401908]])
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)