I am not understand this line #238
-
I have completed the course but I don't understand this one line;
what and why you used it instead of using the previous
Hopefully anyone can explain this to me. Huge thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Hi, I am still learning, but they are the same. Aren't they? By |
Beta Was this translation helpful? Give feedback.
-
Is your question still unanswered? If not have a look at the below example it might help: This part makes two random boolean list of size 10 each: import torch
torch.manual_seed(SEED)
X = torch.randint(0, 2, (10,))
Y = torch.randint(0, 2, (10,))
Output:
```python
(tensor([0, 1, 0, 1, 1, 1, 1, 0, 1, 1])
tensor([0, 1, 1, 0, 1, 1, 1, 1, 1, 1])) Then by the following line we actually count the indices where two arrays have the same value: sum = (X == Y).sum() The output is look like: tensor(7) And to get the value itself: sum = sum.item() Output: 7.0 Which means that 7 entries in the arrays have the same values:
Now if we divide this value ( acc = sum / len(X) The output is: 0.7 Which we can multiply it by 100 and return it as a percentage of accuracy: print(f"{(sum/len(X))*100}%") Output: 70.0% |
Beta Was this translation helpful? Give feedback.
-
Hi @Ammar-Azman, @AlienSarlak is correct (in the above), the two equations are the same. You can use both of them or either of them.
With tensors, it's basically like saying "are the elements in this tensor equal to the elements in this other tensor? See this example: import torch
# Create three tensors
A = torch.tensor([1, 2, 3])
B = torch.tensor([1, 2, 3])
C = torch.tensor([4, 5, 6])
# See if they are equal
print(A == B) # equal
print(A == C) # not equal
print(B == C) # not equal >>>
tensor([True, True, True])
tensor([False, False, False])
tensor([False, False, False]) |
Beta Was this translation helpful? Give feedback.
Is your question still unanswered? If not have a look at the below example it might help:
This part makes two random boolean list of size 10 each:
Then by the following line we actually count the indices where two arrays have the same value:
The output is look like:
And to get the value itself:
Output:
7.0
Which means that 7 entries in the arrays have the same values: