Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tensor.prod returns a NaN when there is a negative value in the tensor #2509

Closed
quinton11 opened this issue Nov 18, 2024 · 0 comments · Fixed by #2515
Closed

Tensor.prod returns a NaN when there is a negative value in the tensor #2509

quinton11 opened this issue Nov 18, 2024 · 0 comments · Fixed by #2515
Labels
bug Something isn't working

Comments

@quinton11
Copy link
Contributor

quinton11 commented Nov 18, 2024

Describe the bug
The prod and prod_dim tensor operation results contain NaN values if there is a negative value in the tensor performing the operation. This was tested with both ndarray and tch backends.

To Reproduce

use burn::{
    backend::{libtorch::LibTorchDevice, ndarray::{NdArray, NdArrayDevice}, LibTorch},
    tensor::{backend::Backend, Bool, Shape, Tensor, TensorData}};
    
    
    fn main() {
        type MyBackend = NdArray::<f32>;
        type MyTchBackend = LibTorch::<f32>;
        let device = NdArrayDevice::Cpu;
        let tch_device = LibTorchDevice::Cpu;

        // prod
        let nd_data = Tensor::<MyBackend,2>::from_data([[-1.0, 2.0, 3.0], [5.0, 9.0, 6.0]], &device);
        let nd_tensor = nd_data.clone().prod();
        println("{:?}",nd_tensor)
        // Tensor { primitive: Float(NdArrayTensor { array: [NaN], shape=[1], strides=[1], dynamic ndim=1 }) }

        // prod tch
        let tch_data = Tensor::<MyTchBackend,2>::from_data([[-1.0, 2.0, 3.0], [5.0, 9.0, 6.0]], &tch_device);
        let tch_tensor = tch_data.clone().prod();
        println("{:?}",tch_tensor)
        // Tensor { primitive: Float(TchTensor { tensor: [-1620], phantom: PhantomData<f32> }) }
    }

Expected behavior
Result should be [-1620] instead of NaN.

Screenshots

Desktop (please complete the following information):

  • OS: [e.g. macos]
  • Browser [e.g. chrome, safari]
  • Version [Sequoia 15.0]

Additional context
The prod method calls the float_log implementation for the NdArray Backend which basically takes the natural log of the tensor's data, the result is then summed and the exponent taken.
Naturally the natural log of a negative number is undefined, explaining the NaN.
With pytorch however, negative numbers are catered for.

If this is desired behaviour this issue should be closed.

Edit: Not able to reproduce for the Tch Backend, NdArray however still returning NaN values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants