-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcastable
33 lines (24 loc) · 1.05 KB
/
broadcastable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(1) align starting from the right, going to left
example:
D1 = torch.Size([27,27])
D2 = torch.Size([27])
# 27 27
# 27
(2) the dimension sizes must either be equal, one of them is 1, or one of them does not exist.
internally, it will do, create 1 here and will broadcast:
# 27 27
# 1 27
it is a bug
the issue comes from sliently adding the dimension here, which is 1
need to keepdims = True !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
###################### another trick
p = p /p,sum(1,keepdim=True)
--->
p /= p.sum(1,keepdim=True) # inplace operation, faster, less memory
################# how to normalize each row
probs = counts / counts.sum(1,keepdim=True)
each row summartion == 1
################### another way to think of softmax
logits = encoded_input * Weights
counts = logits.exp() # logits can be negative or positive. using exp() will make sure all positives with all original negatives converted to [0,1]
# so that we can normalize it and generate new numbers that can represent probabilities with sum of them == 1