Machine ChallengeIn the Python file, write a PyTorch program to create a 2-dimensional tensor of size 3x3, filled with predefined random float numbers between 0 and 1. Set the seed for the random number generator in PyTorch to 0 using torch.manual_seed(0).Next, compute and print the sum of all the tensor elements. Finally, convert the tensor to a Python list and print it.Example Output:Sum of all tensor elements: 4.724854469299316[[0.7576315999031067, 0.2793108820915222, 0.40306925773620605], [0.7346844673156738, 0.029281556606292725, 0.7998586297035217], [0.3971373438835144, 0.7543719410896301, 0.5695084929466248]]
Question
Machine ChallengeIn the Python file, write a PyTorch program to create a 2-dimensional tensor of size 3x3, filled with predefined random float numbers between 0 and 1. Set the seed for the random number generator in PyTorch to 0 using torch.manual_seed(0).Next, compute and print the sum of all the tensor elements. Finally, convert the tensor to a Python list and print it.Example Output:Sum of all tensor elements: 4.724854469299316[[0.7576315999031067, 0.2793108820915222, 0.40306925773620605], [0.7346844673156738, 0.029281556606292725, 0.7998586297035217], [0.3971373438835144, 0.7543719410896301, 0.5695084929466248]]
Solution
Here is the Python code to solve the problem:
import torch
# Set the seed for the random number generator
torch.manual_seed(0)
# Create a 2-dimensional tensor of size 3x3, filled with random float numbers between 0 and 1
tensor = torch.rand(3, 3)
# Compute and print the sum of all the tensor elements
tensor_sum = torch.sum(tensor)
print('Sum of all tensor elements:', tensor_sum.item())
# Convert the tensor to a Python list and print it
tensor_list = tensor.tolist()
print(tensor_list)
This code first imports the torch module. Then it sets the seed for the random number generator to 0 using torch.manual_seed(0). After that, it creates a 2-dimensional tensor of size 3x3 filled with random float numbers between 0 and 1 using torch.rand(3, 3). The sum of all the tensor elements is computed using torch.sum(tensor) and printed. Finally, the tensor is converted to a Python list using tensor.tolist() and printed.
Similar Questions
# Find the output of the following:tensor_A = torch.tensor([[1, 2], [3, 4], [5, 6]], dtype=torch.float32)tensor_B = torch.tensor([[7, 10], [8, 11], [9, 12]], dtype=torch.float32)torch.matmul(tensor_A, tensor_B) )[[58,64],[139,154]][[27,30,33],[61,68,75],[95,106,117]]value errornone of the above
Consider the following code:1a=torch.tensor([[0,1,1],[1,0,1]])What is the output of a.size() and a.ndimension()?
What's wrong with the following lines of code?1234q=torch.tensor(1.0,requires_grad=False)fq=2q**3+qfq.backward()q.grad1 pointThe parameter requires_grad should be set to Trueq is a float A differentiable function should be used
what is the result of the following:123A = torch.tensor([[0, 1, 1], [1, 0, 1]])B = torch.tensor([[1, 1], [1, 1], [-1, 1]])A_times_B = torch.mm(A,B)1 pointtensor([[0, 2], [0, 2]])tensor([[0, 1], [1, 4]])
Write the output of the following code: import pandas as pd Import numpy as np S = pd.Series(np.random.randn(4)) print(S.ndim)210None of the above
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.