Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
8b62864b
提交
8b62864b
authored
2月 15, 2017
作者:
hantek
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
test passing
上级
f4c355f4
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
72 行增加
和
16 行删除
+72
-16
subtensor.py
theano/gpuarray/subtensor.py
+4
-7
test_subtensor.py
theano/gpuarray/tests/test_subtensor.py
+43
-3
basic.py
theano/tensor/basic.py
+25
-6
没有找到文件。
theano/gpuarray/subtensor.py
浏览文件 @
8b62864b
...
...
@@ -1102,9 +1102,7 @@ class GpuExtractDiag(Subtensor):
broadcastable
=
x
.
broadcastable
[:
axis_small
]
+
\
x
.
broadcastable
[
axis_small
+
1
:
axis_large
]
+
\
x
.
broadcastable
[
axis_large
+
1
:]
+
(
False
,)
return
gof
.
Apply
(
self
,
[
x
],
[
x
.
type
.
__class__
(
dtype
=
x
.
dtype
,
broadcastable
=
broadcastable
)()])
return
gof
.
Apply
(
self
,
[
x
],
[
x
.
type
.
clone
(
broadcastable
=
broadcastable
)()])
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
x
,)
=
inputs
...
...
@@ -1197,9 +1195,8 @@ class GpuAllocDiag(Op):
if
x
.
ndim
!=
1
:
raise
ValueError
(
'AllocDiag argument must be a vector!'
,
x
)
return
gof
.
Apply
(
self
,
[
x
],
[
x
.
type
.
__class__
(
dtype
=
x
.
dtype
,
broadcastable
=
x
.
broadcastable
)()])
return
gof
.
Apply
(
self
,
[
x
],
[
x
.
type
.
clone
(
broadcastable
=
(
False
,
False
))()])
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
x
,)
=
inputs
...
...
@@ -1218,7 +1215,7 @@ class GpuAllocDiag(Op):
def
grad
(
self
,
inputs
,
gout
):
(
gz
,)
=
gout
return
[
GpuExtractDiag
(
offset
=
self
.
offset
,
axis1
=
0
,
axis2
=
1
,
view
=
False
)(
gz
)]
return
[
GpuExtractDiag
(
offset
=
self
.
offset
,
axis1
=
0
,
axis2
=
1
)(
gz
)]
def
infer_shape
(
self
,
node
,
shapes
):
dim
=
shapes
[
0
][
0
]
+
abs
(
self
.
offset
)
...
...
theano/gpuarray/tests/test_subtensor.py
浏览文件 @
8b62864b
...
...
@@ -177,7 +177,7 @@ def test_adv_subtensor():
assert
np
.
allclose
(
rval
,
rep
)
class
test_gpu
diagonal
(
unittest
.
TestCase
):
class
test_gpu
extractdiag
(
unittest
.
TestCase
):
def
test_matrix
(
self
):
x
=
tensor
.
matrix
()
np_x
=
np
.
arange
(
77
)
.
reshape
(
7
,
11
)
.
astype
(
theano
.
config
.
floatX
)
...
...
@@ -213,8 +213,48 @@ class test_gpuallocdiag(unittest.TestCase):
def
test_grad
(
self
):
x
=
tensor
.
vector
()
np_x
=
np
.
arange
(
7
)
.
astype
(
theano
.
config
.
floatX
)
np_x
=
np
.
random
.
randn
(
7
)
.
astype
(
theano
.
config
.
floatX
)
# offset = 0 case:
mtx_x
=
GpuAllocDiag
()(
x
)
sum_mtx_x
=
tensor
.
sum
(
mtx_x
)
grad
=
tensor
.
grad
(
sum_mtx_x
,
x
)
grad_x
=
tensor
.
grad
(
sum_mtx_x
,
x
)
grad_mtx_x
=
tensor
.
grad
(
sum_mtx_x
,
mtx_x
)
fn_grad_x
=
theano
.
function
([
x
],
grad_x
)
fn_grad_mtx_x
=
theano
.
function
([
x
],
grad_mtx_x
)
computed_grad_x
=
fn_grad_x
(
np_x
)
computed_grad_mtx_x
=
fn_grad_mtx_x
(
np_x
)
true_grad_x
=
np
.
diagonal
(
computed_grad_mtx_x
,
0
)
assert
np
.
allclose
(
computed_grad_x
,
true_grad_x
)
# offset > 0 case:
mtx_x
=
GpuAllocDiag
(
2
)(
x
)
sum_mtx_x
=
tensor
.
sum
(
mtx_x
)
grad_x
=
tensor
.
grad
(
sum_mtx_x
,
x
)
grad_mtx_x
=
tensor
.
grad
(
sum_mtx_x
,
mtx_x
)
fn_grad_x
=
theano
.
function
([
x
],
grad_x
)
fn_grad_mtx_x
=
theano
.
function
([
x
],
grad_mtx_x
)
computed_grad_x
=
fn_grad_x
(
np_x
)
computed_grad_mtx_x
=
fn_grad_mtx_x
(
np_x
)
true_grad_x
=
np
.
diagonal
(
computed_grad_mtx_x
,
2
)
assert
np
.
allclose
(
computed_grad_x
,
true_grad_x
)
# offset < 0 case:
mtx_x
=
GpuAllocDiag
(
-
3
)(
x
)
sum_mtx_x
=
tensor
.
sum
(
mtx_x
)
grad_x
=
tensor
.
grad
(
sum_mtx_x
,
x
)
grad_mtx_x
=
tensor
.
grad
(
sum_mtx_x
,
mtx_x
)
fn_grad_x
=
theano
.
function
([
x
],
grad_x
)
fn_grad_mtx_x
=
theano
.
function
([
x
],
grad_mtx_x
)
computed_grad_x
=
fn_grad_x
(
np_x
)
computed_grad_mtx_x
=
fn_grad_mtx_x
(
np_x
)
true_grad_x
=
np
.
diagonal
(
computed_grad_mtx_x
,
-
3
)
assert
np
.
allclose
(
computed_grad_x
,
true_grad_x
)
# assert
theano/tensor/basic.py
浏览文件 @
8b62864b
...
...
@@ -6167,6 +6167,24 @@ class ExtractDiag(Op):
def
diagonal
(
a
,
offset
=
0
,
axis1
=
0
,
axis2
=
1
):
"""
A helper function for `theano.tensor.ExtractDiag`. It accepts tensor with
`ndim >= 2` as input. The name `diagonal` is just meant to keep it
consistent with numpy.
Parameters
----------
a : symbolic tensor
offset : int
offset
axis1 : int
axis2 : int
Returns
-------
tensor : symbolic tensor
"""
return
ExtractDiag
(
offset
,
axis1
,
axis2
)(
a
)
...
...
@@ -6223,12 +6241,13 @@ class AllocDiag(Op):
def
diag
(
v
,
k
=
0
):
"""
A helper function for two ops: theano.tensor.ExtractDiag and
theano.tensor.AllocDiag. It both accepts tensor vector and tensor matrix.
While the passed tensor variable `v` has `v.ndim>=2`, it builds a
ExtractDiag instance, and returns a vector with its entries equal to
`v`'s main diagonal; otherwise if `v.ndim` is `1`, it builds a AllocDiag
instance, and returns a matrix with `v` at its k-th diaogonal.
A helper function for two ops: `theano.tensor.ExtractDiag` and
`theano.tensor.AllocDiag`. The name `diag` is meant to keep it consistent
with numpy. It both accepts tensor vector and tensor matrix.
While the passed tensor variable `v` has `v.ndim>=2`, it builds a
`ExtractDiag` instance, and returns a vector with its entries equal to
`v`'s main diagonal; otherwise if `v.ndim` is `1`, it builds an `AllocDiag`
instance, and returns a matrix with `v` at its k-th diaogonal.
Parameters
----------
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论