Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
80d8bf27
提交
80d8bf27
authored
6月 08, 2017
作者:
João Victor Tozatti Risso
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor CTC GPU wrapper to make use of L_op and allocate costs and gradients in make_node
Signed-off-by:
João Victor Tozatti Risso
<
joaovictor.risso@gmail.com
>
上级
ba6d2de5
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
52 行增加
和
51 行删除
+52
-51
ctc.py
theano/gpuarray/ctc.py
+52
-51
没有找到文件。
theano/gpuarray/ctc.py
浏览文件 @
80d8bf27
from
__future__
import
absolute_import
,
print_function
,
division
import
theano
from
theano
import
Op
from
theano
import
config
from
theano
import
(
config
,
gof
)
import
theano.tensor
as
T
from
.basic_ops
import
(
gpu_contiguous
,
as_gpuarray_variable
,
infer_context_name
,
CGpuKernelBase
)
from
.basic_ops
import
(
gpu_contiguous
,
as_gpuarray_variable
,
infer_context_name
)
import
theano.tensor.nnet.ctc
from
.type
import
GpuArrayType
from
.type
import
(
GpuArrayType
,
gpu_context_type
)
from
.elemwise
import
GpuDimShuffle
from
theano.gradient
import
grad_undefined
from
theano.gof
import
local_optimizer
...
...
@@ -20,32 +18,44 @@ import pygpu
ctc_enabled
=
config
.
ctc
.
enabled
class
GpuConnectionistTemporalClassification
(
CGpuKernelBase
,
Op
):
class
GpuConnectionistTemporalClassification
(
gof
.
C
Op
):
"""
GPU wrapper for Baidu CTC loss function.
Parameters
----------
activations
Three-dimensional tensor, which has a shape of (t, m, p), where
t is the time index, m is the minibatch index, and p is the index
over the probabilities of each symbol in the alphabet. The memory
layout is assumed to be in C-order, which consists in the slowest
to the fastest changing dimension, from left to right. In this case,
p is the fastest changing dimension.
labels
A 1-D tensor of all the labels for the minibatch.
input_lengths
A 1-D tensor with the number of time steps for each sequence in
the minibatch.
Returns
-------
3D tensor
Cost of each example in the minibatch. Tensor is of shape
(time index, minibatch index, probabilities).
"""
__props__
=
(
'co
ntext_name'
,
'co
mpute_grad'
,)
__props__
=
(
'compute_grad'
,)
func_file
=
"./ctc_wrapper.c"
func_name
=
"APPLY_SPECIFIC(ctc_cost_gpu)"
def
__init__
(
self
,
compute_grad
=
True
,
context_name
=
None
):
params_type
=
gpu_context_type
def
__init__
(
self
,
compute_grad
=
True
):
if
not
compute_grad
:
self
.
func_name
=
"APPLY_SPECIFIC(ctc_cost_gpu_no_grad)"
self
.
compute_grad
=
compute_grad
self
.
context_name
=
context_name
Op
.
__init__
(
self
)
CGpuKernelBase
.
__init__
(
self
,
self
.
func_file
,
self
.
func_name
)
self
.
costs
=
GpuArrayType
(
dtype
=
'float32'
,
broadcastable
=
(
False
,),
context_name
=
self
.
context_name
)
if
self
.
compute_grad
:
self
.
gradients
=
GpuArrayType
(
dtype
=
'float32'
,
broadcastable
=
(
False
,
False
,
False
,),
context_name
=
self
.
context_name
)
gof
.
COp
.
__init__
(
self
,
self
.
func_file
,
self
.
func_name
)
if
config
.
ctc
.
root
==
""
:
raise
ValueError
(
'ctc.root variable is not set, please set it '
...
...
@@ -75,35 +85,19 @@ class GpuConnectionistTemporalClassification(CGpuKernelBase, Op):
return
[
'ctc.h'
,
'numpy_compat.h'
,
'gpuarray_helper.h'
,
'gpuarray/types.h'
,
'gpuarray_api.h'
,
'gpuarray/array.h'
,
'gpuarray/util.h'
]
def
make_node
(
self
,
activations
,
labels
,
input_lengths
):
"""
Parameters
----------
activations
Three-dimensional tensor, which has a shape of (t, m, p), where
t is the time index, m is the minibatch index, and p is the index
over the probabilities of each symbol in the alphabet. The memory
layout is assumed to be in C-order, which consists in the slowest
to the fastest changing dimension, from left to right. In this case,
p is the fastest changing dimension.
labels
A 1-D tensor of all the labels for the minibatch.
input_lengths
A 1-D tensor with the number of time steps for each sequence in
the minibatch.
"""
def
get_params
(
self
,
node
):
return
node
.
inputs
[
0
]
.
type
.
context
def
make_node
(
self
,
activations
,
labels
,
input_lengths
):
if
not
ctc_enabled
:
raise
RuntimeError
(
'Baidu CTC is not enabled and '
'GpuConnectionistTemporalClassification Op '
'can not be constructed.'
)
context
=
infer_context_name
(
activations
,
labels
,
input_lengths
)
assert
context
==
self
.
context_name
t_activations
=
as_gpuarray_variable
(
activations
,
context_name
=
self
.
context_name
)
context_name
=
context
)
# Ensure activations array is C-contiguous
t_activations
=
gpu_contiguous
(
t_activations
)
...
...
@@ -123,24 +117,31 @@ class GpuConnectionistTemporalClassification(CGpuKernelBase, Op):
# Return only the cost. Gradient will be returned by grad()
self
.
default_output
=
0
out_params
=
[
as_gpuarray_variable
(
self
.
costs
(),
context_name
=
self
.
context_name
)]
if
self
.
gradients
is
not
None
:
out_params
.
append
(
as_gpuarray_variable
(
self
.
gradients
(),
context_name
=
self
.
context_name
))
costs
=
GpuArrayType
(
dtype
=
'float32'
,
broadcastable
=
(
False
,),
context_name
=
context
)()
if
self
.
compute_grad
:
gradients
=
GpuArrayType
(
dtype
=
'float32'
,
broadcastable
=
(
False
,
False
,
False
,),
context_name
=
context
)()
return
theano
.
Apply
(
self
,
inputs
=
[
t_activations
,
t_labels
,
t_input_lengths
],
outputs
=
out_params
)
outputs
=
[
costs
,
gradients
]
)
def
grad
(
self
,
in
puts
,
output_grads
):
def
L_op
(
self
,
inputs
,
out
puts
,
output_grads
):
if
not
ctc_enabled
:
raise
RuntimeError
(
'Baidu CTC is not enabled and '
'GpuConnectionistTemporalClassification Op '
'can not be constructed.'
)
z
=
output_grads
[
0
]
grad_shuffle
=
GpuDimShuffle
(
input_broadcastable
=
(
False
,
False
,
False
),
new_order
=
(
1
,
0
,
2
))(
self
.
gradients
())
grad_bdot
=
T
.
basic
.
batched_dot
(
z
,
grad_shuffle
)
grad_shuffle_reverse
=
GpuDimShuffle
(
input_broadcastable
=
(
False
,
False
,
False
),
# Gradients computed by Op
gradients
=
outputs
[
1
]
# Gradients of original function, to compose chain rule
grad_op
=
output_grads
[
0
]
grad_shuffle
=
GpuDimShuffle
(
input_broadcastable
=
(
False
,
False
,
False
,),
new_order
=
(
1
,
0
,
2
))(
gradients
)
grad_bdot
=
T
.
basic
.
batched_dot
(
grad_op
,
grad_shuffle
)
grad_shuffle_reverse
=
GpuDimShuffle
(
input_broadcastable
=
(
False
,
False
,
False
,),
new_order
=
(
1
,
0
,
2
))(
grad_bdot
)
return
[
grad_shuffle_reverse
,
grad_undefined
(
self
,
1
,
inputs
[
1
]),
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论