Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
eb39ced0
提交
eb39ced0
authored
5月 21, 2008
作者:
Olivier Breuleux
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cleaned up and tested pprint
上级
4573a825
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
86 行增加
和
59 行删除
+86
-59
_test_pprint.py
_test_pprint.py
+47
-0
pprint.py
pprint.py
+39
-59
没有找到文件。
_test_pprint.py
0 → 100644
浏览文件 @
eb39ced0
import
unittest
from
pprint
import
*
import
tensor
as
T
import
gof
class
_test_pp
(
unittest
.
TestCase
):
def
check
(
self
,
expr
,
expected
):
s
=
pp
.
process
(
expr
)
self
.
failUnless
(
s
==
expected
,
"for (
%
s), pp produced (
%
s)"
%
(
expected
,
s
))
def
test_operator_precedence
(
self
):
x
,
y
,
z
=
T
.
matrices
(
'xyz'
)
self
.
check
(
x
+
y
*
z
,
"x + y * z"
)
self
.
check
((
x
+
y
)
*
z
,
"(x + y) * z"
)
self
.
check
(
x
*
y
**
z
,
"x * y ** z"
)
self
.
check
(
z
**
x
*
y
,
"z ** x * y"
)
self
.
check
((
x
*
y
)
**
z
,
"(x * y) ** z"
)
self
.
check
(
z
**
(
x
*
y
),
"z ** (x * y)"
)
def
test_unary_minus_precedence
(
self
):
x
,
y
,
z
=
T
.
matrices
(
'xyz'
)
self
.
check
(
-
x
+
y
,
"-x + y"
)
self
.
check
(
-
(
x
*
y
),
"-(x * y)"
)
self
.
check
((
-
x
)
*
y
,
"-x * y"
)
self
.
check
(
x
*-
y
,
"x * -y"
)
self
.
check
(
x
/-
y
,
"x / -y"
)
self
.
check
(
-
x
**
y
,
"-x ** y"
)
self
.
check
((
-
x
)
**
y
,
"(-x) ** y"
)
self
.
check
(
-
(
x
**
y
),
"-x ** y"
)
self
.
check
(
x
**-
y
,
"x ** (-y)"
)
def
test_parenthesizing
(
self
):
x
,
y
,
z
=
T
.
matrices
(
'xyz'
)
self
.
check
(
x
*
(
y
*
z
),
"x * y * z"
)
self
.
check
(
x
/
(
y
/
z
)
/
x
,
"x / (y / z) / x"
)
self
.
check
((
x
**
y
)
**
z
,
"(x ** y) ** z"
)
self
.
check
(
x
/
(
y
*
z
),
"x / (y * z)"
)
self
.
check
(
x
*
(
y
/
z
),
"x * y / z"
)
self
.
check
(
x
/
y
*
z
,
"x / y * z"
)
if
__name__
==
'__main__'
:
unittest
.
main
()
pprint.py
浏览文件 @
eb39ced0
import
tensor
as
T
import
gof
from
copy
import
copy
class
PrinterState
(
gof
.
utils
.
scratchpad
):
...
...
@@ -31,8 +33,6 @@ class OperatorPrinter:
outer_assoc
=
getattr
(
pstate
,
'assoc'
,
'none'
)
if
outer_precedence
>
self
.
precedence
:
parenthesize
=
True
#elif outer_assoc != self.assoc:
# parenthesize = True
else
:
parenthesize
=
False
input_strings
=
[]
...
...
@@ -81,12 +81,10 @@ class DimShufflePrinter:
def
process
(
self
,
r
,
pstate
):
if
r
.
owner
is
None
:
raise
TypeError
(
"Can only print DimShuffle."
)
elif
isinstance
(
r
.
owner
.
op
,
ShuffleRule
):
#print r, r.owner.op
new_r
=
r
.
owner
.
op
.
expand
(
r
.
owner
)
#print new_r.owner, isinstance(new_r.owner.op, ShuffleRule)
return
self
.
process
(
new_r
,
pstate
)
elif
isinstance
(
r
.
owner
.
op
,
DimShuffle
):
elif
isinstance
(
r
.
owner
.
op
,
T
.
ShuffleRule
):
new_r
=
r
.
owner
.
op
.
expand
(
r
.
owner
)[
0
]
return
pstate
.
pprinter
.
process
(
new_r
,
pstate
)
elif
isinstance
(
r
.
owner
.
op
,
T
.
DimShuffle
):
ord
=
r
.
owner
.
op
.
new_order
return
self
.
__p
(
ord
,
pstate
,
r
.
owner
.
inputs
[
0
])
else
:
...
...
@@ -114,26 +112,6 @@ class LeafPrinter:
return
str
(
r
)
special
=
dict
(
middle_dot
=
u"
\u00B7
"
,
big_sigma
=
u"
\u03A3
"
)
greek
=
dict
(
alpha
=
u"
\u03B1
"
,
beta
=
u"
\u03B2
"
,
gamma
=
u"
\u03B3
"
,
delta
=
u"
\u03B4
"
,
epsilon
=
u"
\u03B5
"
)
ppow
=
OperatorPrinter
(
'**'
,
0
,
'right'
)
pmul
=
OperatorPrinter
(
'*'
,
-
1
,
'either'
)
pdiv
=
OperatorPrinter
(
'/'
,
-
1
,
'left'
)
padd
=
OperatorPrinter
(
'+'
,
-
2
,
'either'
)
psub
=
OperatorPrinter
(
'-'
,
-
2
,
'left'
)
pdot
=
OperatorPrinter
(
special
[
'middle_dot'
],
-
1
,
'left'
)
psum
=
OperatorPrinter
(
special
[
'big_sigma'
]
+
' '
,
-
2
,
'left'
)
plog
=
FunctionPrinter
(
'log'
)
class
PPrinter
:
def
__init__
(
self
):
...
...
@@ -163,39 +141,41 @@ class PPrinter:
return
cp
special
=
dict
(
middle_dot
=
u"
\u00B7
"
,
big_sigma
=
u"
\u03A3
"
)
from
tensor
import
*
from
elemwise
import
Sum
,
ShuffleRule
x
,
y
,
z
=
matrices
(
'xyz'
)
pp
=
PPrinter
()
pp
.
assign
(
lambda
pstate
,
r
:
True
,
DefaultPrinter
())
pp
.
assign
(
add
,
padd
)
pp
.
assign
(
mul
,
pmul
)
pp
.
assign
(
sub
,
psub
)
pp
.
assign
(
neg
,
psub
)
pp
.
assign
(
div
,
pdiv
)
pp
.
assign
(
pow
,
ppow
)
pp
.
assign
(
dot
,
pdot
)
pp
.
assign
(
Sum
(),
FunctionPrinter
(
'sum'
))
pp
.
assign
(
sgrad
,
FunctionPrinter
(
'd'
))
pp
.
assign
(
lambda
pstate
,
r
:
r
.
owner
and
isinstance
(
r
.
owner
.
op
,
DimShuffle
),
DimShufflePrinter
())
pp
.
assign
(
lambda
pstate
,
r
:
r
.
owner
and
isinstance
(
r
.
owner
.
op
,
ShuffleRule
),
DimShufflePrinter
())
greek
=
dict
(
alpha
=
u"
\u03B1
"
,
beta
=
u"
\u03B2
"
,
gamma
=
u"
\u03B3
"
,
delta
=
u"
\u03B4
"
,
epsilon
=
u"
\u03B5
"
)
# print pp.process(x + y * z
)
# print pp.process((x + y) * z
)
# print pp.process(x * (y * z)
)
# print pp.process(x / (y / z) / x
)
# print pp.process((x ** y) ** z
)
# print pp.process(-x+y
)
# print pp.process(-x*y
)
# print pp.process(sum(x)
)
# print pp.process(sum(x * 10)
)
ppow
=
OperatorPrinter
(
'**'
,
1
,
'right'
)
pneg
=
OperatorPrinter
(
'-'
,
0
,
'either'
)
pmul
=
OperatorPrinter
(
'*'
,
-
1
,
'either'
)
pdiv
=
OperatorPrinter
(
'/'
,
-
1
,
'left'
)
padd
=
OperatorPrinter
(
'+'
,
-
2
,
'either'
)
psub
=
OperatorPrinter
(
'-'
,
-
2
,
'left'
)
pdot
=
OperatorPrinter
(
special
[
'middle_dot'
],
-
1
,
'left'
)
psum
=
OperatorPrinter
(
special
[
'big_sigma'
]
+
' '
,
-
2
,
'left'
)
plog
=
FunctionPrinter
(
'log'
)
# a = Tensor(broadcastable=(False,False,False), dtype='float64')('alpha')
# print a.type
# print pp.process(DimShuffle((False,)*2, [1, 0])(x) + a)
def
make_default_pp
():
pp
=
PPrinter
()
pp
.
assign
(
lambda
pstate
,
r
:
True
,
DefaultPrinter
())
pp
.
assign
(
T
.
add
,
padd
)
pp
.
assign
(
T
.
mul
,
pmul
)
pp
.
assign
(
T
.
sub
,
psub
)
pp
.
assign
(
T
.
neg
,
pneg
)
pp
.
assign
(
T
.
div
,
pdiv
)
pp
.
assign
(
T
.
pow
,
ppow
)
pp
.
assign
(
T
.
dot
,
pdot
)
pp
.
assign
(
T
.
Sum
(),
FunctionPrinter
(
'sum'
))
pp
.
assign
(
T
.
grad
,
FunctionPrinter
(
'd'
))
pp
.
assign
(
lambda
pstate
,
r
:
r
.
owner
and
isinstance
(
r
.
owner
.
op
,
T
.
DimShuffle
),
DimShufflePrinter
())
pp
.
assign
(
lambda
pstate
,
r
:
r
.
owner
and
isinstance
(
r
.
owner
.
op
,
T
.
ShuffleRule
),
DimShufflePrinter
())
return
pp
pp
=
make_default_pp
()
# print pp.process(x / (y * z))
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论