piantado / lotlib3 Goto Github PK
View Code? Open in Web Editor NEWLanguage of thought library for python 3
Language of thought library for python 3
Awesome library, thank you!
I have a minor design suggestion that I think would make the library a bit more intuitive and easy to use. At the moment, primitive functions are registered by assigning them to builtins.__dict__
. This makes it difficult to benchmark different grammars with different versions of the same primitive functions, especially when those functions are given the same name. Basically you can only use one grammar version per interpreter session.
One alternative would be to have register_primitive
assign the primitive function to a specific grammar instance. You'd have to modify LOTHypothesis.compile_function()
to somehow factor in self.grammar
when evaluating the expression string.
Just a thought - perhaps this change is a little more nuanced than I imagine.
I tried this: a simple right branching grammar for binary sequences
grammar = Grammar(start='S')
# basic symbols
grammar.add_rule('S', '"0"', None, 0.5)
grammar.add_rule('S', '"1"', None, 0.5)
# concatenation
grammar.add_rule('S', 'concat_', ['"0"', 'S'], 1)
grammar.add_rule('S', 'concat_', ['"1"', 'S'], 1)
grammar.add_rule('S', 'concat_', ['"101"', 'S'], 1)
trees = grammar.enumerate_at_depth(1)
for t in trees:
print(t)
get error
`RuntimeError Traceback (most recent call last)
/Users/hh7792/Desktop/Knitting/Knitting.ipynb Cell 6 line 1
16 trees = grammar.enumerate_at_depth(1)
18 # t = eval(str(formula))
---> 19 for t in trees:
20 print(t)
File ~/Desktop/Knitting/LOTlib3/Grammar.py:297, in Grammar.enumerate_at_depth(self, d, nt, leaves)
294 # BVRuleContextManager here makes us remove the rule BEFORE yielding,
295 # or else this will be incorrect. Wasteful but necessary.
296 with BVRuleContextManager(self, fn, recurse_up=False):
--> 297 yieldfn.args = next(myiter)
298 for a in yieldfn.argFunctionNodes():
299 # Update parents
...
--> 316 state[idx] = next(iterators[idx])
317 break # break the idx loop (which would process "carries")
318 except StopIteration:
RuntimeError: generator raised StopIteration`
hi, great library. asking a question here in case it is useful to someone later. I'm not entirely clear on the advantages of using a string versus a LOTLib-provided primitive. For instance, both of these should work the same from the user/modeler perspective:
grammar.add_rule('EXPR', '(%s + %s)', ['EXPR', 'EXPR'], 1.0)
and
grammar.add_rule('EXPR', 'plus_', ['EXPR', 'EXPR'], 1.0)
with the later using the built-in 'plus_' primitive. I understand the registering new primitives is critical for adding custom functionality but unclear why LOTlib3 goes so far as to provide primitive things like addition if they would be interpreted correctly by the python interpreter just as a string.
thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.