Skip to content

Clarification for discussion of shared memory in torch.from_numpy(ndarray) and torch.Tensor.numpy() #926

Open
@pvelayudhan

Description

@pvelayudhan

Thank you for this wonderful resource!

I have noticed that in the "PyTorch and Numpy" video: https://youtu.be/Z_ikDlimN6A?si=fufjAATXrinMXGtu&t=13085 as well as in the online book: https://www.learnpytorch.io/00_pytorch_fundamentals/#pytorch-tensors-numpy

The provided explanations for torch.from_numpy(ndarray) and torch.Tensor.numpy() suggest that the inputs and outputs of these functions are not linked in memory.

The explanation provided is based on the example that the code below:

import torch
import numpy as np

array = np.arange(1.0, 8.0)
tensor = torch.from_numpy(array)

array = array + 1
array, tensor # array and tensor have different values

Will lead to array and tensor having different values, and that lack of a connection occurs at the moment of calling tensor = torch.from_numpy(array).

However it's my understanding that the actual moment of these two variables pointing to different memory addresses is during the addition line, array = array + 1. You can see that both array and tensor are still tied together before this addition in this example:

import torch
import numpy as np

array = np.arange(1.0, 8.0)
tensor = torch.from_numpy(array)

array[0] = 5.0
array, tensor # array and tensor now both start with 5.0

This is topic is clarified better here: https://stackoverflow.com/questions/61526297/pytorch-memory-model-how-does-torch-from-numpy-work

Sorry if I am misunderstanding and thanks again for making all this wonderful content!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions