Tensor computation out of memory problem

I’m trying compute euclidian distance between query and reference images. but because there are too many, out of memory problem occurs. is there a solution that I could use to solve this??

I get the shape of (num_query images, num_reference images) tensor which is the output of the cnn model. thanks