-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoftmax_LUT.py
38 lines (32 loc) · 922 Bytes
/
Softmax_LUT.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
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Given:
# f(x1 | (x1, x2, x3, x4)) = e^x1 / (e^x1 + e^x2 + e^x3 + e^x4)
# Required:
# Calculation of f(x1 | (x1, x2, x3, x4))
#-----------------------------------
import numpy as np
import math
def f_x1(x1,x2,x3,x4):
return math.e**x1/(math.e**x1 + math.e**x2 + math.e**x3 + math.e**x4)
def f_exp(x):
return math.e**x
def f_inv(x):
return 1/x
# Initialize
xmin = -5
xmax = +5
S = 128
dx = (xmax - xmin)/S
# Build up the look-up tables
exp = [f_exp(x) for x in np.arange(xmin,xmax,dx)]
inv = [f_inv(x) for x in np.arange(xmin,xmax,dx)]
# look-up example
x1 = -0.55
x2 = -1
x3 = 0.5
x4 = 0.87
summation = sum([exp[int((xi-xmin)//dx)] for xi in [x1, x2, x3, x4]])
inv_sum = inv[int((summation-xmin)//dx)]
y = exp[int((x1-xmin)//dx)] * inv_sum
print("Look-up Value: ", y) # >> 0.11050558524263025
print("Function Value : ", f_x1(x1,x2,x3,x4)) # >> 0.11584264325120966