-
Hello, I have 1 question regarding Qn 9, Find the maximum and minimum index values of the output of 7. The output of qn7 is a 2-dimensional tensor
but the output index values are not representative of the position of the maximum and/or minimum value. For example, in the case of maximum value, the index position should be [1, 1] or [1][1] to arrive at the maximum value, but it shows the output as 3, i.e. (tensor(3, device='cuda:0'). As the lesson video only shows 1-dimensional tensor as demonstration, I am not sure how the code can be modified to get the accurate index position. I refer to the solution and the output is also shown as 3 which isn't correct logically. Code: # Find arg max
tensor_C.argmax()
# Find arg min
tensor_C.argmin()
tensor_C.argmax(), tensor_C.argmin() |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
You should indicate the dim from which you want to find the min or maxFirst time, it may be a bit confusing. I hope this explanation will help you:Moreover, it is more understandable if you use bigger matrices |
Beta Was this translation helpful? Give feedback.
-
I wrote a random example to summarise and clarify my last reply |
Beta Was this translation helpful? Give feedback.
-
I read it from the book "Deep Learning with PyTorch". |
Beta Was this translation helpful? Give feedback.
-
Hi @chewyh26, First, fantastic question, the exercise + answers are a little confusing now I read them again. And @JahoNematov thank you for your help and offerings. I've got a hacked-together solution from some research online. Namely via the use of NumPy's Again this is where the confusion comes in because this is a PyTorch course not a NumPy course. But for most NumPy functions there is a PyTorch equivalent. You can achieve the desired outcome of question 9 "Find the maximum and minimum index values of the output of 7" via the following: import torch
x = torch.tensor([[0.3647, 0.4709],
[0.5184, 0.5617]])
x
Now to find the max indexes: def find_max_index_with_numpy(x):
# Find the max value with torch.max()
max = torch.max(x)
# Find the max indices with unravel_index()
max_index = np.unravel_index(x.argmax(), x.shape)
# Index into the target tensor with the max indices
max_with_indexing = x[max_index]
print(f"Max with torch.max(): {max}")
print(f"Max index: {max_index}")
print(f"Max with indexing: {max_with_indexing}")
print(f"Do they match? {max == max_with_indexing}")
find_max_index_with_numpy(x)
Teaching the insides of These resources helped me to learn about this:M
Let me know if you have anymore questions :) |
Beta Was this translation helpful? Give feedback.
Hi @chewyh26,
First, fantastic question, the exercise + answers are a little confusing now I read them again.
And @JahoNematov thank you for your help and offerings.
I've got a hacked-together solution from some research online.
Namely via the use of NumPy's
unravel_index()
.Again this is where the confusion comes in because this is a PyTorch course not a NumPy course. But for most NumPy functions there is a PyTorch equivalent.
You can achieve the desired outcome of question 9 "Find the maximum and minimum index values of the output of 7" via the following: