Suppose, I have an integer tensor and it contains some unique and some duplicative numbers. Is it possible to create a tensor of the same length filled with random numbers that will be the same at those indices, where elements from initial tensor are the same? Like if elements from first tensor were seeds for producing random numbers in second tensor?
Can you give an input and expected output?
I do not understand what this bit means:
where elements from initial tensor are the same?
Ok, for example:
input: [5, 2, 2, 4, 2, 7, 5]
;
output: [322, 67, 67, 3938, 67, 190, 322]
import torch
# Define the input tensor
input_tensor = torch.tensor([5, 2, 2, 4, 2, 7, 5])
# Create an empty tensor to store the output
output_tensor = torch.empty_like(input_tensor, dtype=torch.int32)
# Use a dictionary to store the mapping of input value to generated random number
seed_to_random = {}
# Generate random numbers using the input values as seeds
for i, value in enumerate(input_tensor):
if value.item() not in seed_to_random:
torch.manual_seed(value.item()) # Use the value as a seed
seed_to_random[value.item()] = torch.randint(0, 10000, (1,)).item()
output_tensor[i] = seed_to_random[value.item()]
print(input_tensor)
print(output_tensor)
I think this works?
Thanks, but my input tensor will be about half of a million long and for loop will take a very long time. Actually, I’ve already found a vectorized solution for my task