Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f766415c
提交
f766415c
authored
9月 21, 2017
作者:
Shawn Tan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
GpuAllocDiag implementation. Current tests work fine.
上级
268207b4
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
60 行增加
和
22 行删除
+60
-22
subtensor.py
theano/gpuarray/subtensor.py
+57
-20
basic.py
theano/tensor/basic.py
+3
-2
没有找到文件。
theano/gpuarray/subtensor.py
浏览文件 @
f766415c
...
@@ -1357,34 +1357,71 @@ class GpuExtractDiag(Op):
...
@@ -1357,34 +1357,71 @@ class GpuExtractDiag(Op):
class
GpuAllocDiag
(
Op
):
class
GpuAllocDiag
(
Op
):
__props__
=
(
"offset"
,)
__props__
=
(
"offset"
,
"axis1"
,
"axis2"
)
def
__init__
(
self
,
offset
=
0
):
def
__init__
(
self
,
offset
=
0
,
axis1
=
0
,
axis2
=
1
):
self
.
offset
=
offset
self
.
offset
=
offset
self
.
axis1
=
axis1
self
.
axis2
=
axis2
def
make_node
(
self
,
_x
):
def
make_node
(
self
,
diag
):
ctx_name
=
infer_context_name
(
_x
)
ctx_name
=
infer_context_name
(
diag
)
x
=
as_gpuarray_variable
(
_x
,
ctx_name
)
diag
=
as_gpuarray_variable
(
diag
,
ctx_name
)
if
diag
.
type
.
ndim
<
1
:
if
x
.
ndim
!=
1
:
raise
ValueError
(
'AllocDiag needs an input with 1 or more '
raise
ValueError
(
'AllocDiag argument must be a vector!'
,
x
)
'dimensions'
,
diag
.
type
)
return
gof
.
Apply
(
return
gof
.
Apply
(
self
,
[
x
],
[
x
.
type
.
clone
(
broadcastable
=
(
False
,
False
))()])
self
,
[
diag
],
[
diag
.
type
.
__class__
(
dtype
=
diag
.
dtype
,
broadcastable
=
[
False
]
*
(
diag
.
ndim
+
1
))()]
)
def
perform
(
self
,
node
,
inputs
,
outputs
):
def
perform
(
self
,
node
,
inputs
,
outputs
):
(
x
,)
=
inputs
(
x
,)
=
inputs
(
z
,)
=
outputs
(
z
,)
=
outputs
axis1
=
np
.
minimum
(
self
.
axis1
,
self
.
axis2
)
axis2
=
np
.
maximum
(
self
.
axis1
,
self
.
axis2
)
offset
=
self
.
offset
dim
=
x
.
shape
[
0
]
+
abs
(
self
.
offset
)
result_shape
=
x
.
shape
[:
-
1
]
+
(
x
.
shape
[
-
1
]
+
abs
(
offset
),)
*
2
z
[
0
]
=
gpuarray
.
zeros
((
dim
,
dim
),
dtype
=
x
.
dtype
,
context
=
x
.
context
)
result_buffer_shape
=
((
np
.
prod
(
x
.
shape
[:
-
1
])
.
astype
(
np
.
int64
),)
+
((
x
.
shape
[
-
1
]
+
abs
(
offset
))
**
2
,))
if
self
.
offset
<=
0
:
# diag in the lower triangle
result_buffer
=
gpuarray
.
zeros
(
result_buffer_shape
,
diag_z
=
z
[
0
][
-
self
.
offset
,
:(
dim
+
self
.
offset
)]
dtype
=
x
.
dtype
,
else
:
# diag in the upper triangle
context
=
x
.
context
)
diag_z
=
z
[
0
][:(
dim
-
self
.
offset
),
self
.
offset
]
diag_z
.
strides
=
(
sum
(
z
[
0
]
.
strides
),)
if
offset
!=
0
:
row_size
=
x
.
shape
[
-
1
]
+
abs
(
offset
)
diag_z
[:]
=
x
[:]
if
offset
>=
0
:
start_flattened_offset
=
abs
(
offset
)
end_flattened_offset
=
row_size
*
abs
(
offset
)
else
:
start_flattened_offset
=
row_size
*
abs
(
offset
)
end_flattened_offset
=
abs
(
offset
)
diag_view
=
result_buffer
[:,
start_flattened_offset
:
-
end_flattened_offset
:
row_size
+
1
]
# print("offset", offset)
# print("buffer shape:", result_buffer.shape)
# print("result_buffer[%d:%d:%d]" % (start_flattened_offset, -end_flattened_offset, row_size + 1), diag_view.shape)
# print("input_shape:", x.shape)
else
:
diag_view
=
result_buffer
[:,
::
x
.
shape
[
-
1
]
+
1
]
diag_view
[:]
=
x
.
reshape
(
diag_view
.
shape
)[:]
result
=
result_buffer
.
reshape
(
result_shape
)
# print(result)
# Fill in final 2 axes with x
if
len
(
x
.
shape
)
>
1
:
# Re-order axes so they correspond to diagonals at axis1, axis2
axes
=
list
(
range
(
len
(
x
.
shape
[:
-
1
])))
last_idx
=
axes
[
-
1
]
axes
=
axes
[:
axis1
]
+
[
last_idx
+
1
]
+
axes
[
axis1
:]
axes
=
axes
[:
axis2
]
+
[
last_idx
+
2
]
+
axes
[
axis2
:]
result
=
result
.
transpose
(
axes
)
z
[
0
]
=
result
def
grad
(
self
,
inputs
,
gout
):
def
grad
(
self
,
inputs
,
gout
):
(
gz
,)
=
gout
(
gz
,)
=
gout
...
...
theano/tensor/basic.py
浏览文件 @
f766415c
...
@@ -6573,9 +6573,10 @@ class AllocDiag(Op):
...
@@ -6573,9 +6573,10 @@ class AllocDiag(Op):
result
=
np
.
zeros
(
result_shape
,
dtype
=
x
.
dtype
)
result
=
np
.
zeros
(
result_shape
,
dtype
=
x
.
dtype
)
# Create slice for diagonal in final 2 axes
# Create slice for diagonal in final 2 axes
idxs
=
np
.
arange
(
x
.
shape
[
-
1
])
diagonal_slice
=
((
len
(
result_shape
)
-
2
)
*
[
slice
(
None
)]
+
diagonal_slice
=
((
len
(
result_shape
)
-
2
)
*
[
slice
(
None
)]
+
[
np
.
arange
(
x
.
shape
[
-
1
])
+
np
.
maximum
(
0
,
-
offset
),
[
idxs
+
np
.
maximum
(
0
,
-
offset
),
np
.
arange
(
x
.
shape
[
-
1
])
+
np
.
maximum
(
0
,
offset
)])
idxs
+
np
.
maximum
(
0
,
offset
)])
# Fill in final 2 axes with x
# Fill in final 2 axes with x
result
[
diagonal_slice
]
=
x
result
[
diagonal_slice
]
=
x
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论