I'm using an Autoencoder LSTM in python(Keras). I have a multivariate input and I use a sliding window approach to convert it to the proper format of LSTM input. In the end, I get the output with the same shape as the window. Then I want to convert this array to the original input shape. Can anyone help me how should I do this?
This is my code to put a sliding window on a multivariate signal:
def window(samples, windows_size, step):
m, n = samples.shape
print("\nold shape: ", m, "*", n)
num_signals = n
num_samples = (samples.shape[0] - windows_size) // step + 1
aa = np.empty([num_samples, windows_size, num_signals])
for j in range(num_samples):
for i in range(num_signals):
aa[j, :, i] = samples[(j * step):(j * step + windows_size), i]
samples = aa
m ,n, k = samples.shape
print("new shape: ", m, "*", n, "*", k)
return samples
x = np.asarray([[1,0.1,0.1],[2,0.2,0.2],[3,0.3,0.3],[4,0.4,0.4],
[5,0.5,0.5],[6,0.6,0.6],[7,0.7,0.7],[8,0.8,0.8]])
window(x, 3, 2)
old shape: 8 * 3
new shape: 3 * 3 * 3
Out[65]:
array([[[1. , 0.1, 0.1],
[2. , 0.2, 0.2],
[3. , 0.3, 0.3]],
[[3. , 0.3, 0.3],
[4. , 0.4, 0.4],
[5. , 0.5, 0.5]],
[[5. , 0.5, 0.5],
[6. , 0.6, 0.6],
[7. , 0.7, 0.7]]])
You can use this:
Note: stride is the same concept as in CNNs, number of elements you skip to get the next window.
inp = np.array([[[1. , 0.1, 0.1],
[2. , 0.2, 0.2],
[3. , 0.3, 0.3]],
[[3. , 0.3, 0.3],
[4. , 0.4, 0.4],
[5. , 0.5, 0.5]],
[[5. , 0.5, 0.5],
[6. , 0.6, 0.6],
[7. , 0.7, 0.7]]])
def restitch(array, stride):
flat = array.flatten().reshape(-1,array.shape[2])
keep = [i for i in range(len(flat)) if not(i%(stride+1)==0 and i>0)]
return flat[keep]
restitch(inp, 2)
array([[1. , 0.1, 0.1],
[2. , 0.2, 0.2],
[3. , 0.3, 0.3],
[4. , 0.4, 0.4],
[5. , 0.5, 0.5],
[6. , 0.6, 0.6],
[7. , 0.7, 0.7]])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.