How to apply k-means clustering on 4D tensor?

I received tensor of 4 dimensions (805, 8, 8, 128) from latent space of autoencoder (as input it was tensor of (1108, 32, 32, 3) - 1108 arrays of 32x32 pictures of 3 colors)

now I want to apply k-means to my feature tensor of 4 dimensions (805, 8, 8, 128), but every kmeans function I try (from sklearn, scipy etc) it through an error that number of dimensions different than 1 or 2 are not supported

how could i find clusters above all my arrays of feature tensor?

Hi @asya, you should be able to flatten the rightmost three dimensions so that k-means algorithms can view them as a single unstructured tensor:

x = ...
assert x.dim() == 4
x_flat = x.reshape(x.shape[0], -1)
assert x_flat.dim() == 2

won’t it be loosing some information when flattening tensor?

1 Like