From the course: PyTorch Essential Training: Deep Learning

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Creating tensors like other tensors

Creating tensors like other tensors

- [Instructor] Sometimes there is a need to create a tensor that has the same device, size, data type, and layout properties as another preference tensor. PyTorch has special functions that allow to achieve this. You can easily spot and memorize those functions as they have the postfix like as a part of the function name. We have explored the tensor creation functions, empty, zero, ones, rand and rand int. And all these functions have similar function, which we call similarity functions with postfix like. So empty has its pair empty like, zeroes have a pair zeroes like and so on. We have already imported PyTorch. We'll start by creating a tensor using zeroes function, call it starting tensor and display it. We'll create a tensor with the same size filled with random values using torch.rand like function. Notice we are using manual seed to have the same numbers generated each time we rerun our code. Let's create another two dimensional tensor. This time we want to have the same shape…

Contents