Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
94fb4d03
提交
94fb4d03
authored
6月 23, 2014
作者:
Arnaud Bergeron
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add tests for grad shape.
Some improvements to the gradient, but it's still transposed and partially wrong.
上级
bcb902e9
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
27 行增加
和
22 行删除
+27
-22
blocksparse.py
theano/sandbox/cuda/blocksparse.py
+12
-11
test_blocksparse.py
theano/sandbox/cuda/tests/test_blocksparse.py
+15
-11
没有找到文件。
theano/sandbox/cuda/blocksparse.py
浏览文件 @
94fb4d03
...
@@ -69,7 +69,7 @@ def bptr(a):
...
@@ -69,7 +69,7 @@ def bptr(a):
dtype
=
cublas
.
ctypes
.
c_void_p
)
dtype
=
cublas
.
ctypes
.
c_void_p
)
class
SparseBlockGemv
D
S
(
GpuOp
):
class
SparseBlockGemv
S
S
(
GpuOp
):
def
__init__
(
self
,
inplace
):
def
__init__
(
self
,
inplace
):
self
.
inplace
=
inplace
self
.
inplace
=
inplace
if
self
.
inplace
:
if
self
.
inplace
:
...
@@ -112,9 +112,10 @@ class SparseBlockGemvDS(GpuOp):
...
@@ -112,9 +112,10 @@ class SparseBlockGemvDS(GpuOp):
go
=
grads
[
0
]
go
=
grads
[
0
]
# might revise that interface to not have a huge output
# might revise that interface to not have a huge output
Wgrad
=
sparse_block_outer_ss
(
W
.
zeros_like
()
.
dimshuffle
((
1
,
0
,
3
,
2
)),
Wgrad
=
sparse_block_outer_ss
(
W
.
zeros_like
(),
go
,
h
,
outputIdx
,
inputIdx
)
h
,
go
,
inputIdx
,
outputIdx
)
hgrad
=
sparse_block_gemv_ds
(
h
.
zeros_like
(),
W
.
dimshuffle
((
1
,
0
,
3
,
2
)),
hgrad
=
sparse_block_gemv_ss
(
h
.
zeros_like
(),
W
.
dimshuffle
((
1
,
0
,
3
,
2
)),
go
,
go
,
outputIdx
,
inputIdx
)
outputIdx
,
inputIdx
)
return
[
go
,
Wgrad
,
hgrad
,
return
[
go
,
Wgrad
,
hgrad
,
...
@@ -124,7 +125,7 @@ class SparseBlockGemvDS(GpuOp):
...
@@ -124,7 +125,7 @@ class SparseBlockGemvDS(GpuOp):
"grad of outputIdx makes no sense"
)]
"grad of outputIdx makes no sense"
)]
sparse_block_gemv_
ds
=
SparseBlockGemvD
S
(
False
)
sparse_block_gemv_
ss
=
SparseBlockGemvS
S
(
False
)
class
SparseBlockOuterSS
(
GpuOp
):
class
SparseBlockOuterSS
(
GpuOp
):
...
@@ -145,10 +146,10 @@ class SparseBlockOuterSS(GpuOp):
...
@@ -145,10 +146,10 @@ class SparseBlockOuterSS(GpuOp):
if
not
self
.
inplace
:
if
not
self
.
inplace
:
o
=
o
.
copy
()
o
=
o
.
copy
()
for
i
in
range
(
x
.
shape
[
0
]):
for
j
in
range
(
y
.
shape
[
0
]):
inp_id
=
xIdx
[
i
]
out_id
=
yIdx
[
j
]
for
j
in
range
(
y
.
shape
[
0
]):
for
i
in
range
(
x
.
shape
[
0
]):
out_id
=
yIdx
[
j
]
inp_id
=
xIdx
[
i
]
ger
(
numpy
.
float32
(
1.0
),
x
[
i
],
ger
(
numpy
.
float32
(
1.0
),
x
[
i
],
y
[
j
],
o
[
inp_id
,
out_id
])
y
[
j
],
o
[
inp_id
,
out_id
])
...
@@ -161,7 +162,7 @@ sparse_block_outer_ss = SparseBlockOuterSS()
...
@@ -161,7 +162,7 @@ sparse_block_outer_ss = SparseBlockOuterSS()
# All code above this line is unused (except for the imports)
# All code above this line is unused (except for the imports)
def
sparse_block_dot_
D
S
(
W
,
h
,
inputIdx
,
b
,
outputIdx
):
def
sparse_block_dot_
S
S
(
W
,
h
,
inputIdx
,
b
,
outputIdx
):
"""
"""
var: shape, comment
var: shape, comment
W: (iBlocks, oBlocks, iSize, oSize), weight matrix
W: (iBlocks, oBlocks, iSize, oSize), weight matrix
...
@@ -171,7 +172,7 @@ def sparse_block_dot_DS(W, h, inputIdx, b, outputIdx):
...
@@ -171,7 +172,7 @@ def sparse_block_dot_DS(W, h, inputIdx, b, outputIdx):
outputIdx: (oWin,), indexes of the output blocks
outputIdx: (oWin,), indexes of the output blocks
returns (oBlocks, oSize), dot(W[i, j], h[i]) + b[j]
returns (oBlocks, oSize), dot(W[i, j], h[i]) + b[j]
but b[j] is only added once
but b[j] is only added once
"""
"""
o
=
b
.
take
(
outputIdx
,
axis
=
0
)
o
=
b
.
take
(
outputIdx
,
axis
=
0
)
def
outer_fn
(
out_id
,
W
,
h
,
b
,
iIdx
):
def
outer_fn
(
out_id
,
W
,
h
,
b
,
iIdx
):
...
...
theano/sandbox/cuda/tests/test_blocksparse.py
浏览文件 @
94fb4d03
...
@@ -5,8 +5,8 @@ import theano.tests.unittest_tools as utt
...
@@ -5,8 +5,8 @@ import theano.tests.unittest_tools as utt
import
numpy
import
numpy
from
numpy.random
import
randn
from
numpy.random
import
randn
from
theano.sandbox.cuda.blocksparse
import
(
sparse_block_dot_
D
S
,
from
theano.sandbox.cuda.blocksparse
import
(
sparse_block_dot_
S
S
,
sparse_block_gemv_
d
s
)
sparse_block_gemv_
s
s
)
def
blocksparse_data
():
def
blocksparse_data
():
nInputBlock
=
128
nInputBlock
=
128
...
@@ -45,7 +45,7 @@ def test_blocksparse():
...
@@ -45,7 +45,7 @@ def test_blocksparse():
iIdx
=
tensor
.
lvector
()
iIdx
=
tensor
.
lvector
()
oIdx
=
tensor
.
lvector
()
oIdx
=
tensor
.
lvector
()
o
=
sparse_block_dot_
D
S
(
W
,
h
,
iIdx
,
b
,
oIdx
)
o
=
sparse_block_dot_
S
S
(
W
,
h
,
iIdx
,
b
,
oIdx
)
f
=
theano
.
function
([
W
,
h
,
iIdx
,
b
,
oIdx
],
o
)
f
=
theano
.
function
([
W
,
h
,
iIdx
,
b
,
oIdx
],
o
)
...
@@ -64,7 +64,7 @@ def test_blocksparse_op():
...
@@ -64,7 +64,7 @@ def test_blocksparse_op():
iIdx
=
tensor
.
lvector
()
iIdx
=
tensor
.
lvector
()
oIdx
=
tensor
.
lvector
()
oIdx
=
tensor
.
lvector
()
o
=
sparse_block_gemv_
d
s
(
b
.
take
(
oIdx
,
axis
=
0
),
W
,
h
,
iIdx
,
oIdx
)
o
=
sparse_block_gemv_
s
s
(
b
.
take
(
oIdx
,
axis
=
0
),
W
,
h
,
iIdx
,
oIdx
)
f
=
theano
.
function
([
W
,
h
,
iIdx
,
b
,
oIdx
],
o
)
f
=
theano
.
function
([
W
,
h
,
iIdx
,
b
,
oIdx
],
o
)
...
@@ -80,16 +80,16 @@ def test_blocksparse_op_grad():
...
@@ -80,16 +80,16 @@ def test_blocksparse_op_grad():
h_val
=
randn
(
2
,
3
)
.
astype
(
'float32'
)
h_val
=
randn
(
2
,
3
)
.
astype
(
'float32'
)
iIdx_val
=
numpy
.
random
.
permutation
(
3
)[:
2
]
iIdx_val
=
numpy
.
random
.
permutation
(
3
)[:
2
]
oIdx_val
=
numpy
.
random
.
permutation
(
3
)[:
2
]
oIdx_val
=
numpy
.
random
.
permutation
(
3
)[:
2
]
W_val
=
randn
(
3
,
3
,
3
,
3
)
.
astype
(
'float32'
)
W_val
=
randn
(
3
,
3
,
4
,
3
)
.
astype
(
'float32'
)
b_val
=
randn
(
3
,
3
)
.
astype
(
'float32'
)
b_val
=
randn
(
3
,
4
)
.
astype
(
'float32'
)
iIdx
=
theano
.
tensor
.
constant
(
iIdx_val
)
iIdx
=
theano
.
tensor
.
constant
(
iIdx_val
)
oIdx
=
theano
.
tensor
.
constant
(
oIdx_val
)
oIdx
=
theano
.
tensor
.
constant
(
oIdx_val
)
def
f
(
b
,
W
,
h
):
def
f
(
b
,
h
,
W
):
return
sparse_block_gemv_
d
s
(
b
.
take
(
oIdx
,
axis
=
0
),
W
,
h
,
iIdx
,
oIdx
)
return
sparse_block_gemv_
s
s
(
b
.
take
(
oIdx
,
axis
=
0
),
W
,
h
,
iIdx
,
oIdx
)
utt
.
verify_grad
(
f
,
[
b_val
,
W_val
,
h
_val
])
utt
.
verify_grad
(
f
,
[
b_val
,
h_val
,
W
_val
])
def
test_blocksparse_op_grad2
():
def
test_blocksparse_op_grad2
():
...
@@ -99,7 +99,7 @@ def test_blocksparse_op_grad2():
...
@@ -99,7 +99,7 @@ def test_blocksparse_op_grad2():
iIdx
=
tensor
.
lvector
()
iIdx
=
tensor
.
lvector
()
oIdx
=
tensor
.
lvector
()
oIdx
=
tensor
.
lvector
()
o
=
sparse_block_gemv_
d
s
(
b
.
take
(
oIdx
,
axis
=
0
),
W
,
h
,
iIdx
,
oIdx
)
o
=
sparse_block_gemv_
s
s
(
b
.
take
(
oIdx
,
axis
=
0
),
W
,
h
,
iIdx
,
oIdx
)
go
=
theano
.
grad
(
o
.
sum
(),
[
b
,
W
,
h
])
go
=
theano
.
grad
(
o
.
sum
(),
[
b
,
W
,
h
])
f
=
theano
.
function
([
W
,
h
,
iIdx
,
b
,
oIdx
],
go
)
f
=
theano
.
function
([
W
,
h
,
iIdx
,
b
,
oIdx
],
go
)
...
@@ -107,4 +107,8 @@ def test_blocksparse_op_grad2():
...
@@ -107,4 +107,8 @@ def test_blocksparse_op_grad2():
W_val
,
h_val
,
iIdx_val
,
b_val
,
oIdx_val
=
blocksparse_data
()
W_val
,
h_val
,
iIdx_val
,
b_val
,
oIdx_val
=
blocksparse_data
()
# just make sure that it runs correcly and all the shapes are ok.
# just make sure that it runs correcly and all the shapes are ok.
f
(
W_val
,
h_val
,
iIdx_val
,
b_val
,
oIdx_val
)
b_g
,
W_g
,
h_g
=
f
(
W_val
,
h_val
,
iIdx_val
,
b_val
,
oIdx_val
)
assert
b_g
.
shape
==
b_val
.
shape
assert
h_g
.
shape
==
h_val
.
shape
assert
W_g
.
shape
==
W_val
.
shape
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论