Learn practical skills, build real-world projects, and advance your career
Updated 3 years ago
Some Pytorch functions on Tensor
These are function of pytorch which helps in creating tensors.
- torch.from_numpy
- torch.ones
- torch.zeros
- torch.eye
- torch.empty
Before we begin, let's install and import PyTorch
# Uncomment and run the appropriate command for your operating system, if required
# Linux / Binder
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
# Windows
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
# MacOS
# !pip install numpy torch torchvision torchaudio
# Import torch and other required modules
import torch
import numpy as np
Function 1 - torch.from_numpy
This function is used to create a tensor from numpy dataset.
# Example 1 - working (change this)
x=np.array([1,2])
z=torch.from_numpy(x)
z
tensor([1, 2])