gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
import os import torch dir = "H:/gelbooruall/embeddings/" alltensors = [] Gather all…
- User
import os import torch
dir = "H:/gelbooru_all/embeddings/" all_tensors = []
Gather all tensors
for path in os.listdir(dir): path = os.path.join(dir, path) if os.path.isdir(path): tensor = torch.load(f'{path}_tensor.pt').to('cuda') # Load each tensor all_tensors.append(tensor)
Concatenate all tensors into one
if all_tensors: combined_tensor = torch.cat(all_tensors, dim=0) # Adjust dim depending on your tensor shape
Process the combined tensor
with torch.no_grad(): scores = model(combined_tensor).cpu().detach()
Print tensor shape to verify
print(scores.shape)
make it so it evals model when loading and stores all on cpu
- Assistant
To modify your script so that you evaluate the model when loading and store all tensors on the CPU, you can make the following changes:
- Ensure the model is in evaluation mode to disable dropout and batch normalization layers (if any).
- Move each tensor to the CPU immediately after loading it.
- Ensure that the combined tensor and output scores are also managed on the CPU.
Here is the revised script:
import os import torch # Ensure your model is defined and loaded # For example: model = YourModelClass(...) # model.load_state_dict(torch.load("path_to_your_model_checkpoint.pth")) model.eval() # Set the model to evaluation mode dir = "H:/gelbooru_all/embeddings/" all_tensors = [] # Gather all tensors for path in os.listdir(dir): path = os.path.join(dir, path) if os.path.isdir(path): tensor = torch.load(f'{path}_tensor.pt').to('cpu') # Load each tensor onto the CPU all_tensors.append(tensor) # Concatenate all tensors into one if all_tensors: combined_tensor = torch.cat(all_tensors, dim=0) # Adjust dim depending on your tensor shape # Process the combined tensor on CPU with torch.no_grad(): scores = model(combined_tensor).cpu() # Ensure output scores are on the CPU # Print tensor shape to verify print(scores.shape)