Challenge: Performing Mathematical Operations
Tehtävä
Swipe to start coding
- Compute the sum of each row in the given tensor and retain the dimensions of the output.
- Calculate the mean of each column in the tensor.
- Find the maximum value of each row.
- Normalize the tensor by dividing each element by the overall sum of the tensor.
Ratkaisu
import torch
tensor = torch.tensor([[3, 7, 1], [2, 5, 9], [8, 6, 4]]).float()
# Compute the sum of each row and retain the dimensions
row_sum = tensor.sum(dim=1, keepdim=True)
# Calculate the mean of each column
column_mean = tensor.mean(dim=0)
# Find the maximum value of each row
row_max = tensor.max(dim=1)[0]
# Normalize the tensor by dividing each element by the overall sum
overall_sum = tensor.sum()
normalized_tensor = tensor / overall_sum
# Print results
print("Original tensor:\n", tensor)
print("Row sum:\n", row_sum)
print("Column mean:\n", column_mean)
print("Row max:\n", row_max)
print("Normalized tensor:\n", normalized_tensor)
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 1. Luku 8
import torch
tensor = torch.tensor([[3, 7, 1], [2, 5, 9], [8, 6, 4]]).float()
# Compute the sum of each row and retain the dimensions
row_sum = ___
# Calculate the mean of each column
column_mean = ___
# Find the maximum value of each row
row_max = ___[0]
# Normalize the tensor by dividing each element by the overall sum
overall_sum = ___
normalized_tensor = ____
# Print results
print("Original tensor:\n", tensor)
print("Row sum:\n", row_sum)
print("Column mean:\n", column_mean)
print("Row max:\n", row_max)
print("Normalized tensor:\n", normalized_tensor)