Skip to content

Commit 58ba73f

Browse files
author
Feras A Saad
committed
Fix #243, let output SPs read a global memory address space.
Some cleanup to follow.
1 parent 97980cb commit 58ba73f

File tree

4 files changed

+223
-106
lines changed

4 files changed

+223
-106
lines changed

src/utils/general.py

+8
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ def logsumexp(array):
128128
# so a - m <= 0; hence exp(a - m) is guaranteed not to overflow.
129129
return m + math.log(sum(math.exp(a - m) for a in array))
130130

131+
def relerr(expected, actual):
132+
"""Relative error between `expected` and `actual`: ``abs((a - e)/e)``."""
133+
return abs((actual - expected)/expected)
134+
135+
def abserr(expected, actual):
136+
"""Relative error between `expected` and `actual`: ``abs((a - e))``."""
137+
return abs((actual - expected))
138+
131139
def logmeanexp(array):
132140
# https://github.com/probcomp/bayeslite/blob/master/src/math_util.py
133141
inf = float('inf')

src/venturescript/helpers.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright (c) 2010-2016, MIT Probabilistic Computing Project
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the 'License');
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an 'AS IS' BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import venture.lite.types as vt
18+
import venture.lite.value as vv
19+
20+
from venture.lite.sp_help import deterministic_typed
21+
22+
from venture.exception import VentureException
23+
24+
# XXX Mutators generally do not exist in Venture.
25+
def dict_set(d, k, v):
26+
assert isinstance(d, vv.VentureDict)
27+
d.dict[k] = v
28+
29+
def dict_pop(d, k):
30+
assert isinstance(d, vv.VentureDict)
31+
return d.dict.pop(k)
32+
33+
def dict_pop2(d, k):
34+
assert isinstance(d, vv.VentureDict)
35+
return dict_pop(d, k) if k in d.dict else vv.VentureNil()
36+
37+
def check_unbound_sp(ripl, name):
38+
try:
39+
ripl.sample(name)
40+
return False
41+
except VentureException:
42+
return True
43+
44+
def __venture_start__(ripl):
45+
if check_unbound_sp(ripl, 'dict_set'):
46+
ripl.bind_foreign_sp('dict_set', deterministic_typed(
47+
dict_set,
48+
[vt.HomogeneousMappingType(vt.AnyType('k'), vt.AnyType('v')),
49+
vt.AnyType('k'), vt.AnyType('v')],
50+
vt.NilType()))
51+
if check_unbound_sp(ripl, 'dict_pop'):
52+
ripl.bind_foreign_sp('dict_pop', deterministic_typed(
53+
dict_pop,
54+
[vt.HomogeneousMappingType(vt.AnyType('k'), vt.AnyType('v')),
55+
vt.AnyType('k')],
56+
vt.AnyType('v')))
57+
if check_unbound_sp(ripl, 'dict_pop2'):
58+
ripl.bind_foreign_sp('dict_pop2', deterministic_typed(
59+
dict_pop2,
60+
[vt.HomogeneousMappingType(vt.AnyType('k'), vt.AnyType('v')),
61+
vt.AnyType('k')],
62+
vt.AnyType('v')))

0 commit comments

Comments
 (0)