解析、执行和计算程序表单问题的预期信息增益。

expected-information-gain的Python项目详细描述


问题程序和预期信息增益

这是一个用于解析/执行问题和计算论文“Question Asking as Program Generation”中战舰数据集上定义的问题程序的预期信息增益(eig)的包。

此包提供纯Python版本(慢)和Python /C++混合版本(FAST)。两个版本都有相同的api,但实现不同。

安装

可以使用pip安装此软件包

pip install expected-information-gain

基本用法

下面的示例演示如何在给定的板上执行程序

# define a board using BattleshipHypothesisfromeig.battleshipimportShip,BattleshipHypothesis,Parser,Executorships=[Ship(ship_label=1,topleft=(0,0),size=2,orientation='V'),Ship(ship_label=2,topleft=(1,2),size=2,orientation='V')hypothesis=BattleshipHypothesis(grid_size=3,ships=ships)# the board looks like this# B W W# B W R# W W R# parse and execute the programquestion=Parser.parse("(bottomright (coloredTiles Red))")executor=Executor(question)executor.execute(hypothesis)# (2, 2)# we can also evaluate general arithmic and logical expressions, with whatever hypothesis providedquestion2=Parser.parse("(and (not (< 4 9)) (== (+ 1 3) 4))")executor2=Executor(question)executor.execute(hypothesis)# False

下一个示例演示如何计算部分显示的板上的预期信息增益

# first we need to construct a hypothesis space # We suggest to do this as an initialization step, and use this instance every time# Because this step is time consuming, and may take several seconds to finish.fromeig.battleshipimportBattleshipHypothesisSpacehypotheses=BattleshipHypothesisSpace(grid_size=6,ship_labels=[1,2,3],ship_sizes=[2,3,4],orientations=['V','H'])# suppose we have a program and a partly revealed boardimportnumpyasnpprogram="..."board=np.array([...])# next we can calculate EIG as followsfromeigimportcompute_eig_basicfromeig.battleship.programimportProgramSyntaxErrortry:score=compute_eig_basic(hypotheses,program,board)exceptProgramSyntaxError:# if the program is invalid, a ProgramSyntaxError will be raised# do somethingexceptRuntimeError:# if error happens during execution, a RuntimeError will be raised# do something

高级用法

我们还提供了一些高级接口,当用户需要计算许多 程序在一个给定的板上,它们还允许用户合并更复杂的分布。

# construct the hypothesis spacefromeig.battleshipimportBattleshipHypothesisSpacehypotheses=BattleshipHypothesisSpace(grid_size=6,ship_labels=[1,2,3],ship_sizes=[2,3,4],orientations=['V','H'])# calculate EIG as followsfromeigimportcompute_eig,Bayes,Contextfromeig.battleshipimportParser,Executorfromeig.battleship.programimportProgramSyntaxErrortry:ast=Parser.parse(program)# parse the program into abstract syntax treeexecutor=Executor(ast)# obtain an executor to execute the programprior=EqualSizesDistribution(ship_labels=[1,2,3])# a more cognitive inspired prior distributionbelief=eig.Bayes(hypotheses,prior)# a prior belief given the hypothesis spacecontext=eig.Context(hypotheses,belief)# context stores the posterior beliefcontext.observe(board)# update posterior belief given the boardscore=eig.compute_eig(executor,context)# compute EIG given program and posterior beliefexceptProgramSyntaxError:# if the program is invalid, a ProgramSyntaxError will be raised# do somethingexceptRuntimeError:# if error happens during execution, a RuntimeError will be raised# do something

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java使用Eclipse Helios for Web项目   java指定行。作为编译器设置的分隔符?   osm映射中带有osmdroid的java标记   java Android ClassNotFoundException在路径:DexPathList上未找到类   java从j2me调用GWT方法   更新到java 8后的aspectj问题   如何仅从根元素java,com提取文本内容。滴虫软件。是的。html   安卓公司。谷歌。火基。数据库DatabaseException:未能转换java类型的值。将字符串改为长字符串   从java中的方法返回arraylist变量   java如果将引用重新分配给同步块内的锁对象,会是什么?   java下载。使用spring mvc的资源文件中的xlsx文件   java Eclipse提供了什么工具来运行Eclipse中未包含的程序?   spring boot Java泛型如何读取传递给泛型方法的类型上的字段或调用方法   Javacard中的javaecdsa签名