我有原始数组
test
:
from numpy.lib.stride_tricks import sliding_window_view
test = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14]).reshape(-1,7) # (batch_size, seq_len) -> (2,7)
slided = sliding_window_view(test, window_shape=(3,), axis=-1)
print(test, test.shape)
print(slided, slided.shape)
输出:
[[ 1 2 3 4 5 6 7]
[ 8 9 10 11 12 13 14]] (2, 7)
[[[ 1 2 3]
[ 2 3 4]
[ 3 4 5]
[ 4 5 6]
[ 5 6 7]]
[[ 8 9 10]
[ 9 10 11]
[10 11 12]
[11 12 13]
[12 13 14]]] (2, 5, 3)
给定现有的
slided
数组是通过 sliding_window_view
返回形状 (batch_size, num_win, win_len)
计算的,如何重建回原始数组 test
且形状为 (batch_size, seq_len)
?
已知
batch_size
等于 2
:
batch_size=2
inversed = np.unique(slided.reshape(batch_size, -1)).reshape(batch_size, -1)
输出:
[[ 1 2 3 4 5 6 7]
[ 8 9 10 11 12 13 14]] (2, 7)