Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
1272852f
提交
1272852f
authored
7月 12, 2013
作者:
lamblin
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1448 from nouiz/adv_sub1_grad
Add theano.sparse_grad interface.
上级
61b6baeb
ddb0aa47
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
115 行增加
和
2 行删除
+115
-2
index.txt
doc/library/index.txt
+2
-0
__init__.py
theano/__init__.py
+14
-0
gradient.py
theano/gradient.py
+6
-0
test_basic.py
theano/sparse/tests/test_basic.py
+69
-0
basic.py
theano/tensor/basic.py
+24
-2
没有找到文件。
doc/library/index.txt
浏览文件 @
1272852f
...
...
@@ -48,3 +48,5 @@ There are also some top-level imports that you might find more convenient:
Works like :func:`tensor.dot` for both sparse and dense matrix products
.. autofunction:: theano.clone
.. autofunction:: theano.sparse_grad
theano/__init__.py
浏览文件 @
1272852f
...
...
@@ -168,6 +168,20 @@ def get_scalar_constant_value(v):
return
tensor
.
get_scalar_constant_value
(
v
)
def
sparse_grad
(
var
):
"""This function return a new variable whose gradient will be
stored in a sparse format instead of dense.
Currently only variable created by AdvancedSubtensor1 is supported.
i.e. a_tensor_var[an_int_vector].
.. versionadded:: 0.6rc4
"""
assert
isinstance
(
var
.
owner
.
op
,
tensor
.
AdvancedSubtensor1
)
ret
=
var
.
owner
.
op
.
__class__
(
sparse_grad
=
True
)(
*
var
.
owner
.
inputs
)
return
ret
import
theano.tests
if
hasattr
(
theano
.
tests
,
"TheanoNoseTester"
):
test
=
theano
.
tests
.
TheanoNoseTester
()
.
test
...
...
theano/gradient.py
浏览文件 @
1272852f
...
...
@@ -1266,6 +1266,12 @@ class numeric_grad(object):
"""
abs_err
=
abs
(
a
-
b
)
rel_err
=
abs_err
/
numpy
.
maximum
(
abs
(
a
)
+
abs
(
b
),
1e-8
)
# The numpy.asarray are needed as if a or b is a sparse matrix
# this would result in a numpy.matrix and not a numpy.ndarray
# and the behave differently causing problem later.
# In particular a_npy_matrix.flatten().shape == (1, n_element)
abs_err
=
numpy
.
asarray
(
abs_err
)
rel_err
=
numpy
.
asarray
(
rel_err
)
return
(
abs_err
,
rel_err
)
def
abs_rel_errors
(
self
,
g_pt
):
...
...
theano/sparse/tests/test_basic.py
浏览文件 @
1272852f
...
...
@@ -417,6 +417,75 @@ class SparseInferShapeTester(utt.InferShapeTester):
)
class
TestConstructSparseFromList
(
unittest
.
TestCase
):
def
test_adv_sub1_sparse_grad
(
self
):
v
=
theano
.
tensor
.
ivector
()
# Assert we don't create a sparse grad by default
m
=
theano
.
tensor
.
matrix
()
sub
=
m
[
v
]
g
=
theano
.
grad
(
sub
.
sum
(),
m
)
assert
isinstance
(
g
.
owner
.
op
,
tensor
.
AdvancedIncSubtensor1
)
# Test that we create a sparse grad when asked
# OLD INTERFACE
m
=
theano
.
tensor
.
matrix
()
sub
=
m
[
v
]
m
.
type
.
sparse_grad
=
True
g
=
theano
.
grad
(
sub
.
sum
(),
m
)
assert
isinstance
(
g
.
owner
.
op
,
ConstructSparseFromList
)
# Test that we create a sparse grad when asked
# OLD INTERFACE CONSEQUENCE
m
=
theano
.
tensor
.
matrix
()
sub
=
m
[
v
]
sub
.
type
.
sparse_grad
=
True
g
=
theano
.
grad
(
sub
.
sum
(),
m
)
assert
isinstance
(
g
.
owner
.
op
,
ConstructSparseFromList
)
# Test that we create a sparse grad when asked
# USER INTERFACE
m
=
theano
.
tensor
.
matrix
()
v
=
theano
.
tensor
.
ivector
()
sub
=
theano
.
sparse_grad
(
m
[
v
])
g
=
theano
.
grad
(
sub
.
sum
(),
m
)
assert
isinstance
(
g
.
owner
.
op
,
ConstructSparseFromList
)
# Test that we create a sparse grad when asked
# Op INTERFACE
m
=
theano
.
tensor
.
matrix
()
v
=
theano
.
tensor
.
ivector
()
sub
=
theano
.
tensor
.
AdvancedSubtensor1
(
sparse_grad
=
True
)(
m
,
v
)
g
=
theano
.
grad
(
sub
.
sum
(),
m
)
assert
isinstance
(
g
.
owner
.
op
,
ConstructSparseFromList
)
# Test the sparse grad
valm
=
numpy
.
random
.
rand
(
5
,
4
)
.
astype
(
config
.
floatX
)
valv
=
numpy
.
random
.
random_integers
(
0
,
4
,
10
)
m
=
theano
.
tensor
.
matrix
()
shared_v
=
theano
.
shared
(
valv
)
def
fn
(
m
):
return
theano
.
sparse_grad
(
m
[
shared_v
])
verify_grad_sparse
(
fn
,
[
valm
])
def
test_err
(
self
):
for
ndim
in
[
1
,
3
]:
t
=
theano
.
tensor
.
TensorType
(
dtype
=
config
.
floatX
,
broadcastable
=
(
False
,)
*
ndim
)()
v
=
theano
.
tensor
.
ivector
()
sub
=
t
[
v
]
# Assert we don't create a sparse grad by default
g
=
theano
.
grad
(
sub
.
sum
(),
t
)
assert
isinstance
(
g
.
owner
.
op
,
tensor
.
AdvancedIncSubtensor1
)
# Test that we raise an error, as we can't create a sparse
# grad from tensors that don't have 2 dimensions.
sub
=
theano
.
sparse_grad
(
sub
)
self
.
assertRaises
(
TypeError
,
theano
.
grad
,
sub
.
sum
(),
t
)
class
T_AddMul
(
unittest
.
TestCase
):
def
testAddSS
(
self
):
self
.
_testSS
(
add
)
...
...
theano/tensor/basic.py
浏览文件 @
1272852f
...
...
@@ -706,6 +706,11 @@ class TensorType(Type):
self
.
name
=
name
self
.
numpy_dtype
=
numpy
.
dtype
(
self
.
dtype
)
self
.
sparse_grad
=
sparse_grad
if
sparse_grad
:
warnings
.
warn
(
"DEPRECATION WARNING: You use an old interface to"
" AdvancedSubtensor1 sparse_grad. Now use"
" theano.sparse_grad(a_tensor[an_int_vector])."
)
def
filter
(
self
,
data
,
strict
=
False
,
allow_downcast
=
None
):
"""Convert `data` to something which can be associated to a
...
...
@@ -7153,10 +7158,17 @@ def inverse_permutation(perm):
class
AdvancedSubtensor1
(
Op
):
"""Implement x[ilist] where ilist is a vector of integers."""
def
__init__
(
self
,
sparse_grad
=
False
):
self
.
sparse_grad
=
sparse_grad
def
__hash__
(
self
):
return
hash
(
type
(
self
))
def
__eq__
(
self
,
other
):
# Don't check the sparse_grad attribute as
# This don't change the output of this op
# So we want the merge optimier to merge two op
# that differ from there sparse_grad attribute.
return
type
(
self
)
==
type
(
other
)
def
__str__
(
self
):
...
...
@@ -7212,8 +7224,18 @@ class AdvancedSubtensor1(Op):
x
,
ilist
=
inputs
gz
,
=
grads
assert
len
(
inputs
)
==
2
if
x
.
type
.
sparse_grad
:
sparse
=
False
if
getattr
(
x
.
type
,
'sparse_grad'
,
False
):
sparse
=
True
warnings
.
warn
(
"DEPRECATION WARNING: AdvancedSubtensor1, you are using"
" an old interface to the sparse grad. You should use"
" theano.sparse_grad(a_tensor[an_int_vector]). "
)
if
sparse
or
self
.
sparse_grad
:
if
x
.
type
.
ndim
!=
2
:
raise
TypeError
(
"AdvancedSubtensor1: you can't take the sparse grad"
" from a tensor with ndim != 2. ndim is "
+
str
(
x
.
type
.
ndim
))
if
sparse_module_ref
is
None
:
import
theano.sparse
as
sparse_module_ref
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论