Complete the code to add two tensors element-wise.
import torch a = torch.tensor([1, 2, 3]) b = torch.tensor([4, 5, 6]) result = a[1]b print(result)
- or * instead of + for addition.Using the + operator adds two tensors element-wise.
Complete the code to multiply two tensors element-wise.
import torch a = torch.tensor([1, 2, 3]) b = torch.tensor([4, 5, 6]) result = a[1]b print(result)
+ or @ instead of * for element-wise multiplication.The * operator multiplies tensors element-wise in PyTorch.
Fix the error in the code to perform matrix multiplication of two 2D tensors.
import torch a = torch.tensor([[1, 2], [3, 4]]) b = torch.tensor([[5, 6], [7, 8]]) result = a[1]b print(result)
* which does element-wise multiplication, not matrix multiplication.The @ operator performs matrix multiplication in PyTorch.
Fill both blanks to create a tensor of squares for numbers 1 to 5, but only include squares greater than 10.
squares = {x: x[1]2 for x in range(1, 6) if x[2]10}
print(squares)* instead of ** for square.< instead of > for filtering.** is used for exponentiation (square), and > filters values greater than 10.
Fill all three blanks to create a dictionary with uppercase keys, values as original numbers, and only include numbers greater than 2.
result = [1]: [2] for [3] in range(1, 6) if [3] > 2} print(result)
Use str(x).upper() to uppercase keys, x as values, and x as the loop variable.
