How to initialise the value of a tensor where a certain value doesn't exist for it

I want to initialise the value of a tensor which doesn’t get value from a certain function as below -

img_path = "Path_to_single_image"
mask = function(img_path)
class0,class1 = mask.unique(return_counts = True)
class0,class1

The general default output looks like this -

tensor([0, 1, 2, 3]) tensor([304960, 13746, 10765, 489])  

I want to loop through multiple files and the issue is many files don’t have all the the same tensors -
so some file may just have only have data as below -
tensor([0, 1, 2]) tensor([304960, 13746, 10765])

I need the above tensor values for some additional operation and the data like above is creating issue.
class0 will always same 4 values if it is present in the file - tensor([0, 1, 2, 3])
while values of class1 may differ.
If any of the value of class0 is present , then I want class1 to be initialised to 0
like

`tensor([0, 1, 2,3]) tensor([304960, 13746, 10765,0]) ` 

But I am not able to do it.
I also tried initialising it using the below method but was not able to do it.

img_path = "Path_to_single_image"
mask = function(img_path)
class0 = torch.tensor([0,1,2,3],dtype = torch.float32)
class1 = torch.zeros([4],dtype = torch.float32)
class0,class1 = mask.unique(return_counts = True)
class0,class1

Output for above code -
tensor([0, 1, 2]) tensor([304960, 13746, 10765])

You could use the returned unique values to index another tensor and add the counts to it:

counts = torch.zeros(4)

data1 = torch.randint(0, 4, (1000, ))
u, c = data1.unique(return_counts=True)
counts[u] += c

data2 = torch.randint(0, 3, (1000, ))
u, c = data2.unique(return_counts=True)
counts[u] += c
1 Like