Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Initializing Tensors | PyTorch Introduction
PyTorch Essentials

book
Challenge: Initializing Tensors

Task

Swipe to start coding

  1. Create a 2x3 tensor filled with zeros using the appropriate creation function.
  2. Create a 1D tensor with integer values from 1 to 10 inclusive (11 exclusive) using the appropriate creation function.
  3. Create a 1D tensor with 10 evenly spaced values between 2 and 4 inclusive using the appropriate creation function.

Solution

import torch

# Create a 2x3 tensor filled with zeros
tensor_1 = torch.zeros(2, 3)
# Create a 1D tensor with values from 1 to 10 inclusive
tensor_2 = torch.arange(1, 11)
# Create a 1D tensor with 10 evenly spaced values between 2 and 4
tensor_3 = torch.linspace(2, 4, steps=10)
# Print the tensors
print(tensor_1)
print(tensor_2)
print(tensor_3)
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 4
import torch

# Create a 2x3 tensor filled with zeros
tensor_1 = ___
# Create a 1D tensor with values from 1 to 10 inclusive
tensor_2 = ___
# Create a 1D tensor with 10 evenly spaced values between 2 and 4
tensor_3 = ___
# Print the tensors
print(tensor_1)
print(tensor_2)
print(tensor_3)

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt