Pytorch Torch As_Tensor
# PyTorch torch.as_tensor Function
* * Pytorch torch Reference Manual](#)
`torch.as_tensor` is a function in PyTorch used to convert data into tensors. Unlike `torch.tensor`, it shares data memory whenever possible.
### Function Definition
torch.as_tensor(data, dtype=None, device=None)
* * *
## Usage Examples
## Example
import torch
import numpy as np
# Create from NumPy array (shared memory)
numpy_array = np.array([1,2,3])
x = torch.as_tensor(numpy_array)
# Modify NumPy array
numpy_array=100
print("Tensor:", x)# Tensor will also change
Output result:
Tensor: tensor([100, 2, 3])
* * Pytorch torch Reference Manual](#)
YouTip