Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
1744a5b1
提交
1744a5b1
authored
7月 09, 2014
作者:
Tanjay94
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Modified small mistakes in GetItemList and GetItem2Lists.
上级
519f9d26
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
35 行增加
和
10 行删除
+35
-10
basic.py
theano/sparse/basic.py
+32
-10
test_basic.py
theano/sparse/tests/test_basic.py
+3
-0
没有找到文件。
theano/sparse/basic.py
浏览文件 @
1744a5b1
...
@@ -1000,6 +1000,7 @@ csc_from_dense = SparseFromDense('csc')
...
@@ -1000,6 +1000,7 @@ csc_from_dense = SparseFromDense('csc')
:return: The same as `x` in a sparse matrix format.
:return: The same as `x` in a sparse matrix format.
"""
"""
# Indexing
# Indexing
class
GetItemList
(
gof
.
op
.
Op
):
class
GetItemList
(
gof
.
op
.
Op
):
...
@@ -1037,6 +1038,14 @@ class GetItemList(gof.op.Op):
...
@@ -1037,6 +1038,14 @@ class GetItemList(gof.op.Op):
return
self
.
__class__
.
__name__
return
self
.
__class__
.
__name__
get_item_list
=
GetItemList
()
get_item_list
=
GetItemList
()
"""Select row of sparse matrix,
returning them as a new sparse matrix.
:param x: Sparse matrix.
:param index: List of rows.
:return: The corresponding rows in `x`.
"""
class
GetItemListGrad
(
gof
.
op
.
Op
):
class
GetItemListGrad
(
gof
.
op
.
Op
):
...
@@ -1048,7 +1057,7 @@ class GetItemListGrad(gof.op.Op):
...
@@ -1048,7 +1057,7 @@ class GetItemListGrad(gof.op.Op):
return
hash
(
type
(
self
))
return
hash
(
type
(
self
))
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
return
[(
shapes
[
0
]
[
0
],
shapes
[
0
][
1
]
)]
return
[(
shapes
[
0
])]
def
make_node
(
self
,
x
,
index
,
gz
):
def
make_node
(
self
,
x
,
index
,
gz
):
x
=
as_sparse_variable
(
x
)
x
=
as_sparse_variable
(
x
)
...
@@ -1060,6 +1069,11 @@ class GetItemListGrad(gof.op.Op):
...
@@ -1060,6 +1069,11 @@ class GetItemListGrad(gof.op.Op):
ind
=
tensor
.
as_tensor_variable
(
index
)
ind
=
tensor
.
as_tensor_variable
(
index
)
assert
ind
.
ndim
==
1
assert
ind
.
ndim
==
1
scipy_ver
=
[
int
(
n
)
for
n
in
scipy
.
__version__
.
split
(
'.'
)[:
2
]]
if
not
scipy_ver
>=
[
0
,
13
]:
raise
NotImplemented
(
"Scipy version is to old"
)
return
gof
.
Apply
(
self
,
[
x
,
ind
,
gz
],
[
x
.
type
()])
return
gof
.
Apply
(
self
,
[
x
,
ind
,
gz
],
[
x
.
type
()])
def
perform
(
self
,
node
,
inp
,
(
out
,
)):
def
perform
(
self
,
node
,
inp
,
(
out
,
)):
...
@@ -1071,11 +1085,8 @@ class GetItemListGrad(gof.op.Op):
...
@@ -1071,11 +1085,8 @@ class GetItemListGrad(gof.op.Op):
y
=
scipy
.
sparse
.
csr_matrix
((
x
.
shape
[
0
],
x
.
shape
[
1
]))
y
=
scipy
.
sparse
.
csr_matrix
((
x
.
shape
[
0
],
x
.
shape
[
1
]))
else
:
else
:
y
=
scipy
.
sparse
.
csc_matrix
((
x
.
shape
[
0
],
x
.
shape
[
1
]))
y
=
scipy
.
sparse
.
csc_matrix
((
x
.
shape
[
0
],
x
.
shape
[
1
]))
c
=
0
for
a
in
range
(
0
,
len
(
indices
)):
for
a
in
range
(
0
,
gz
.
shape
[
0
]):
y
[
indices
[
a
]]
=
gz
[
a
]
for
b
in
range
(
0
,
gz
.
shape
[
1
]):
y
[(
indices
[
c
],
b
)]
=
gz
[(
a
,
b
)]
c
=
c
+
1
out
[
0
]
=
y
out
[
0
]
=
y
...
@@ -1107,7 +1118,9 @@ class GetItem2Lists(gof.op.Op):
...
@@ -1107,7 +1118,9 @@ class GetItem2Lists(gof.op.Op):
ind1
=
inp
[
1
]
ind1
=
inp
[
1
]
ind2
=
inp
[
2
]
ind2
=
inp
[
2
]
out
[
0
]
=
numpy
.
asarray
(
x
[
ind1
,
ind2
])
.
flatten
()
out
[
0
]
=
numpy
.
asarray
(
x
[
ind1
,
ind2
])
.
flatten
()
"""Here scipy returns the corresponding elements in a matrix which isn't what we are aiming for.
Using asarray and flatten, out[0] becomes an array.
"""
def
grad
(
self
,
inputs
,
g_outputs
):
def
grad
(
self
,
inputs
,
g_outputs
):
x
,
ind1
,
ind2
=
inputs
x
,
ind1
,
ind2
=
inputs
gout
,
=
g_outputs
gout
,
=
g_outputs
...
@@ -1119,6 +1132,14 @@ class GetItem2Lists(gof.op.Op):
...
@@ -1119,6 +1132,14 @@ class GetItem2Lists(gof.op.Op):
return
self
.
__class__
.
__name__
return
self
.
__class__
.
__name__
get_item_2lists
=
GetItem2Lists
()
get_item_2lists
=
GetItem2Lists
()
"""Select elements of sparse matrix, returning them in a vector.
:param x: Sparse matrix.
:param index: List of two lists, first list indicating the row
of each element and second list indicating its column.
:return: The corresponding elements in `x`.
"""
class
GetItem2ListsGrad
(
gof
.
op
.
Op
):
class
GetItem2ListsGrad
(
gof
.
op
.
Op
):
...
@@ -1130,7 +1151,7 @@ class GetItem2ListsGrad(gof.op.Op):
...
@@ -1130,7 +1151,7 @@ class GetItem2ListsGrad(gof.op.Op):
return
hash
(
type
(
self
))
return
hash
(
type
(
self
))
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
return
[(
shapes
[
0
]
[
0
],
shapes
[
0
][
1
]
)]
return
[(
shapes
[
0
])]
def
make_node
(
self
,
x
,
ind1
,
ind2
,
gz
):
def
make_node
(
self
,
x
,
ind1
,
ind2
,
gz
):
x
=
as_sparse_variable
(
x
)
x
=
as_sparse_variable
(
x
)
...
@@ -1141,6 +1162,8 @@ class GetItem2ListsGrad(gof.op.Op):
...
@@ -1141,6 +1162,8 @@ class GetItem2ListsGrad(gof.op.Op):
ind2
=
tensor
.
as_tensor_variable
(
ind2
)
ind2
=
tensor
.
as_tensor_variable
(
ind2
)
assert
ind1
.
ndim
==
1
assert
ind1
.
ndim
==
1
assert
ind2
.
ndim
==
1
assert
ind2
.
ndim
==
1
assert
"int"
in
ind1
.
dtype
assert
"int"
in
ind2
.
dtype
return
gof
.
Apply
(
self
,
[
x
,
ind1
,
ind2
,
gz
],
[
x
.
type
()])
return
gof
.
Apply
(
self
,
[
x
,
ind1
,
ind2
,
gz
],
[
x
.
type
()])
...
@@ -1155,9 +1178,8 @@ class GetItem2ListsGrad(gof.op.Op):
...
@@ -1155,9 +1178,8 @@ class GetItem2ListsGrad(gof.op.Op):
else
:
else
:
y
=
scipy
.
sparse
.
csc_matrix
((
x
.
shape
[
0
],
x
.
shape
[
1
]))
y
=
scipy
.
sparse
.
csc_matrix
((
x
.
shape
[
0
],
x
.
shape
[
1
]))
z
=
0
z
=
0
for
i
in
range
(
0
,
len
(
ind1
)):
for
z
in
range
(
0
,
len
(
ind1
)):
y
[(
ind1
[
z
],
ind2
[
z
])]
=
gz
[
z
]
y
[(
ind1
[
z
],
ind2
[
z
])]
=
gz
[
z
]
z
=
z
+
1
out
[
0
]
=
y
out
[
0
]
=
y
...
...
theano/sparse/tests/test_basic.py
浏览文件 @
1744a5b1
...
@@ -2054,7 +2054,10 @@ class Test_getitem(unittest.TestCase):
...
@@ -2054,7 +2054,10 @@ class Test_getitem(unittest.TestCase):
x
,
x_val
=
sparse_random_inputs
(
"csr"
,
(
4
,
5
),
out_dtype
=
"float64"
)
x
,
x_val
=
sparse_random_inputs
(
"csr"
,
(
4
,
5
),
out_dtype
=
"float64"
)
try
:
verify_grad_sparse
(
op_with_fixed_index
,
x_val
)
verify_grad_sparse
(
op_with_fixed_index
,
x_val
)
except
NotImplementedError
,
e
:
assert
"Scipy version is to old"
in
str
(
e
)
def
test_GetItem2Lists
(
self
):
def
test_GetItem2Lists
(
self
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论