Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f76c3213
提交
f76c3213
authored
1月 12, 2010
作者:
Pierre-Antoine Manzagol
浏览文件
操作
浏览文件
下载
差异文件
Merge.
上级
fca1efac
ea551ee8
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
67 行增加
和
12 行删除
+67
-12
basic.py
theano/tensor/basic.py
+19
-12
test_incsubtensor.py
theano/tensor/tests/test_incsubtensor.py
+48
-0
没有找到文件。
theano/tensor/basic.py
浏览文件 @
f76c3213
...
...
@@ -1896,9 +1896,9 @@ class Subtensor(Op):
return
entry
.
type
elif
isinstance
(
entry
,
gof
.
Type
)
and
entry
in
scal_types
:
return
entry
if
isinstance
(
entry
,
gof
.
Variable
)
and
entry
.
type
in
tensor_types
:
if
isinstance
(
entry
,
gof
.
Variable
)
and
entry
.
type
in
tensor_types
and
numpy
.
all
(
entry
.
type
.
broadcastable
)
:
return
scal
.
Scalar
(
entry
.
type
.
dtype
)
elif
isinstance
(
entry
,
gof
.
Type
)
and
entry
in
tensor_types
:
elif
isinstance
(
entry
,
gof
.
Type
)
and
entry
in
tensor_types
and
numpy
.
all
(
entry
.
broadcastable
)
:
return
scal
.
Scalar
(
entry
.
dtype
)
elif
slice_ok
and
isinstance
(
entry
,
slice
):
a
=
entry
.
start
...
...
@@ -1934,14 +1934,20 @@ class Subtensor(Op):
def
__init__
(
self
,
idx_list
):
self
.
idx_list
=
map
(
self
.
convert
,
idx_list
)
@staticmethod
def
my_as_scalar
(
a
):
# Since scal.as_scalar does not know about tensor types (it would
# create a circular import) , this method converts either a
# TensorVariable or a ScalarVariable to a scalar.
if
isinstance
(
a
,
gof
.
Variable
)
and
isinstance
(
a
.
type
,
TensorType
):
return
scalar_from_tensor
(
a
)
else
:
return
scal
.
as_scalar
(
a
)
def
make_node
(
self
,
x
,
*
inputs
):
x
=
as_tensor_variable
(
x
)
def
my_as_scalar
(
a
):
if
isinstance
(
a
,
gof
.
Variable
)
and
isinstance
(
a
.
type
,
TensorType
):
return
scalar_from_tensor
(
a
)
else
:
return
scal
.
as_scalar
(
a
)
inputs
=
tuple
(
my_as_scalar
(
a
)
for
a
in
inputs
)
inputs
=
tuple
(
self
.
my_as_scalar
(
a
)
for
a
in
inputs
)
idx_list
=
list
(
self
.
idx_list
)
if
len
(
idx_list
)
>
x
.
type
.
ndim
:
...
...
@@ -2074,7 +2080,8 @@ pprint.assign(lambda pstate, r: r.owner and isinstance(r.owner.op, Subtensor), S
def
incsubtensor
(
x
,
y
,
idx_list
,
inplace
=
False
,
set_instead_of_inc
=
False
):
the_op
=
IncSubtensor
(
idx_list
,
inplace
,
set_instead_of_inc
)
return
the_op
(
x
,
y
)
return
the_op
(
x
,
y
,
*
Subtensor
.
collapse
(
idx_list
,
lambda
entry
:
isinstance
(
entry
,
Variable
)))
class
IncSubtensor
(
Op
):
"""Increment a subtensor.
...
...
@@ -2139,7 +2146,7 @@ class IncSubtensor(Op):
def
make_node
(
self
,
x
,
y
,
*
inputs
):
x
,
y
=
map
(
as_tensor_variable
,
[
x
,
y
])
inputs
=
tuple
(
map
(
scal
.
as_scalar
,
inputs
))
inputs
=
tuple
(
map
(
Subtensor
.
my_
as_scalar
,
inputs
))
idx_list
=
list
(
self
.
idx_list
)
if
len
(
idx_list
)
>
x
.
type
.
ndim
:
...
...
@@ -2150,8 +2157,8 @@ class IncSubtensor(Op):
padded
=
idx_list
+
[
slice
(
0
,
sys
.
maxint
,
1
)]
*
(
x
.
type
.
ndim
-
len
(
idx_list
))
broadcastable
=
[
bc
for
p
,
bc
in
zip
(
padded
,
x
.
type
.
broadcastable
)
if
isinstance
(
p
,
slice
)]
if
y
.
type
.
broadcastable
!=
tuple
(
broadcastable
):
raise
TypeError
(
"Invalid broadcastable pattern for y in IncSubtensor.make_node"
)
#
if y.type.broadcastable != tuple(broadcastable):
#
raise TypeError("Invalid broadcastable pattern for y in IncSubtensor.make_node")
input_types
=
Subtensor
.
collapse
(
idx_list
,
lambda
entry
:
isinstance
(
entry
,
gof
.
Type
))
if
len
(
inputs
)
!=
len
(
input_types
):
...
...
theano/tensor/tests/test_incsubtensor.py
0 → 100644
浏览文件 @
f76c3213
import
numpy
as
N
import
unittest
from
theano.tests
import
unittest_tools
as
utt
import
theano
import
theano.tensor
as
T
class
Test_incsubtensor
(
unittest
.
TestCase
):
"""Partial testing.
What could be tested:
- increment vs set
- thing incremented: scalar, vector, matrix,
- increment/set: constant, scalar, vector, matrix
- indices: scalar vs slice, constant vs variable, out of bound, ...
- inplace
"""
def
setUp
(
self
):
utt
.
seed_rng
()
def
test_simple_ok
(
self
):
"""Increments or sets part of a tensor by a scalar using full slice and
a partial slice depending on a scalar.
"""
a
=
T
.
dmatrix
()
increment
=
T
.
dscalar
()
sl1
=
slice
(
None
)
sl2_end
=
T
.
lscalar
()
sl2
=
slice
(
sl2_end
)
for
do_set
in
[
False
,
True
]:
a_incremented
=
T
.
incsubtensor
(
a
,
increment
,
[
sl1
,
sl2
],
set_instead_of_inc
=
do_set
)
f
=
theano
.
function
([
a
,
increment
,
sl2_end
],
a_incremented
)
val_a
=
N
.
ones
((
5
,
5
))
val_inc
=
2.3
val_sl2_end
=
2
result
=
f
(
val_a
,
val_inc
,
val_sl2_end
)
expected_result
=
N
.
copy
(
val_a
)
if
do_set
:
expected_result
[:,:
val_sl2_end
]
=
val_inc
else
:
expected_result
[:,:
val_sl2_end
]
+=
val_inc
self
.
failUnless
(
N
.
array_equal
(
result
,
expected_result
))
return
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论