-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunctions.py
31 lines (26 loc) · 851 Bytes
/
functions.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
###
### Python script to quickly compute the values and derivative values of a
### numerical function for arbitrary inputs (used in the Rust tests).
###
import math
# common functions
sigmoid = lambda x: 1 / (1 + math.exp(-x))
sigmoid_dx = lambda x: sigmoid(x) * (1 - sigmoid(x))
tanh = lambda x: math.tanh(x)
tanh_dx = lambda x: 1 - tanh(x) ** 2
# TO MODIFY
inputs = [-2.0, -1.0, 0.0, 1.0, 2.0]
inputs = [0.5 * v for v in inputs]
function, function_dx = tanh, tanh_dx
# compute
outputs = []
for input in inputs:
output = [function(input), function_dx(input)]
outputs.append(output)
# print to file
digits = 16
with open("functions_outputs.txt", "w") as f:
for output in outputs:
value, derivative = round(output[0], digits), round(output[1], digits)
f.write("{} ||| {}\n".format(value, derivative))
f.write("\n")