Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
1294ff93
提交
1294ff93
authored
7月 31, 2012
作者:
Nicolas Bouchard
提交者:
Frederic
7月 12, 2013
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Made corrections, add missing methods and tests.
上级
2edb35bc
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
111 行增加
和
12 行删除
+111
-12
truedot.py
theano/sparse/sandbox/truedot.py
+111
-12
没有找到文件。
theano/sparse/sandbox/truedot.py
浏览文件 @
1294ff93
import
unittest
import
unittest
import
theano
import
numpy
import
numpy
import
scipy.sparse
as
sp
from
theano
import
sparse
from
theano
import
gof
,
tensor
,
compile
from
theano
import
gof
,
tensor
,
compile
from
theano.sparse.tests.test_basic
import
eval_outputs
from
theano.sparse.tests.test_basic
import
eval_outputs
from
theano.sparse.basic
import
_
(
from
theano.sparse.basic
import
(
is_sparse_variable
,
_is_dense_variable
,
_
is_sparse_variable
,
_is_dense_variable
,
as_sparse_variable
,
_is_sparse
,
_mtypes
,
_mtype_to_str
)
as_sparse_variable
,
_is_sparse
,
_mtypes
,
_mtype_to_str
)
from
theano.sparse
import
SparseType
,
dense_from_sparse
,
transpose
from
theano.sparse
import
SparseType
,
dense_from_sparse
,
transpose
from
theano.sparse.tests.test_basic
import
sparse_random_inputs
from
theano.tests
import
unittest_tools
as
utt
from
theano.sparse
import
verify_grad_sparse
class
TrueDot
(
gof
.
op
.
Op
):
class
TrueDot
(
gof
.
op
.
Op
):
"""Calculate the true dot operation between two matrices.
"""Calculate the true dot operation between two matrices.
...
@@ -29,7 +35,7 @@ class TrueDot(gof.op.Op):
...
@@ -29,7 +35,7 @@ class TrueDot(gof.op.Op):
:param x: Sparse matrix for the left operand.
:param x: Sparse matrix for the left operand.
:param y: Sparse or dense matrix for the right operand.
:param y: Sparse or dense matrix for the right operand.
:return: The dot product `x` . `y`.
:return: The dot product `x` . `y`
in a sparse matrix
.
:note:
:note:
- The grad implemented is regular, i.e. not structured.
- The grad implemented is regular, i.e. not structured.
...
@@ -46,7 +52,7 @@ class TrueDot(gof.op.Op):
...
@@ -46,7 +52,7 @@ class TrueDot(gof.op.Op):
self
.
grad_preserves_dense
==
other
.
grad_preserves_dense
)
self
.
grad_preserves_dense
==
other
.
grad_preserves_dense
)
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
(
self
.
grad_preserves_dense
)
return
hash
(
type
(
self
))
^
hash
(
self
.
grad_preserves_dense
)
def
__ne__
(
self
,
other
):
def
__ne__
(
self
,
other
):
return
not
(
self
==
other
)
return
not
(
self
==
other
)
...
@@ -71,7 +77,7 @@ class TrueDot(gof.op.Op):
...
@@ -71,7 +77,7 @@ class TrueDot(gof.op.Op):
else
:
else
:
raise
NotImplementedError
()
raise
NotImplementedError
()
inputs
=
[
x
,
y
]
# Need to convert? e.g. assparse
inputs
=
[
x
,
y
]
# Need to convert? e.g. assparse
outputs
=
[
SparseType
(
dtype
=
x
.
type
.
dtype
,
outputs
=
[
SparseType
(
dtype
=
x
.
type
.
dtype
,
format
=
myformat
)
.
make_variable
()]
format
=
myformat
)
.
make_variable
()]
return
gof
.
Apply
(
self
,
inputs
,
outputs
)
return
gof
.
Apply
(
self
,
inputs
,
outputs
)
...
@@ -86,19 +92,26 @@ class TrueDot(gof.op.Op):
...
@@ -86,19 +92,26 @@ class TrueDot(gof.op.Op):
x
,
y
=
inp
x
,
y
=
inp
out
,
=
out_
out
,
=
out_
rval
=
x
.
dot
(
y
)
rval
=
x
.
dot
(
y
)
if
not
sp
.
issparse
(
rval
):
rval
=
getattr
(
sp
,
x
.
format
+
'_matrix'
)(
rval
)
out
[
0
]
=
rval
out
[
0
]
=
rval
def
grad
(
self
,
inp
,
grads
):
def
grad
(
self
,
(
x
,
y
),
(
gz
,
)):
x
,
y
=
inp
gz
,
=
grads
assert
_is_sparse_variable
(
gz
)
assert
_is_sparse_variable
(
gz
)
assert
_is_sparse_variable
(
x
)
assert
_is_sparse_variable
(
x
)
rval
=
[
true_dot
(
gz
,
y
.
T
),
true_dot
(
x
.
T
,
gz
)]
rval
=
[
true_dot
(
gz
,
y
.
T
),
true_dot
(
x
.
T
,
gz
)]
if
_is_dense_variable
(
y
):
if
_is_dense_variable
(
y
):
if
self
.
grad_preserves_dense
:
if
self
.
grad_preserves_dense
:
rval
[
1
]
=
dense_from_sparse
(
rval
[
1
])
rval
[
1
]
=
dense_from_sparse
(
rval
[
1
])
return
rval
return
rval
def
infer_shape
(
self
,
node
,
shapes
):
return
[(
shapes
[
0
][
0
],
shapes
[
1
][
1
])]
def
__str__
(
self
):
return
self
.
__class__
.
__name__
def
true_dot
(
x
,
y
,
grad_preserves_dense
=
True
):
def
true_dot
(
x
,
y
,
grad_preserves_dense
=
True
):
# TODO
# TODO
...
@@ -123,7 +136,93 @@ def true_dot(x, y, grad_preserves_dense=True):
...
@@ -123,7 +136,93 @@ def true_dot(x, y, grad_preserves_dense=True):
return
transpose
(
TrueDot
(
grad_preserves_dense
)(
y
.
T
,
x
.
T
))
return
transpose
(
TrueDot
(
grad_preserves_dense
)(
y
.
T
,
x
.
T
))
class
test_true_dot
(
unittest
.
TestCase
):
class
TrueDotTester
(
utt
.
InferShapeTester
):
def
setUp
(
self
):
super
(
TrueDotTester
,
self
)
.
setUp
()
self
.
op
=
true_dot
self
.
op_class
=
TrueDot
def
test_op_ss
(
self
):
for
format
in
sparse
.
sparse_formats
:
for
dtype
in
sparse
.
all_dtypes
:
variable
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
10
,
10
),
out_dtype
=
dtype
,
n
=
2
,
p
=
0.1
)
f
=
theano
.
function
(
variable
,
self
.
op
(
*
variable
))
tested
=
f
(
*
data
)
x
,
y
=
[
m
.
toarray
()
for
m
in
data
]
expected
=
numpy
.
dot
(
x
,
y
)
assert
tested
.
format
==
format
assert
tested
.
dtype
==
expected
.
dtype
tested
=
tested
.
toarray
()
assert
numpy
.
allclose
(
tested
,
expected
)
def
test_op_sd
(
self
):
for
format
in
sparse
.
sparse_formats
:
for
dtype
in
sparse
.
all_dtypes
:
variable
,
data
=
sparse_random_inputs
(
format
,
shape
=
(
10
,
10
),
out_dtype
=
dtype
,
n
=
2
,
p
=
0.1
)
variable
[
1
]
=
tensor
.
TensorType
(
dtype
=
dtype
,
broadcastable
=
(
False
,
False
))()
data
[
1
]
=
data
[
1
]
.
toarray
()
f
=
theano
.
function
(
variable
,
self
.
op
(
*
variable
))
tested
=
f
(
*
data
)
expected
=
numpy
.
dot
(
data
[
0
]
.
toarray
(),
data
[
1
])
assert
tested
.
format
==
format
assert
tested
.
dtype
==
expected
.
dtype
tested
=
tested
.
toarray
()
assert
numpy
.
allclose
(
tested
,
expected
)
def
test_infer_shape
(
self
):
for
format
in
sparse
.
sparse_formats
:
for
dtype
in
sparse
.
all_dtypes
:
(
x
,
),
(
x_value
,
)
=
sparse_random_inputs
(
format
,
shape
=
(
9
,
10
),
out_dtype
=
dtype
,
p
=
0.1
)
(
y
,
),
(
y_value
,
)
=
sparse_random_inputs
(
format
,
shape
=
(
10
,
24
),
out_dtype
=
dtype
,
p
=
0.1
)
variable
=
[
x
,
y
]
data
=
[
x_value
,
y_value
]
self
.
_compile_and_check
(
variable
,
[
self
.
op
(
*
variable
)],
data
,
self
.
op_class
)
def
test_grad
(
self
):
for
format
in
sparse
.
sparse_formats
:
for
dtype
in
sparse
.
float_dtypes
:
(
x
,
),
(
x_value
,
)
=
sparse_random_inputs
(
format
,
shape
=
(
9
,
10
),
out_dtype
=
dtype
,
p
=
0.1
)
(
y
,
),
(
y_value
,
)
=
sparse_random_inputs
(
format
,
shape
=
(
10
,
24
),
out_dtype
=
dtype
,
p
=
0.1
)
variable
=
[
x
,
y
]
data
=
[
x_value
,
y_value
]
verify_grad_sparse
(
self
.
op
,
data
,
structured
=
False
)
class
TrueDotTester2
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
numpy
.
random
.
seed
(
44
)
numpy
.
random
.
seed
(
44
)
...
@@ -210,7 +309,7 @@ class test_true_dot(unittest.TestCase):
...
@@ -210,7 +309,7 @@ class test_true_dot(unittest.TestCase):
z
=
eval_outputs
([
zop
])
z
=
eval_outputs
([
zop
])
self
.
assertTrue
(
_is_sparse
(
z
))
self
.
assertTrue
(
_is_sparse
(
z
))
self
.
assertTrue
(
z
.
shape
==
(
500
,
2
))
self
.
assertTrue
(
z
.
shape
==
(
500
,
2
))
#
self.assertTrue(type(z) is mtype)
#
self.assertTrue(type(z) is mtype)
w
=
mtype
((
500
,
2
))
w
=
mtype
((
500
,
2
))
w
[(
10
,
0
)]
=
3.
w
[(
10
,
0
)]
=
3.
...
@@ -254,7 +353,7 @@ class test_true_dot(unittest.TestCase):
...
@@ -254,7 +353,7 @@ class test_true_dot(unittest.TestCase):
for
epoch
in
xrange
(
50
):
for
epoch
in
xrange
(
50
):
y
,
loss
,
gw
=
trainfn
(
x
,
w
)
y
,
loss
,
gw
=
trainfn
(
x
,
w
)
w
=
w
-
(
lr
*
gw
)
w
=
w
-
(
lr
*
gw
)
print
loss
#
print loss
self
.
assertTrue
(
origloss
>
loss
)
self
.
assertTrue
(
origloss
>
loss
)
self
.
assertTrue
(
'1.05191241115'
==
str
(
loss
))
self
.
assertTrue
(
'1.05191241115'
==
str
(
loss
))
...
@@ -265,7 +364,7 @@ class test_true_dot(unittest.TestCase):
...
@@ -265,7 +364,7 @@ class test_true_dot(unittest.TestCase):
for
mtype
in
_mtypes
:
for
mtype
in
_mtypes
:
x
=
tensor
.
matrix
(
'x'
)
x
=
tensor
.
matrix
(
'x'
)
w
=
SparseType
(
dtype
=
'float64'
,
w
=
SparseType
(
dtype
=
'float64'
,
format
=
mtype_to_str
[
mtype
])
.
make_variable
()
format
=
_
mtype_to_str
[
mtype
])
.
make_variable
()
xw
=
dense_from_sparse
(
true_dot
(
w
,
x
))
xw
=
dense_from_sparse
(
true_dot
(
w
,
x
))
y
=
dense_from_sparse
(
true_dot
(
w
.
T
,
xw
))
y
=
dense_from_sparse
(
true_dot
(
w
.
T
,
xw
))
diff
=
x
-
y
diff
=
x
-
y
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论