How to apply k-means clustering to 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?

As e.g. scikit-learn max 2d arrays inputs. But you can reshape the input.

See python 3.x - Applying Kmeans on 3D data with Scikit-learn - Stack Overflow

E.g. for a basic example with an Autoencoder and MNIST see:

thank you for the links! i think i just don’t understand how reshaping array would not lead to loosing information (it was also in a comments to stackoverflow question but still not getting the clear understanding)

like when i flatten the array from 3d to 1d it turns out to be completely different data, isn’t it?

If don’t think that the Autoencoder output will care about the structure of the data you can try to have an UMAP pass like in:

1 Like