Casting multi-dimensional Tensors to Float - Only one element tensors can be converted to Python scalars

Viewed 21

I have initialized a hook layer within an autoencoder in attempt to extract the features from the dataset during the encoder process. I need to convert the output into a float so it can be visualized within a specific tool. I have used this exact code previously with the same data where the model was a resnet-18 and it worked fine.

However using an autoencoder model I cannot seem to get it to function. The outputs of the aforementioned resnet model were one element tensors such as tensor([[0.3463]]). This model is outputting multi-element tensors that are of size torch.Size([1014, 512])

I have attempted to alter the autoencoder using functions like nn.Flatten(). I have also tried this within the code below, as well torch.unsqueeze, torch.stack and torch.cat.

Is there any method in which I can alter my autoencoder or hook layer extraction code or initial data set up so I can get these into the correct shape/size?

Hook Layer extracting the activations

rn_start = time.time()
rn_output = []

for i in range(len(HB_test)):    
    # Predict the class of the image
    acc = predict(HB_test[i][0], model.encoder, HB_test[i][1])

    # Blank row for dataframe 
    act_row = []
    
    # Add filename
    act_row.append(HB_test.imgs[i][0])
    
    # Add CNN predictions to row
    act_row.append(acc)

    # Add actual class label to row
    act_row.append(acc)
    
    # Extract activations
    for j in range(len(activs.stored[0])):
        x = activs.stored[0][j] 
        act_row.append(float(x))    <-- Line Causing the Error
    
    # Append row to full output
    rn_output.append(tuple(act_row))

rn_end = time.time()

Auto Encoder

class AutoEncoder(nn.Module):
    def __init__(self):
        super(AutoEncoder, self).__init__()
        self.encoder = nn.Sequential(
            nn.Linear(in_features=512, out_features=256),  # N, 512 -> N,128 
            nn.ReLU(),  # Activation Function
            nn.Linear(in_features=256, out_features=128),
            nn.ReLU(),
            nn.Linear(in_features=128, out_features=64),
            nn.ReLU(),  # Activation Function
            nn.Linear(in_features=64, out_features=12),
            nn.ReLU(), 
            nn.Linear(in_features=12, out_features=3)
            #Flatten()
        )
        self.decoder = nn.Sequential(
            nn.Linear(in_features = 3, out_features= 12),
            nn.ReLU(),
            nn.Linear(in_features=12, out_features=64),  # N, 3 -> N,12 
            nn.ReLU(),  # Activation Function
            nn.Linear(in_features=64, out_features=128),
            nn.Linear(in_features=128, out_features=256),
            nn.ReLU(),  # Activation Function
            nn.Linear(in_features=256, out_features=512),
            nn.Tanh()
        )
    
    def forward(self, x):
        encoded = self.encoder(x)
        decoded = self.decoder(encoded)
        return decoded
0 Answers
Related