Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f14d4799
提交
f14d4799
authored
9月 13, 2020
作者:
Brandon T. Willard
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Misc. style and format updates
上级
e842fd19
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
10 个修改的文件
包含
101 行增加
和
119 行删除
+101
-119
test_graph.py
tests/gof/test_graph.py
+3
-33
test_scan_utils.py
tests/scan_module/test_scan_utils.py
+5
-4
test_extra_ops.py
tests/tensor/test_extra_ops.py
+0
-0
test_subtensor.py
tests/tensor/test_subtensor.py
+1
-1
__init__.py
theano/__init__.py
+3
-2
ops.py
theano/compile/ops.py
+15
-15
toolbox.py
theano/gof/toolbox.py
+11
-7
scan_opt.py
theano/scan_module/scan_opt.py
+3
-6
extra_ops.py
theano/tensor/extra_ops.py
+36
-36
opt.py
theano/tensor/opt.py
+24
-15
没有找到文件。
tests/gof/test_graph.py
浏览文件 @
f14d4799
from
itertools
import
count
import
pickle
import
pickle
import
pytest
import
pytest
import
numpy
as
np
import
numpy
as
np
from
itertools
import
count
from
theano
import
sparse
,
shared
,
tensor
from
theano
import
sparse
,
shared
,
tensor
from
theano.gof.graph
import
(
from
theano.gof.graph
import
(
Apply
,
Apply
,
...
@@ -58,10 +60,6 @@ class MyOp(Op):
...
@@ -58,10 +60,6 @@ class MyOp(Op):
MyOp
=
MyOp
()
MyOp
=
MyOp
()
##########
# inputs #
##########
class
TestInputs
:
class
TestInputs
:
def
test_inputs
(
self
):
def
test_inputs
(
self
):
...
@@ -77,11 +75,6 @@ class TestInputs:
...
@@ -77,11 +75,6 @@ class TestInputs:
assert
i
==
[
r1
,
r2
,
r5
],
i
assert
i
==
[
r1
,
r2
,
r5
],
i
#############
# as_string #
#############
class
X
:
class
X
:
def
leaf_formatter
(
self
,
leaf
):
def
leaf_formatter
(
self
,
leaf
):
return
str
(
leaf
.
type
)
return
str
(
leaf
.
type
)
...
@@ -126,11 +119,6 @@ class TestStr(X):
...
@@ -126,11 +119,6 @@ class TestStr(X):
assert
self
.
str
(
node2
.
inputs
,
node2
.
outputs
)
==
[
"MyOp(R3, R3)"
]
assert
self
.
str
(
node2
.
inputs
,
node2
.
outputs
)
==
[
"MyOp(R3, R3)"
]
#########
# clone #
#########
class
TestClone
(
X
):
class
TestClone
(
X
):
def
test_accurate
(
self
):
def
test_accurate
(
self
):
r1
,
r2
=
MyVariable
(
1
),
MyVariable
(
2
)
r1
,
r2
=
MyVariable
(
1
),
MyVariable
(
2
)
...
@@ -186,11 +174,6 @@ class TestClone(X):
...
@@ -186,11 +174,6 @@ class TestClone(X):
assert
i
[
0
]
is
c1
and
o
[
0
]
is
c1
assert
i
[
0
]
is
c1
and
o
[
0
]
is
c1
############
# toposort #
############
def
prenode
(
obj
):
def
prenode
(
obj
):
if
isinstance
(
obj
,
Variable
):
if
isinstance
(
obj
,
Variable
):
if
obj
.
owner
:
if
obj
.
owner
:
...
@@ -258,11 +241,6 @@ class TestToposort:
...
@@ -258,11 +241,6 @@ class TestToposort:
assert
all
==
[
o0
]
assert
all
==
[
o0
]
#################
# is_same_graph #
#################
class
TestIsSameGraph
:
class
TestIsSameGraph
:
def
check
(
self
,
expected
,
debug
=
True
):
def
check
(
self
,
expected
,
debug
=
True
):
"""
"""
...
@@ -401,11 +379,6 @@ class TestIsSameGraph:
...
@@ -401,11 +379,6 @@ class TestIsSameGraph:
)
)
################
# eval #
################
class
TestEval
:
class
TestEval
:
def
setup_method
(
self
):
def
setup_method
(
self
):
self
.
x
,
self
.
y
=
tensor
.
scalars
(
"x"
,
"y"
)
self
.
x
,
self
.
y
=
tensor
.
scalars
(
"x"
,
"y"
)
...
@@ -421,9 +394,6 @@ class TestEval:
...
@@ -421,9 +394,6 @@ class TestEval:
),
"temporary functions must not be serialized"
),
"temporary functions must not be serialized"
################
# autoname #
################
class
TestAutoName
:
class
TestAutoName
:
def
test_auto_name
(
self
):
def
test_auto_name
(
self
):
# Get counter value
# Get counter value
...
...
tests/scan_module/test_scan_utils.py
浏览文件 @
f14d4799
import
itertools
import
itertools
import
numpy
as
np
import
pytest
import
pytest
import
numpy
as
np
import
theano
import
theano
from
theano
import
tensor
from
theano
import
tensor
from
theano.scan_module.scan_utils
import
equal_computations
,
map_variables
from
theano.scan_module.scan_utils
import
equal_computations
,
map_variables
from
theano.tensor.type_other
import
NoneConst
from
theano.tensor.type_other
import
NoneConst
...
@@ -17,9 +21,6 @@ def test_equal_compuations():
...
@@ -17,9 +21,6 @@ def test_equal_compuations():
assert
equal_computations
(
max_argmax1
,
max_argmax2
)
assert
equal_computations
(
max_argmax1
,
max_argmax2
)
#################
# map_variables #
#################
class
TestMapVariables
:
class
TestMapVariables
:
...
...
tests/tensor/test_extra_ops.py
浏览文件 @
f14d4799
差异被折叠。
点击展开。
tests/tensor/test_subtensor.py
浏览文件 @
f14d4799
...
@@ -53,7 +53,7 @@ from tests.tensor.test_basic import inplace_func, rand, randint_ranged
...
@@ -53,7 +53,7 @@ from tests.tensor.test_basic import inplace_func, rand, randint_ranged
class
TestSubtensor
(
utt
.
OptimizationTestMixin
):
class
TestSubtensor
(
utt
.
OptimizationTestMixin
):
"""
"""
This is
build in a way that allow to reuse it to test the equivalent gpu op
.
This is
designed to be sub-classed (e.g. by the GPU tests)
.
"""
"""
def
setup_method
(
self
):
def
setup_method
(
self
):
...
...
theano/__init__.py
浏览文件 @
f14d4799
...
@@ -197,7 +197,7 @@ def dot(l, r):
...
@@ -197,7 +197,7 @@ def dot(l, r):
def
get_scalar_constant_value
(
v
):
def
get_scalar_constant_value
(
v
):
"""
return the constant scalar(0-D) value underlying variable `v`
"""
Return the constant scalar (i.e. 0-D) value underlying variable `v`.
If v is the output of dimshuffles, fills, allocs, rebroadcasts, cast
If v is the output of dimshuffles, fills, allocs, rebroadcasts, cast
this function digs through them.
this function digs through them.
...
@@ -208,7 +208,8 @@ def get_scalar_constant_value(v):
...
@@ -208,7 +208,8 @@ def get_scalar_constant_value(v):
tensor.basic.NotScalarConstantError.
tensor.basic.NotScalarConstantError.
"""
"""
# Is it necessary to test for presence of theano.sparse at runtime?
# Is it necessary to test for presence of theano.sparse at runtime?
if
"sparse"
in
globals
()
and
isinstance
(
v
.
type
,
sparse
.
SparseType
):
sparse
=
globals
()
.
get
(
"sparse"
)
if
sparse
and
isinstance
(
v
.
type
,
sparse
.
SparseType
):
if
v
.
owner
is
not
None
and
isinstance
(
v
.
owner
.
op
,
sparse
.
CSM
):
if
v
.
owner
is
not
None
and
isinstance
(
v
.
owner
.
op
,
sparse
.
CSM
):
data
=
v
.
owner
.
inputs
[
0
]
data
=
v
.
owner
.
inputs
[
0
]
return
tensor
.
get_scalar_constant_value
(
data
)
return
tensor
.
get_scalar_constant_value
(
data
)
...
...
theano/compile/ops.py
浏览文件 @
f14d4799
...
@@ -18,7 +18,7 @@ from collections import OrderedDict
...
@@ -18,7 +18,7 @@ from collections import OrderedDict
from
six
import
integer_types
from
six
import
integer_types
from
theano
import
gof
from
theano
.gof
import
Op
,
Apply
,
ParamsType
,
Variable
def
register_view_op_c_code
(
type
,
code
,
version
=
()):
def
register_view_op_c_code
(
type
,
code
,
version
=
()):
...
@@ -39,7 +39,7 @@ def register_view_op_c_code(type, code, version=()):
...
@@ -39,7 +39,7 @@ def register_view_op_c_code(type, code, version=()):
ViewOp
.
c_code_and_version
[
type
]
=
(
code
,
version
)
ViewOp
.
c_code_and_version
[
type
]
=
(
code
,
version
)
class
ViewOp
(
gof
.
Op
):
class
ViewOp
(
Op
):
"""
"""
Returns an inplace view of the input. Used internally by Theano.
Returns an inplace view of the input. Used internally by Theano.
...
@@ -54,7 +54,7 @@ class ViewOp(gof.Op):
...
@@ -54,7 +54,7 @@ class ViewOp(gof.Op):
_f16_ok
=
True
_f16_ok
=
True
def
make_node
(
self
,
x
):
def
make_node
(
self
,
x
):
return
gof
.
Apply
(
self
,
[
x
],
[
x
.
type
()])
return
Apply
(
self
,
[
x
],
[
x
.
type
()])
def
perform
(
self
,
node
,
inp
,
out
):
def
perform
(
self
,
node
,
inp
,
out
):
(
x
,)
=
inp
(
x
,)
=
inp
...
@@ -151,7 +151,7 @@ def register_deep_copy_op_c_code(typ, code, version=()):
...
@@ -151,7 +151,7 @@ def register_deep_copy_op_c_code(typ, code, version=()):
DeepCopyOp
.
c_code_and_version
[
typ
]
=
(
code
,
version
)
DeepCopyOp
.
c_code_and_version
[
typ
]
=
(
code
,
version
)
class
DeepCopyOp
(
gof
.
Op
):
class
DeepCopyOp
(
Op
):
# Mapping from Type to C code (and version) to use.
# Mapping from Type to C code (and version) to use.
# In the C code, the name of the input variable is %(iname)s,
# In the C code, the name of the input variable is %(iname)s,
# the output variable is %(oname)s.
# the output variable is %(oname)s.
...
@@ -165,7 +165,7 @@ class DeepCopyOp(gof.Op):
...
@@ -165,7 +165,7 @@ class DeepCopyOp(gof.Op):
pass
pass
def
make_node
(
self
,
x
):
def
make_node
(
self
,
x
):
return
gof
.
Apply
(
self
,
[
x
],
[
x
.
type
()])
return
Apply
(
self
,
[
x
],
[
x
.
type
()])
def
perform
(
self
,
node
,
args
,
outs
):
def
perform
(
self
,
node
,
args
,
outs
):
if
hasattr
(
args
[
0
],
"copy"
):
if
hasattr
(
args
[
0
],
"copy"
):
...
@@ -235,7 +235,7 @@ def register_shape_c_code(type, code, version=()):
...
@@ -235,7 +235,7 @@ def register_shape_c_code(type, code, version=()):
Shape
.
c_code_and_version
[
type
]
=
(
code
,
version
)
Shape
.
c_code_and_version
[
type
]
=
(
code
,
version
)
class
Shape
(
gof
.
Op
):
class
Shape
(
Op
):
"""
"""
L{Op} to return the shape of a matrix.
L{Op} to return the shape of a matrix.
...
@@ -260,7 +260,7 @@ class Shape(gof.Op):
...
@@ -260,7 +260,7 @@ class Shape(gof.Op):
# This will fail at execution time.
# This will fail at execution time.
if
not
isinstance
(
x
,
theano
.
Variable
):
if
not
isinstance
(
x
,
theano
.
Variable
):
x
=
theano
.
tensor
.
as_tensor_variable
(
x
)
x
=
theano
.
tensor
.
as_tensor_variable
(
x
)
return
gof
.
Apply
(
self
,
[
x
],
[
theano
.
tensor
.
lvector
()])
return
Apply
(
self
,
[
x
],
[
theano
.
tensor
.
lvector
()])
def
perform
(
self
,
node
,
inp
,
out_
):
def
perform
(
self
,
node
,
inp
,
out_
):
(
x
,)
=
inp
(
x
,)
=
inp
...
@@ -329,7 +329,7 @@ shape = Shape()
...
@@ -329,7 +329,7 @@ shape = Shape()
_shape
=
shape
# was used in the past, now use shape directly.
_shape
=
shape
# was used in the past, now use shape directly.
class
Shape_i
(
gof
.
Op
):
class
Shape_i
(
Op
):
"""
"""
L{Op} to return the shape of a matrix.
L{Op} to return the shape of a matrix.
...
@@ -369,7 +369,7 @@ class Shape_i(gof.Op):
...
@@ -369,7 +369,7 @@ class Shape_i(gof.Op):
# using params.
# using params.
@property
@property
def
params_type
(
self
):
def
params_type
(
self
):
return
gof
.
ParamsType
(
i
=
theano
.
scalar
.
basic
.
int64
)
return
ParamsType
(
i
=
theano
.
scalar
.
basic
.
int64
)
def
__str__
(
self
):
def
__str__
(
self
):
return
"
%
s{
%
i}"
%
(
self
.
__class__
.
__name__
,
self
.
i
)
return
"
%
s{
%
i}"
%
(
self
.
__class__
.
__name__
,
self
.
i
)
...
@@ -540,7 +540,7 @@ def load_back(mod, name):
...
@@ -540,7 +540,7 @@ def load_back(mod, name):
return
obj
return
obj
class
FromFunctionOp
(
gof
.
Op
):
class
FromFunctionOp
(
Op
):
"""
"""
Build a basic Theano Op around a function.
Build a basic Theano Op around a function.
...
@@ -666,7 +666,7 @@ def register_rebroadcast_c_code(typ, code, version=()):
...
@@ -666,7 +666,7 @@ def register_rebroadcast_c_code(typ, code, version=()):
Rebroadcast
.
c_code_and_version
[
typ
]
=
(
code
,
version
)
Rebroadcast
.
c_code_and_version
[
typ
]
=
(
code
,
version
)
class
Rebroadcast
(
gof
.
Op
):
class
Rebroadcast
(
Op
):
"""
"""
Change the input's broadcastable fields in some predetermined way.
Change the input's broadcastable fields in some predetermined way.
...
@@ -737,7 +737,7 @@ class Rebroadcast(gof.Op):
...
@@ -737,7 +737,7 @@ class Rebroadcast(gof.Op):
self
.
axis
.
get
(
i
,
b
)
for
i
,
b
in
enumerate
(
x
.
type
.
broadcastable
)
self
.
axis
.
get
(
i
,
b
)
for
i
,
b
in
enumerate
(
x
.
type
.
broadcastable
)
]
]
)
)
return
gof
.
Apply
(
self
,
[
x
],
[
t
()])
return
Apply
(
self
,
[
x
],
[
t
()])
def
perform
(
self
,
node
,
inp
,
out_
):
def
perform
(
self
,
node
,
inp
,
out_
):
(
x
,)
=
inp
(
x
,)
=
inp
...
@@ -848,7 +848,7 @@ def register_specify_shape_c_code(typ, code, version=(), c_support_code_apply=No
...
@@ -848,7 +848,7 @@ def register_specify_shape_c_code(typ, code, version=(), c_support_code_apply=No
SpecifyShape
.
c_code_and_version
[
typ
]
=
(
code
,
version
,
c_support_code_apply
)
SpecifyShape
.
c_code_and_version
[
typ
]
=
(
code
,
version
,
c_support_code_apply
)
class
SpecifyShape
(
gof
.
Op
):
class
SpecifyShape
(
Op
):
"""
"""
L{Op} that puts into the graph the user-provided shape.
L{Op} that puts into the graph the user-provided shape.
...
@@ -876,14 +876,14 @@ class SpecifyShape(gof.Op):
...
@@ -876,14 +876,14 @@ class SpecifyShape(gof.Op):
_f16_ok
=
True
_f16_ok
=
True
def
make_node
(
self
,
x
,
shape
):
def
make_node
(
self
,
x
,
shape
):
if
not
isinstance
(
x
,
gof
.
Variable
):
if
not
isinstance
(
x
,
Variable
):
x
=
theano
.
tensor
.
as_tensor_variable
(
x
)
x
=
theano
.
tensor
.
as_tensor_variable
(
x
)
shape
=
theano
.
tensor
.
as_tensor_variable
(
shape
)
shape
=
theano
.
tensor
.
as_tensor_variable
(
shape
)
assert
shape
.
ndim
==
1
assert
shape
.
ndim
==
1
assert
shape
.
dtype
in
theano
.
tensor
.
integer_dtypes
assert
shape
.
dtype
in
theano
.
tensor
.
integer_dtypes
if
isinstance
(
shape
,
theano
.
tensor
.
TensorConstant
):
if
isinstance
(
shape
,
theano
.
tensor
.
TensorConstant
):
assert
shape
.
data
.
size
==
x
.
ndim
assert
shape
.
data
.
size
==
x
.
ndim
return
gof
.
Apply
(
self
,
[
x
,
shape
],
[
x
.
type
()])
return
Apply
(
self
,
[
x
,
shape
],
[
x
.
type
()])
def
perform
(
self
,
node
,
inp
,
out_
):
def
perform
(
self
,
node
,
inp
,
out_
):
x
,
shape
=
inp
x
,
shape
=
inp
...
...
theano/gof/toolbox.py
浏览文件 @
f14d4799
from
functools
import
partial
from
collections
import
OrderedDict
import
sys
import
sys
import
copy
import
time
import
time
import
inspect
import
inspect
import
numpy
as
np
import
numpy
as
np
import
theano
from
functools
import
partial
from
collections
import
OrderedDict
from
six.moves
import
StringIO
from
six.moves
import
StringIO
import
theano
from
theano
import
config
from
theano
import
config
from
theano.gof
import
graph
from
theano.gof.graph
import
(
io_toposort
,
)
class
AlreadyThere
(
Exception
):
class
AlreadyThere
(
Exception
):
...
@@ -312,7 +316,7 @@ class Bookkeeper(Feature):
...
@@ -312,7 +316,7 @@ class Bookkeeper(Feature):
FunctionGraph is initially populated, this is where you should
FunctionGraph is initially populated, this is where you should
run checks on the initial contents of the FunctionGraph.
run checks on the initial contents of the FunctionGraph.
"""
"""
for
node
in
graph
.
io_toposort
(
fgraph
.
inputs
,
fgraph
.
outputs
):
for
node
in
io_toposort
(
fgraph
.
inputs
,
fgraph
.
outputs
):
self
.
on_import
(
fgraph
,
node
,
"on_attach"
)
self
.
on_import
(
fgraph
,
node
,
"on_attach"
)
def
on_detach
(
self
,
fgraph
):
def
on_detach
(
self
,
fgraph
):
...
@@ -320,7 +324,7 @@ class Bookkeeper(Feature):
...
@@ -320,7 +324,7 @@ class Bookkeeper(Feature):
Should remove any dynamically added functionality
Should remove any dynamically added functionality
that it installed into the function_graph
that it installed into the function_graph
"""
"""
for
node
in
graph
.
io_toposort
(
fgraph
.
inputs
,
fgraph
.
outputs
):
for
node
in
io_toposort
(
fgraph
.
inputs
,
fgraph
.
outputs
):
self
.
on_prune
(
fgraph
,
node
,
"Bookkeeper.detach"
)
self
.
on_prune
(
fgraph
,
node
,
"Bookkeeper.detach"
)
...
...
theano/scan_module/scan_opt.py
浏览文件 @
f14d4799
...
@@ -62,19 +62,16 @@ from collections import OrderedDict
...
@@ -62,19 +62,16 @@ from collections import OrderedDict
from
six
import
integer_types
from
six
import
integer_types
from
theano
import
tensor
,
scalar
from
theano
import
gof
,
tensor
,
scalar
from
theano.tensor
import
opt
,
get_scalar_constant_value
,
Alloc
,
AllocEmpty
from
theano.tensor
import
opt
,
get_scalar_constant_value
,
Alloc
,
AllocEmpty
from
theano
import
gof
from
theano.compile
import
optdb
from
theano.compile
import
optdb
from
theano.compile.function_module
import
deep_copy_op
from
theano.compile.function_module
import
deep_copy_op
from
theano.gof
import
toolbox
,
DestroyHandler
,
InconsistencyError
from
theano.gof
import
toolbox
,
DestroyHandler
,
InconsistencyError
from
theano.gof.opt
import
Optimizer
from
theano.gof.opt
import
Optimizer
,
pre_constant_merge
,
pre_greedy_local_optimizer
from
theano.gof.opt
import
pre_constant_merge
,
pre_greedy_local_optimizer
from
theano.scan_module
import
scan_op
from
theano.scan_module
import
scan_utils
from
theano.scan_module.scan_utils
import
equal_computations
,
scan_args
from
theano.scan_module.scan_utils
import
equal_computations
,
scan_args
from
theano.scan_module
import
scan_op
,
scan_utils
__docformat__
=
"restructedtext en"
__docformat__
=
"restructedtext en"
__authors__
=
(
__authors__
=
(
...
...
theano/tensor/extra_ops.py
浏览文件 @
f14d4799
...
@@ -4,15 +4,15 @@ import theano
...
@@ -4,15 +4,15 @@ import theano
from
collections.abc
import
Collection
from
collections.abc
import
Collection
from
theano.
tensor
import
basic
from
theano.
gof
import
Op
,
Apply
,
Generic
,
ParamsType
,
EnumList
from
theano.tensor
import
nlinalg
# noqa
from
theano.tensor
import
basic
,
nlinalg
from
theano
import
gof
,
scalar
from
theano
.scalar
import
int32
as
int_t
,
upcast
from
theano.g
of
import
Generic
,
ParamsType
,
EnumList
from
theano.g
radient
import
(
from
theano
import
gradient
DisconnectedType
,
from
theano.gradient
import
DisconnectedType
,
disconnected_type
disconnected_type
,
from
theano.scalar
import
int32
as
int_t
_float_zeros_like
,
grad_undefined
,
tensor
=
basic
)
class
CpuContiguous
(
theano
.
Op
):
class
CpuContiguous
(
theano
.
Op
):
...
@@ -197,8 +197,8 @@ class SearchsortedOp(theano.Op):
...
@@ -197,8 +197,8 @@ class SearchsortedOp(theano.Op):
else
:
else
:
x
,
v
=
inputs
x
,
v
=
inputs
x_grad
=
gradient
.
_float_zeros_like
(
x
)
x_grad
=
_float_zeros_like
(
x
)
v_grad
=
gradient
.
_float_zeros_like
(
v
)
v_grad
=
_float_zeros_like
(
v
)
if
num_ins
==
3
:
if
num_ins
==
3
:
return
[
x_grad
,
v_grad
,
disconnected_type
()]
return
[
x_grad
,
v_grad
,
disconnected_type
()]
else
:
else
:
...
@@ -332,7 +332,7 @@ class CumOp(theano.Op):
...
@@ -332,7 +332,7 @@ class CumOp(theano.Op):
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
if
self
.
axis
is
None
:
if
self
.
axis
is
None
:
return
[(
tensor
.
prod
(
shapes
[
0
]),)]
# Flatten
return
[(
basic
.
prod
(
shapes
[
0
]),)]
# Flatten
return
shapes
return
shapes
...
@@ -658,7 +658,7 @@ class RepeatOp(theano.Op):
...
@@ -658,7 +658,7 @@ class RepeatOp(theano.Op):
x
=
basic
.
as_tensor_variable
(
x
)
x
=
basic
.
as_tensor_variable
(
x
)
repeats
=
basic
.
as_tensor_variable
(
repeats
)
repeats
=
basic
.
as_tensor_variable
(
repeats
)
if
repeats
.
dtype
not
in
tensor
.
integer_dtypes
:
if
repeats
.
dtype
not
in
basic
.
integer_dtypes
:
raise
TypeError
(
"repeats.dtype must be an integer."
)
raise
TypeError
(
"repeats.dtype must be an integer."
)
# Some dtypes are not supported by numpy's implementation of repeat.
# Some dtypes are not supported by numpy's implementation of repeat.
...
@@ -784,7 +784,7 @@ def repeat(x, repeats, axis=None):
...
@@ -784,7 +784,7 @@ def repeat(x, repeats, axis=None):
.. versionadded:: 0.6
.. versionadded:: 0.6
"""
"""
repeats
=
tensor
.
as_tensor_variable
(
repeats
)
repeats
=
basic
.
as_tensor_variable
(
repeats
)
if
repeats
.
ndim
>
1
:
if
repeats
.
ndim
>
1
:
raise
ValueError
(
"The dimension of repeats should not exceed 1."
)
raise
ValueError
(
"The dimension of repeats should not exceed 1."
)
...
@@ -827,22 +827,22 @@ def repeat(x, repeats, axis=None):
...
@@ -827,22 +827,22 @@ def repeat(x, repeats, axis=None):
# After the original tensor is duplicated along the additional
# After the original tensor is duplicated along the additional
# dimension, we reshape it to the expected output shape, and
# dimension, we reshape it to the expected output shape, and
# return the output z.
# return the output z.
z
=
tensor
.
alloc
(
x
.
dimshuffle
(
*
dims_
),
*
shape_
)
.
reshape
(
shape
)
z
=
basic
.
alloc
(
x
.
dimshuffle
(
*
dims_
),
*
shape_
)
.
reshape
(
shape
)
return
z
return
z
class
Bartlett
(
gof
.
Op
):
class
Bartlett
(
Op
):
# See function bartlett for docstring
# See function bartlett for docstring
__props__
=
()
__props__
=
()
def
make_node
(
self
,
M
):
def
make_node
(
self
,
M
):
M
=
tensor
.
as_tensor_variable
(
M
)
M
=
basic
.
as_tensor_variable
(
M
)
if
M
.
ndim
!=
0
:
if
M
.
ndim
!=
0
:
raise
TypeError
(
"
%
s only works on scalar input"
%
self
.
__class__
.
__name__
)
raise
TypeError
(
"
%
s only works on scalar input"
%
self
.
__class__
.
__name__
)
elif
M
.
dtype
not
in
theano
.
tensor
.
integer_dtypes
:
elif
M
.
dtype
not
in
theano
.
tensor
.
integer_dtypes
:
# dtype is a theano attribute here
# dtype is a theano attribute here
raise
TypeError
(
"
%
s only works on integer input"
%
self
.
__class__
.
__name__
)
raise
TypeError
(
"
%
s only works on integer input"
%
self
.
__class__
.
__name__
)
return
gof
.
Apply
(
self
,
[
M
],
[
tensor
.
dvector
()])
return
Apply
(
self
,
[
M
],
[
basic
.
dvector
()])
def
perform
(
self
,
node
,
inputs
,
out_
):
def
perform
(
self
,
node
,
inputs
,
out_
):
M
=
inputs
[
0
]
M
=
inputs
[
0
]
...
@@ -851,7 +851,7 @@ class Bartlett(gof.Op):
...
@@ -851,7 +851,7 @@ class Bartlett(gof.Op):
def
infer_shape
(
self
,
node
,
in_shapes
):
def
infer_shape
(
self
,
node
,
in_shapes
):
temp
=
node
.
inputs
[
0
]
temp
=
node
.
inputs
[
0
]
M
=
tensor
.
switch
(
tensor
.
lt
(
temp
,
0
),
tensor
.
cast
(
0
,
temp
.
dtype
),
temp
)
M
=
basic
.
switch
(
basic
.
lt
(
temp
,
0
),
basic
.
cast
(
0
,
temp
.
dtype
),
temp
)
return
[[
M
]]
return
[[
M
]]
def
grad
(
self
,
inputs
,
output_grads
):
def
grad
(
self
,
inputs
,
output_grads
):
...
@@ -889,7 +889,7 @@ def bartlett(M):
...
@@ -889,7 +889,7 @@ def bartlett(M):
return
bartlett_
(
M
)
return
bartlett_
(
M
)
class
FillDiagonal
(
gof
.
Op
):
class
FillDiagonal
(
Op
):
# See function fill_diagonal for docstring
# See function fill_diagonal for docstring
__props__
=
()
__props__
=
()
...
@@ -897,8 +897,8 @@ class FillDiagonal(gof.Op):
...
@@ -897,8 +897,8 @@ class FillDiagonal(gof.Op):
return
[
in_shapes
[
0
]]
return
[
in_shapes
[
0
]]
def
make_node
(
self
,
a
,
val
):
def
make_node
(
self
,
a
,
val
):
a
=
tensor
.
as_tensor_variable
(
a
)
a
=
basic
.
as_tensor_variable
(
a
)
val
=
tensor
.
as_tensor_variable
(
val
)
val
=
basic
.
as_tensor_variable
(
val
)
if
a
.
ndim
<
2
:
if
a
.
ndim
<
2
:
raise
TypeError
(
raise
TypeError
(
"
%
s: first parameter must have at least"
"
%
s: first parameter must have at least"
...
@@ -908,13 +908,13 @@ class FillDiagonal(gof.Op):
...
@@ -908,13 +908,13 @@ class FillDiagonal(gof.Op):
raise
TypeError
(
raise
TypeError
(
"
%
s: second parameter must be a scalar"
%
self
.
__class__
.
__name__
"
%
s: second parameter must be a scalar"
%
self
.
__class__
.
__name__
)
)
val
=
tensor
.
cast
(
val
,
dtype
=
scalar
.
upcast
(
a
.
dtype
,
val
.
dtype
))
val
=
basic
.
cast
(
val
,
dtype
=
upcast
(
a
.
dtype
,
val
.
dtype
))
if
val
.
dtype
!=
a
.
dtype
:
if
val
.
dtype
!=
a
.
dtype
:
raise
TypeError
(
raise
TypeError
(
"
%
s: type of second parameter must be the same as"
"
%
s: type of second parameter must be the same as"
" the first's"
%
self
.
__class__
.
__name__
" the first's"
%
self
.
__class__
.
__name__
)
)
return
gof
.
Apply
(
self
,
[
a
,
val
],
[
a
.
type
()])
return
Apply
(
self
,
[
a
,
val
],
[
a
.
type
()])
def
perform
(
self
,
node
,
inputs
,
output_storage
):
def
perform
(
self
,
node
,
inputs
,
output_storage
):
a
=
inputs
[
0
]
.
copy
()
a
=
inputs
[
0
]
.
copy
()
...
@@ -950,7 +950,7 @@ class FillDiagonal(gof.Op):
...
@@ -950,7 +950,7 @@ class FillDiagonal(gof.Op):
)
)
wr_a
=
fill_diagonal
(
grad
,
0
)
# valid for any number of dimensions
wr_a
=
fill_diagonal
(
grad
,
0
)
# valid for any number of dimensions
# diag is only valid for matrices
# diag is only valid for matrices
wr_val
=
theano
.
tensor
.
nlinalg
.
diag
(
grad
)
.
sum
()
wr_val
=
nlinalg
.
diag
(
grad
)
.
sum
()
return
[
wr_a
,
wr_val
]
return
[
wr_a
,
wr_val
]
...
@@ -991,7 +991,7 @@ def fill_diagonal(a, val):
...
@@ -991,7 +991,7 @@ def fill_diagonal(a, val):
return
fill_diagonal_
(
a
,
val
)
return
fill_diagonal_
(
a
,
val
)
class
FillDiagonalOffset
(
gof
.
Op
):
class
FillDiagonalOffset
(
Op
):
# See function fill_diagonal_offset for docstring
# See function fill_diagonal_offset for docstring
__props__
=
()
__props__
=
()
...
@@ -999,9 +999,9 @@ class FillDiagonalOffset(gof.Op):
...
@@ -999,9 +999,9 @@ class FillDiagonalOffset(gof.Op):
return
[
in_shapes
[
0
]]
return
[
in_shapes
[
0
]]
def
make_node
(
self
,
a
,
val
,
offset
):
def
make_node
(
self
,
a
,
val
,
offset
):
a
=
tensor
.
as_tensor_variable
(
a
)
a
=
basic
.
as_tensor_variable
(
a
)
val
=
tensor
.
as_tensor_variable
(
val
)
val
=
basic
.
as_tensor_variable
(
val
)
offset
=
tensor
.
as_tensor_variable
(
offset
)
offset
=
basic
.
as_tensor_variable
(
offset
)
if
a
.
ndim
!=
2
:
if
a
.
ndim
!=
2
:
raise
TypeError
(
raise
TypeError
(
"
%
s: first parameter must have exactly"
"
%
s: first parameter must have exactly"
...
@@ -1015,7 +1015,7 @@ class FillDiagonalOffset(gof.Op):
...
@@ -1015,7 +1015,7 @@ class FillDiagonalOffset(gof.Op):
raise
TypeError
(
raise
TypeError
(
"
%
s: third parameter must be a scalar"
%
self
.
__class__
.
__name__
"
%
s: third parameter must be a scalar"
%
self
.
__class__
.
__name__
)
)
val
=
tensor
.
cast
(
val
,
dtype
=
scalar
.
upcast
(
a
.
dtype
,
val
.
dtype
))
val
=
basic
.
cast
(
val
,
dtype
=
upcast
(
a
.
dtype
,
val
.
dtype
))
if
val
.
dtype
!=
a
.
dtype
:
if
val
.
dtype
!=
a
.
dtype
:
raise
TypeError
(
raise
TypeError
(
"
%
s: type of second parameter must be the same"
"
%
s: type of second parameter must be the same"
...
@@ -1028,7 +1028,7 @@ class FillDiagonalOffset(gof.Op):
...
@@ -1028,7 +1028,7 @@ class FillDiagonalOffset(gof.Op):
%
self
.
__class__
.
__name__
%
self
.
__class__
.
__name__
)
)
return
gof
.
Apply
(
self
,
[
a
,
val
,
offset
],
[
a
.
type
()])
return
Apply
(
self
,
[
a
,
val
,
offset
],
[
a
.
type
()])
def
perform
(
self
,
node
,
inputs
,
output_storage
):
def
perform
(
self
,
node
,
inputs
,
output_storage
):
a
=
inputs
[
0
]
.
copy
()
a
=
inputs
[
0
]
.
copy
()
...
@@ -1097,7 +1097,7 @@ class FillDiagonalOffset(gof.Op):
...
@@ -1097,7 +1097,7 @@ class FillDiagonalOffset(gof.Op):
wr_val
=
grad
.
flatten
()[
start
:
end
:
step
]
.
sum
()
wr_val
=
grad
.
flatten
()[
start
:
end
:
step
]
.
sum
()
wr_offset
=
theano
.
gradient
.
grad_undefined
(
wr_offset
=
grad_undefined
(
self
,
self
,
2
,
2
,
offset
,
offset
,
...
@@ -1291,7 +1291,7 @@ class Unique(theano.Op):
...
@@ -1291,7 +1291,7 @@ class Unique(theano.Op):
self
.
axis
=
None
self
.
axis
=
None
class
UnravelIndex
(
gof
.
Op
):
class
UnravelIndex
(
Op
):
__props__
=
(
"order"
,)
__props__
=
(
"order"
,)
def
__init__
(
self
,
order
=
"C"
):
def
__init__
(
self
,
order
=
"C"
):
...
@@ -1313,7 +1313,7 @@ class UnravelIndex(gof.Op):
...
@@ -1313,7 +1313,7 @@ class UnravelIndex(gof.Op):
if
dims
.
ndim
!=
1
:
if
dims
.
ndim
!=
1
:
raise
TypeError
(
"dims must be a 1D array"
)
raise
TypeError
(
"dims must be a 1D array"
)
return
gof
.
Apply
(
return
Apply
(
self
,
self
,
[
indices
,
dims
],
[
indices
,
dims
],
[
[
...
@@ -1372,7 +1372,7 @@ def unravel_index(indices, dims, order="C"):
...
@@ -1372,7 +1372,7 @@ def unravel_index(indices, dims, order="C"):
return
tuple
(
res
)
return
tuple
(
res
)
class
RavelMultiIndex
(
gof
.
Op
):
class
RavelMultiIndex
(
Op
):
__props__
=
(
"mode"
,
"order"
)
__props__
=
(
"mode"
,
"order"
)
def
__init__
(
self
,
mode
=
"raise"
,
order
=
"C"
):
def
__init__
(
self
,
mode
=
"raise"
,
order
=
"C"
):
...
@@ -1397,7 +1397,7 @@ class RavelMultiIndex(gof.Op):
...
@@ -1397,7 +1397,7 @@ class RavelMultiIndex(gof.Op):
if
dims
.
ndim
!=
1
:
if
dims
.
ndim
!=
1
:
raise
TypeError
(
"dims must be a 1D array"
)
raise
TypeError
(
"dims must be a 1D array"
)
return
gof
.
Apply
(
return
Apply
(
self
,
self
,
multi_index
+
[
dims
],
multi_index
+
[
dims
],
[
[
...
...
theano/tensor/opt.py
浏览文件 @
f14d4799
...
@@ -18,16 +18,32 @@ import theano
...
@@ -18,16 +18,32 @@ import theano
from
functools
import
reduce
from
functools
import
reduce
from
collections
import
defaultdict
from
collections
import
defaultdict
from
six
import
integer_types
from
six
import
integer_types
,
StringIO
from
theano
import
gof
from
theano
import
(
gof
,
from
theano.gof
import
opt
,
InconsistencyError
,
TopoOptimizer
,
graph
config
,
from
theano.gof
import
Variable
,
Constant
scalar
,
from
theano.gof.opt
import
copy_stack_trace
,
in2out
compile
,
)
# to register the optimizer built by this file
from
theano.gof
import
(
opt
,
InconsistencyError
,
TopoOptimizer
,
graph
,
Variable
,
Constant
,
toolbox
,
)
from
theano.gof.opt
import
(
copy_stack_trace
,
in2out
,
Optimizer
,
pre_constant_merge
,
pre_greedy_local_optimizer
,
)
from
theano.gof.utils
import
MethodNotDefined
from
theano.gof.utils
import
MethodNotDefined
from
theano.gradient
import
DisconnectedType
from
theano.gradient
import
DisconnectedType
from
theano
import
config
from
theano.tensor.elemwise
import
Elemwise
,
DimShuffle
from
theano.tensor.elemwise
import
Elemwise
,
DimShuffle
from
theano.tensor.subtensor
import
(
from
theano.tensor.subtensor
import
(
get_idx_list
,
get_idx_list
,
...
@@ -43,10 +59,8 @@ from theano.tensor.subtensor import (
...
@@ -43,10 +59,8 @@ from theano.tensor.subtensor import (
advanced_inc_subtensor1
,
advanced_inc_subtensor1
,
)
)
from
theano.tensor.sort
import
TopKOp
from
theano.tensor.sort
import
TopKOp
from
theano
import
scalar
from
theano.scalar
import
basic
from
theano.scalar
import
basic
from
theano.tensor
import
basic
as
T
from
theano.tensor
import
basic
as
T
from
theano
import
compile
# to register the optimizer built by this file
from
theano.compile.ops
import
Shape
,
Shape_i
from
theano.compile.ops
import
Shape
,
Shape_i
from
theano.tensor.type
import
(
from
theano.tensor.type
import
(
values_eq_approx_remove_inf
,
values_eq_approx_remove_inf
,
...
@@ -54,8 +68,6 @@ from theano.tensor.type import (
...
@@ -54,8 +68,6 @@ from theano.tensor.type import (
values_eq_approx_remove_inf_nan
,
values_eq_approx_remove_inf_nan
,
)
)
from
theano.gof.opt
import
Optimizer
,
pre_constant_merge
,
pre_greedy_local_optimizer
from
theano.gof
import
toolbox
from
theano.tensor.basic
import
(
from
theano.tensor.basic
import
(
Alloc
,
Alloc
,
get_scalar_constant_value
,
get_scalar_constant_value
,
...
@@ -64,12 +76,9 @@ from theano.tensor.basic import (
...
@@ -64,12 +76,9 @@ from theano.tensor.basic import (
NotScalarConstantError
,
NotScalarConstantError
,
Reshape
,
Reshape
,
)
)
from
six
import
StringIO
_logger
=
logging
.
getLogger
(
"theano.tensor.opt"
)
_logger
=
logging
.
getLogger
(
"theano.tensor.opt"
)
# Utilities
def
_fill_chain
(
new_out
,
orig_inputs
):
def
_fill_chain
(
new_out
,
orig_inputs
):
for
i
in
orig_inputs
:
for
i
in
orig_inputs
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论