0
votes

It may be an easy problem but I could not find any practical solution. My code has following code segment involving 3 nested for loops. The target is to create a specialized intensity matrix for my algorithms for both prediction and ground_truth image matrix as follows:

    for i in range (batch):
      for j in range (img_width):
        for k in range (img_height):
            tensor=prediction[i][j][:]-prediction[i][k][:]
            extracted_intensity_pred[i][j][k]=torch.norm(tensor, 2)
            tensor=ground truth[i][j][:]-ground_truth[i][k][:]
            extracted_intensity_ground_truth[i][j][k]=torch.norm(tensor, 2)

This nested for loop structure is slowing the execution intensively. Is there any broadcasting implementation(in numpy or pytorch tensor based) that may be used?

1

1 Answers

1
votes

first lets clean up some notation; [:] does nothing

But first what's the dimensions, mostly 3d?

for i in range (batch):
      for j in range (img_width):
        for k in range (img_height):
            tensor = prediction[i,j,:] - prediction[i,k,:]
            # looks like a prediction[:,:,None]-prediction[:,None,:]; making 4d?
            extracted_intensity_pred[i,j,k] = torch.norm(tensor, 2)
            # what can torch.norm work with?

so maybe it's just

 tensor = prediction[:,:,None] - prediction(:,None,:]
 extracted_intensity_pred = torch.norm(tensor, ?)