def sp_subvector_error(self, radius, sp_dimensions, sp_subdimensions=1): """Estimate of representational error of a subvector of a semantic pointer (unit vector). Requires Scipy. Paramaters ---------- radius : float or ndarray Radius of the representing ensemble. sp_dimensions : int Dimensionality of the complete semantic pointer/unit vector. sp_subdimensions : int, optional Dimensionality of the subvector represented by some ensemble. Returns ------- Error estimates for representing a subvector with `subdimensions` dimensions of a `dimensions` dimensional unit vector with an ensemble initialized with of `radius`. """ dist = SubvectorLength(sp_dimensions, sp_subdimensions) in_range = self._sp_subvector_error_in_range(radius, sp_subdimensions) out_of_range = self._sp_subvector_error_out_of_range( radius, sp_dimensions, sp_subdimensions) return dist.cdf(radius) * in_range + ( 1.0 - dist.cdf(radius)) * out_of_range
def test_distributions(): check_init_args(PDF, ["x", "p"]) check_repr(PDF([1, 2, 3], [0.1, 0.8, 0.1])) assert (repr(PDF( [1, 2], [0.4, 0.6])) == "PDF(x=array([1., 2.]), p=array([0.4, 0.6]))") check_init_args(Uniform, ["low", "high", "integer"]) check_repr(Uniform(1, 3)) check_repr(Uniform(1, 4, integer=True)) assert repr(Uniform(0, 1)) == "Uniform(low=0, high=1)" assert repr(Uniform( 0, 5, integer=True)) == "Uniform(low=0, high=5, integer=True)" check_init_args(Gaussian, ["mean", "std"]) check_repr(Gaussian(0, 2)) assert repr(Gaussian(1, 0.1)) == "Gaussian(mean=1, std=0.1)" check_init_args(Exponential, ["scale", "shift", "high"]) check_repr(Exponential(2.0)) check_repr(Exponential(2.0, shift=0.1)) check_repr(Exponential(2.0, shift=0.1, high=10.0)) assert repr(Exponential(2.0)) == "Exponential(scale=2.0)" check_init_args(UniformHypersphere, ["surface", "min_magnitude"]) check_repr(UniformHypersphere()) check_repr(UniformHypersphere(surface=True)) check_repr(UniformHypersphere(min_magnitude=0.3)) assert repr(UniformHypersphere()) == "UniformHypersphere()" assert repr( UniformHypersphere(surface=True)) == "UniformHypersphere(surface=True)" check_init_args(Choice, ["options", "weights"]) check_repr(Choice([3, 2, 1])) check_repr(Choice([3, 2, 1], weights=[0.1, 0.2, 0.7])) assert repr(Choice([1, 2, 3])) == "Choice(options=array([1., 2., 3.]))" assert (repr( Choice([1, 2, 3], weights=[0.1, 0.5, 0.4]) ) == "Choice(options=array([1., 2., 3.]), weights=array([0.1, 0.5, 0.4]))") check_init_args(Samples, ["samples"]) check_repr(Samples([3, 2, 1])) assert repr(Samples([3, 2, 1])) == "Samples(samples=array([3., 2., 1.]))" check_init_args(SqrtBeta, ["n", "m"]) check_repr(SqrtBeta(3)) check_repr(SqrtBeta(3, m=2)) assert repr(SqrtBeta(3)) == "SqrtBeta(n=3)" assert repr(SqrtBeta(3, 2)) == "SqrtBeta(n=3, m=2)" check_init_args(SubvectorLength, ["dimensions", "subdimensions"]) check_repr(SubvectorLength(6)) check_repr(SubvectorLength(6, 2)) assert repr(SubvectorLength(3)) == "SubvectorLength(dimensions=3)" check_init_args(CosineSimilarity, ["dimensions"]) check_repr(CosineSimilarity(6)) assert repr(CosineSimilarity(6)) == "CosineSimilarity(dimensions=6)"
def _sp_subvector_error_out_of_range(radius, dimensions, subdimensions): dist = SubvectorLength(dimensions, subdimensions) sq_r = radius * radius normalization = 1.0 - dist.cdf(radius) b = (dimensions - subdimensions) / 2.0 aligned_integral = beta(subdimensions / 2.0 + 1.0, b) * (1.0 - betainc( subdimensions / 2.0 + 1.0, b, sq_r)) cross_integral = beta((subdimensions + 1) / 2.0, b) * (1.0 - betainc( (subdimensions + 1) / 2.0, b, sq_r)) numerator = (sq_r * normalization + ( aligned_integral - 2.0 * radius * cross_integral) / beta( subdimensions / 2.0, b)) with np.errstate(invalid='ignore'): return np.where( numerator > np.MachAr().eps, numerator / normalization, np.zeros_like(normalization))
def test_argreprs(): def check_init_args(cls, args): assert getfullargspec(cls.__init__).args[1:] == args def check_repr(obj): assert eval(repr(obj)) == obj check_init_args(PDF, ['x', 'p']) check_repr(PDF([1, 2, 3], [0.1, 0.8, 0.1])) check_init_args(Uniform, ['low', 'high', 'integer']) check_repr(Uniform(1, 3)) check_repr(Uniform(1, 4, integer=True)) check_init_args(Gaussian, ['mean', 'std']) check_repr(Gaussian(0, 2)) check_init_args(Exponential, ['scale', 'shift', 'high']) check_repr(Exponential(2.)) check_repr(Exponential(2., shift=0.1)) check_repr(Exponential(2., shift=0.1, high=10.)) check_init_args(UniformHypersphere, ['surface', 'min_magnitude']) check_repr(UniformHypersphere()) check_repr(UniformHypersphere(surface=True)) check_repr(UniformHypersphere(min_magnitude=0.3)) check_init_args(Choice, ['options', 'weights']) check_repr(Choice([3, 2, 1])) check_repr(Choice([3, 2, 1], weights=[0.1, 0.2, 0.7])) check_init_args(Samples, ['samples']) check_repr(Samples([3, 2, 1])) check_init_args(SqrtBeta, ['n', 'm']) check_repr(SqrtBeta(3)) check_repr(SqrtBeta(3, m=2)) check_init_args(SubvectorLength, ['dimensions', 'subdimensions']) check_repr(SubvectorLength(6)) check_repr(SubvectorLength(6, 2)) check_init_args(CosineSimilarity, ['dimensions']) check_repr(CosineSimilarity(6))
def test_argreprs(): def check_init_args(cls, args): assert getfullargspec(cls.__init__).args[1:] == args def check_repr(obj): assert eval(repr(obj)) == obj check_init_args(PDF, ["x", "p"]) check_repr(PDF([1, 2, 3], [0.1, 0.8, 0.1])) check_init_args(Uniform, ["low", "high", "integer"]) check_repr(Uniform(1, 3)) check_repr(Uniform(1, 4, integer=True)) check_init_args(Gaussian, ["mean", "std"]) check_repr(Gaussian(0, 2)) check_init_args(Exponential, ["scale", "shift", "high"]) check_repr(Exponential(2.0)) check_repr(Exponential(2.0, shift=0.1)) check_repr(Exponential(2.0, shift=0.1, high=10.0)) check_init_args(UniformHypersphere, ["surface", "min_magnitude"]) check_repr(UniformHypersphere()) check_repr(UniformHypersphere(surface=True)) check_repr(UniformHypersphere(min_magnitude=0.3)) check_init_args(Choice, ["options", "weights"]) check_repr(Choice([3, 2, 1])) check_repr(Choice([3, 2, 1], weights=[0.1, 0.2, 0.7])) check_init_args(Samples, ["samples"]) check_repr(Samples([3, 2, 1])) check_init_args(SqrtBeta, ["n", "m"]) check_repr(SqrtBeta(3)) check_repr(SqrtBeta(3, m=2)) check_init_args(SubvectorLength, ["dimensions", "subdimensions"]) check_repr(SubvectorLength(6)) check_repr(SubvectorLength(6, 2)) check_init_args(CosineSimilarity, ["dimensions"]) check_repr(CosineSimilarity(6))