error.py (1071B)
1 from theano import tensor 2 import theano 3 import numpy 4 5 def const(v): 6 if theano.config.floatX == 'float32': 7 return numpy.float32(v) 8 else: 9 return numpy.float64(v) 10 11 rearth = const(6371) 12 deg2rad = const(3.141592653589793 / 180) 13 14 def hdist(a, b): 15 lat1 = a[:, 0] * deg2rad 16 lon1 = a[:, 1] * deg2rad 17 lat2 = b[:, 0] * deg2rad 18 lon2 = b[:, 1] * deg2rad 19 20 dlat = abs(lat1-lat2) 21 dlon = abs(lon1-lon2) 22 23 al = tensor.sin(dlat/2)**2 + tensor.cos(lat1) * tensor.cos(lat2) * (tensor.sin(dlon/2)**2) 24 d = tensor.arctan2(tensor.sqrt(al), tensor.sqrt(const(1)-al)) 25 26 hd = const(2) * rearth * d 27 28 return tensor.switch(tensor.eq(hd, float('nan')), (a-b).norm(2, axis=1), hd) 29 30 def erdist(a, b): 31 lat1 = a[:, 0] * deg2rad 32 lon1 = a[:, 1] * deg2rad 33 lat2 = b[:, 0] * deg2rad 34 lon2 = b[:, 1] * deg2rad 35 x = (lon2-lon1) * tensor.cos((lat1+lat2)/2) 36 y = (lat2-lat1) 37 return tensor.sqrt(tensor.sqr(x) + tensor.sqr(y)) * rearth 38 39 def rmsle(a, b): 40 return tensor.sqrt( ( (tensor.log(a+1)-tensor.log(b+1)) ** 2 ).mean() )