Files
  • main.py
main.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import numpy as np

print(range(3))

temp = np.random.randn(5,10)

print(temp[range(5),1])
print('-----')
print(temp[range(5),1]-1)
print('-----')
temp[range(5),1] -= 1

print(temp[range(5),1])


def stable_softmax(X):
    exps = np.exp(X - np.max(X))
    return exps / np.sum(exps)
def SoftmaxLoss(X, y):
    m = y.shape[0]
    p = stable_softmax(X)
    print(p.shape)
    log_likelihood = -np.log(p[range(m), y])
    loss = np.sum(log_likelihood) / m

    dx = p.copy()
    dx[range(m), y] -= 1
    print(dx.shape)
    dx /= m
    return loss, dx

num_classes, num_inputs = 10, 1
x = 0.001 * np.random.randn(num_inputs, num_classes)
y = np.random.randint(num_classes, size=num_inputs)

print(x.shape)
print(y.shape)
print('-----')

loss,dx = SoftmaxLoss(x,y)

# Test softmax_loss function. Loss should be 2.3 and dx error should be 1e-8
print('Testing SoftmaxLoss:')
print('loss: ', loss)
print('dx error: ', dx)