mehta-lab / waveorder

Wave optical models and inverse algorithms for label-agnostic imaging of density & orientation.
BSD 3-Clause "New" or "Revised" License
15 stars 4 forks source link

Torch warning about copying tensors #139

Open talonchandler opened 1 year ago

talonchandler commented 1 year ago

recOrder tests surfaced the following warnings at optics.py:321, optics.py:370, and stokes.py:291

recOrder/tests/cli_tests/test_reconstruct.py::test_reconstruct
  /Users/talon.chandler/waveorder/waveorder/optics.py:321: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
    * torch.tensor(z_position_list)[:, None, None]

You can generate these warnings with a call like

import torch
torch.tensor(torch.tensor([0,1,2]))[:,None,None]

The core issue seems to be that some optics and stokes functions can take either a list or a tensor.

@ziw-liu do you think we should make sure that only lists are passed? Or can you think of a simpler solution?

This does not seem critical.

ziw-liu commented 1 year ago

We can use a simple util function to convert lists to tensor. This raises a warning because it is an implicit copy and can be quite expensive depending on the size of the tensors.