0

I am trying to perform dimensionality reduction using PCA, where outputs is a list of tensors where each tensor has a shape of (1, 3, 32,32). Here is the code:

from sklearn.decomposition import PCA
pca = PCA(10)
pca_result = pca.fit_transform(output)

But I keep getting this error, regardless of whatever I tried:

ValueError: only one element tensors can be converted to Python scalars

I know that the tensors with size(1,3, 32,32) is making the issue, since its looking for 1 element as the error puts it, but do not know how to solve it. I have tried flattening each tensor with looping over output (don't know if its the right way of solving this issue), using the following code but it leads to error in pca:

new_outputs = []     
for i in outputs:
            for j in i:
                j = j.cpu()
                j = j.detach().numpy()
                j = j.flatten()
                new_outputs.append(j)

pca_result = pca.fit_transform(new_output)

I would appreciate if anybody can help with this error whether the flattening approach I took, is correct.

PS:I have read the existing posts (post1,post2) discussing this error but none of them could solve my problem.

Neela
  • 87
  • 6

1 Answers1

1

Assuming your Tensors are stored in a matrix with shape like (10, 3, 32, 32) where 10 corresponds to number of Tensors, you should flatten each like that:

import torch
from sklearn.decomposition import PCA
data= torch.rand((10, 3, 32, 32))
pca = PCA(10)
pca_result = pca.fit_transform(data.flatten(start_dim=1))

data.flatten(start_dim=1) makes your data to be in shape (10, 3*32*32)

The error you posted is actually related to one of the post you linked. The PCA estimator expects array-like object with fit() method and you provided a list of Tensors.

Proko
  • 1,871
  • 1
  • 11
  • 23