-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy51.py
More file actions
28 lines (18 loc) · 798 Bytes
/
Copy pathpy51.py
File metadata and controls
28 lines (18 loc) · 798 Bytes
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
import numpy as np
softmax_outputs = [[0.7, 0.1, 0.2],
[0.1, 0.5, 0.4],
[0.02, 0.9, 0.08]]
class_targets = [0, 1, 1]
for targ_idx, distribution in zip(class_targets, softmax_outputs):
print(targ_idx, distribution)
softmax_outputs2 = np.array([[0.7, 0.1, 0.2],
[0.1, 0.5, 0.4],
[0.02, 0.9, 0.08]])
class_targets2 = [0, 1, 1]
print(softmax_outputs2[[0, 1, 2], class_targets2])
print(softmax_outputs2[[0], 2])
print(softmax_outputs2[range(len(softmax_outputs2)), class_targets2])
print(-np.log(softmax_outputs2[range(len(softmax_outputs2)), class_targets2]))
neg_log = -np.log(softmax_outputs2[range(len(softmax_outputs2)), class_targets2])
average_loss = np.mean(neg_log)
print(average_loss)