File size: 1,155 Bytes
792dec0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Standard-library inference for the exported protected ReLU head.

Run: python predict.py -0.8 -0.2 0 0.2 0.8
The reported error bound is conditional on the stored finite-data contract.
The batch-throughput study used NumPy; its timings do not describe this CLI.
"""
from pathlib import Path
import json,sys
from fractions import Fraction


def load_model(path=None):
    path=Path(path) if path else Path(__file__).with_name('relu_head_example.json')
    return json.loads(path.read_text())


def predict(x,model):
    # Exact rational evaluation preserves the exported mathematical model.
    z=Fraction(x)
    head=Fraction(model['exact_head_bias'])+sum(
        Fraction(w)*max(Fraction(0),z-Fraction(k))
        for k,w in zip(model['exact_hinge_knots'],model['exact_hinge_weights']))
    return float(abs(z)*head)


if __name__=='__main__':
    model=load_model();points=[float(x) for x in sys.argv[1:]] or [-.8,-.2,0.,.2,.8]
    print(json.dumps([{'x':x,'residual':predict(x,model),
                       'conditional_error_bound':model['conditional_uniform_error_bound'] if -1<=x<=1 else None}
                      for x in points],indent=2))