Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7495a50a
提交
7495a50a
authored
5月 11, 2015
作者:
Frédéric Bastien
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2872 from TimSalimans/erfcx_op
Add new scalar and elemwise op erfcx
上级
450365a9
a35ff4eb
显示空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
112 行增加
和
0 行删除
+112
-0
elemwise.py
theano/sandbox/cuda/elemwise.py
+24
-0
opt.py
theano/sandbox/cuda/opt.py
+6
-0
basic_scipy.py
theano/scalar/basic_scipy.py
+35
-0
basic.py
theano/tensor/basic.py
+5
-0
inplace.py
theano/tensor/inplace.py
+5
-0
test_basic.py
theano/tensor/tests/test_basic.py
+37
-0
没有找到文件。
theano/sandbox/cuda/elemwise.py
浏览文件 @
7495a50a
...
@@ -1065,3 +1065,26 @@ class ErfinvGPU(Erfinv):
...
@@ -1065,3 +1065,26 @@ class ErfinvGPU(Erfinv):
return
"
%(z)
s = erfinv(
%(x)
s);"
%
locals
()
return
"
%(z)
s = erfinv(
%(x)
s);"
%
locals
()
erfinv_gpu
=
ErfinvGPU
(
upgrade_to_float_no_complex
,
name
=
'erfinv_gpu'
)
erfinv_gpu
=
ErfinvGPU
(
upgrade_to_float_no_complex
,
name
=
'erfinv_gpu'
)
class
ErfcxGPU
(
Erfinv
):
"""
Provides a c-code implementation of the scaled complementary error function for GPU.
Note: We do not add this c_code to theano.scalar.basic_scipy.Erfcx, as we
currently rely on Nvidia's cublas library to provide the erfcx
c-implementation (which requires different c_headers). As it stands,
theano.scalar.basic_scipy.Erfcx does not have c_code as scipy does not
export the required C function
"""
def
c_headers
(
self
):
return
[
'math_functions.h'
,
'cublas_v2.h'
]
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
x
,
=
inp
z
,
=
out
if
node
.
inputs
[
0
]
.
type
in
complex_types
:
raise
NotImplementedError
(
'type not supported'
,
type
)
return
"
%(z)
s = erfcx(
%(x)
s);"
%
locals
()
erfcx_gpu
=
ErfcxGPU
(
upgrade_to_float_no_complex
,
name
=
'erfcx_gpu'
)
\ No newline at end of file
theano/sandbox/cuda/opt.py
浏览文件 @
7495a50a
...
@@ -50,7 +50,9 @@ from theano.sandbox.cuda.nnet import (
...
@@ -50,7 +50,9 @@ from theano.sandbox.cuda.nnet import (
from
theano.sandbox.cuda.elemwise
import
SupportCodeError
from
theano.sandbox.cuda.elemwise
import
SupportCodeError
from
theano.scalar.basic_scipy
import
Erfinv
from
theano.scalar.basic_scipy
import
Erfinv
from
theano.scalar.basic_scipy
import
Erfcx
from
theano.sandbox.cuda.elemwise
import
erfinv_gpu
from
theano.sandbox.cuda.elemwise
import
erfinv_gpu
from
theano.sandbox.cuda.elemwise
import
erfcx_gpu
from
theano.sandbox.cuda.var
import
CudaNdarrayConstant
from
theano.sandbox.cuda.var
import
CudaNdarrayConstant
from
theano.sandbox.cuda
import
gpu_optimizer
,
register_opt
,
gpu_seqopt
,
GpuOp
from
theano.sandbox.cuda
import
gpu_optimizer
,
register_opt
,
gpu_seqopt
,
GpuOp
from
theano.scan_module
import
scan_utils
,
scan_op
,
scan_opt
from
theano.scan_module
import
scan_utils
,
scan_op
,
scan_opt
...
@@ -239,6 +241,8 @@ def local_gpu_elemwise_0(node):
...
@@ -239,6 +241,8 @@ def local_gpu_elemwise_0(node):
if
isinstance
(
node
.
op
.
scalar_op
,
Erfinv
):
if
isinstance
(
node
.
op
.
scalar_op
,
Erfinv
):
new_op
=
GpuElemwise
(
erfinv_gpu
)
new_op
=
GpuElemwise
(
erfinv_gpu
)
elif
isinstance
(
node
.
op
.
scalar_op
,
Erfcx
):
new_op
=
GpuElemwise
(
erfcx_gpu
)
else
:
else
:
try
:
try
:
new_op
=
GpuElemwise
(
node
.
op
.
scalar_op
)
new_op
=
GpuElemwise
(
node
.
op
.
scalar_op
)
...
@@ -300,6 +304,8 @@ def local_gpu_elemwise_1(node):
...
@@ -300,6 +304,8 @@ def local_gpu_elemwise_1(node):
if
isinstance
(
elemwise_node
.
op
.
scalar_op
,
Erfinv
):
if
isinstance
(
elemwise_node
.
op
.
scalar_op
,
Erfinv
):
new_op
=
GpuElemwise
(
erfinv_gpu
)
new_op
=
GpuElemwise
(
erfinv_gpu
)
elif
isinstance
(
elemwise_node
.
op
.
scalar_op
,
Erfcx
):
new_op
=
GpuElemwise
(
erfcx_gpu
)
else
:
else
:
try
:
try
:
new_op
=
GpuElemwise
(
elemwise_node
.
op
.
scalar_op
)
new_op
=
GpuElemwise
(
elemwise_node
.
op
.
scalar_op
)
...
...
theano/scalar/basic_scipy.py
浏览文件 @
7495a50a
...
@@ -85,6 +85,41 @@ class Erfc(UnaryScalarOp):
...
@@ -85,6 +85,41 @@ class Erfc(UnaryScalarOp):
erfc
=
Erfc
(
upgrade_to_float_no_complex
,
name
=
'erfc'
)
erfc
=
Erfc
(
upgrade_to_float_no_complex
,
name
=
'erfc'
)
class
Erfcx
(
UnaryScalarOp
):
"""
Implements the scaled complementary error function exp(x**2)*erfc(x) in a numerically stable way for large x. This
is useful for calculating things like log(erfc(x)) = log(erfcx(x)) - x ** 2 without causing underflow. Should only
be used if x is known to be large and positive, as using erfcx(x) for large negative x may instead introduce
overflow problems.
Note: This op can still be executed on GPU, despite not having c_code. When
running on GPU, sandbox.cuda.opt.local_gpu_elemwise_[0,1] replaces this op
with sandbox.cuda.elemwise.ErfcxGPU.
"""
def
impl
(
self
,
x
):
if
imported_scipy_special
:
return
scipy
.
special
.
erfcx
(
x
)
else
:
super
(
Erfcx
,
self
)
.
impl
(
x
)
def
grad
(
self
,
inp
,
grads
):
x
,
=
inp
gz
,
=
grads
if
x
.
type
in
complex_types
:
raise
NotImplementedError
()
if
self
(
x
)
.
type
in
discrete_types
:
if
x
.
type
in
discrete_types
:
return
[
x
.
zeros_like
(
dtype
=
theano
.
config
.
floatX
)]
else
:
return
[
x
.
zeros_like
()]
cst
=
numpy
.
asarray
(
2.
/
numpy
.
sqrt
(
numpy
.
pi
),
dtype
=
upcast
(
x
.
type
.
dtype
,
gz
.
type
.
dtype
))
return
gz
*
(
-
cst
+
(
2.
*
x
)
*
erfcx
(
x
)),
erfcx
=
Erfcx
(
upgrade_to_float_no_complex
,
name
=
'erfcx'
)
class
Erfinv
(
UnaryScalarOp
):
class
Erfinv
(
UnaryScalarOp
):
"""
"""
Implements the inverse error function.
Implements the inverse error function.
...
...
theano/tensor/basic.py
浏览文件 @
7495a50a
...
@@ -1937,6 +1937,11 @@ def erfc(a):
...
@@ -1937,6 +1937,11 @@ def erfc(a):
"""complementary error function"""
"""complementary error function"""
@_scal_elemwise
def
erfcx
(
a
):
"""scaled complementary error function"""
@_scal_elemwise
@_scal_elemwise
def
erfinv
(
a
):
def
erfinv
(
a
):
"""inverse error function"""
"""inverse error function"""
...
...
theano/tensor/inplace.py
浏览文件 @
7495a50a
...
@@ -263,6 +263,11 @@ def erfc_inplace(a):
...
@@ -263,6 +263,11 @@ def erfc_inplace(a):
"""complementary error function"""
"""complementary error function"""
@_scal_inplace
def
erfcx_inplace
(
a
):
"""scaled complementary error function"""
@_scal_inplace
@_scal_inplace
def
gamma_inplace
(
a
):
def
gamma_inplace
(
a
):
"""gamma function"""
"""gamma function"""
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
7495a50a
...
@@ -17,6 +17,7 @@ from nose.plugins.attrib import attr
...
@@ -17,6 +17,7 @@ from nose.plugins.attrib import attr
import
numpy
import
numpy
from
numpy.testing
import
dec
,
assert_array_equal
,
assert_allclose
from
numpy.testing
import
dec
,
assert_array_equal
,
assert_allclose
from
numpy.testing.noseclasses
import
KnownFailureTest
from
numpy.testing.noseclasses
import
KnownFailureTest
from
distutils.version
import
LooseVersion
import
theano
import
theano
from
theano.compat
import
PY3
,
exc_message
,
operator_div
from
theano.compat
import
PY3
,
exc_message
,
operator_div
...
@@ -57,6 +58,7 @@ mode_no_scipy = get_default_mode()
...
@@ -57,6 +58,7 @@ mode_no_scipy = get_default_mode()
try
:
try
:
import
scipy.special
import
scipy.special
import
scipy.stats
import
scipy.stats
from
scipy
import
__version__
as
scipy_version
imported_scipy_special
=
True
imported_scipy_special
=
True
except
ImportError
:
except
ImportError
:
if
config
.
mode
==
"FAST_COMPILE"
:
if
config
.
mode
==
"FAST_COMPILE"
:
...
@@ -1080,6 +1082,11 @@ _good_broadcast_unary_normal_float_no_complex = copymod(
...
@@ -1080,6 +1082,11 @@ _good_broadcast_unary_normal_float_no_complex = copymod(
_good_broadcast_unary_normal_float
,
_good_broadcast_unary_normal_float
,
without
=
[
'complex'
])
without
=
[
'complex'
])
_good_broadcast_unary_normal_float_no_complex_small_neg_range
=
dict
(
normal
=
[
rand_ranged
(
-
2
,
5
,
(
2
,
3
))],
corner_case
=
[
corner_case
],
empty
=
[
numpy
.
asarray
([],
dtype
=
config
.
floatX
)])
_good_broadcast_unary_normal
=
dict
(
_good_broadcast_unary_normal
=
dict
(
normal
=
[
numpy
.
asarray
(
rand_ranged
(
-
5
,
5
,
(
2
,
3
)),
normal
=
[
numpy
.
asarray
(
rand_ranged
(
-
5
,
5
,
(
2
,
3
)),
dtype
=
config
.
floatX
)],
dtype
=
config
.
floatX
)],
...
@@ -1109,6 +1116,10 @@ _grad_broadcast_unary_normal = dict(
...
@@ -1109,6 +1116,10 @@ _grad_broadcast_unary_normal = dict(
# empty = [numpy.asarray([])] # XXX: should this be included?
# empty = [numpy.asarray([])] # XXX: should this be included?
)
)
_grad_broadcast_unary_normal_small_neg_range
=
dict
(
normal
=
[
numpy
.
asarray
(
rand_ranged
(
-
2
,
5
,
(
2
,
3
)),
dtype
=
floatX
)],
corner_case
=
[
corner_case_grad
])
_grad_broadcast_unary_normal_no_complex_no_corner_case
=
copymod
(
_grad_broadcast_unary_normal_no_complex_no_corner_case
=
copymod
(
_grad_broadcast_unary_normal_no_complex
,
_grad_broadcast_unary_normal_no_complex
,
without
=
[
'corner_case'
])
without
=
[
'corner_case'
])
...
@@ -1650,9 +1661,16 @@ if imported_scipy_special:
...
@@ -1650,9 +1661,16 @@ if imported_scipy_special:
expected_psi
=
scipy
.
special
.
psi
expected_psi
=
scipy
.
special
.
psi
expected_chi2sf
=
lambda
x
,
df
:
scipy
.
stats
.
chi2
.
sf
(
x
,
df
)
.
astype
(
x
.
dtype
)
expected_chi2sf
=
lambda
x
,
df
:
scipy
.
stats
.
chi2
.
sf
(
x
,
df
)
.
astype
(
x
.
dtype
)
skip_scipy
=
False
skip_scipy
=
False
if
LooseVersion
(
scipy_version
)
>=
LooseVersion
(
"0.12.0"
):
expected_erfcx
=
scipy
.
special
.
erfcx
skip_scipy12
=
False
else
:
expected_erfcx
=
[]
skip_scipy12
=
"the erfcx op requires scipy version >= 0.12, installed version is "
+
scipy_version
else
:
else
:
expected_erf
=
[]
expected_erf
=
[]
expected_erfc
=
[]
expected_erfc
=
[]
expected_erfcx
=
[]
expected_erfinv
=
[]
expected_erfinv
=
[]
expected_erfcinv
=
[]
expected_erfcinv
=
[]
expected_gamma
=
[]
expected_gamma
=
[]
...
@@ -1660,6 +1678,7 @@ else:
...
@@ -1660,6 +1678,7 @@ else:
expected_psi
=
[]
expected_psi
=
[]
expected_chi2sf
=
[]
expected_chi2sf
=
[]
skip_scipy
=
"scipy is not present"
skip_scipy
=
"scipy is not present"
skip_scipy12
=
"scipy is not present"
ErfTester
=
makeBroadcastTester
(
ErfTester
=
makeBroadcastTester
(
op
=
tensor
.
erf
,
op
=
tensor
.
erf
,
...
@@ -1697,6 +1716,24 @@ ErfcInplaceTester = makeBroadcastTester(
...
@@ -1697,6 +1716,24 @@ ErfcInplaceTester = makeBroadcastTester(
inplace
=
True
,
inplace
=
True
,
skip
=
skip_scipy
)
skip
=
skip_scipy
)
ErfcxTester
=
makeBroadcastTester
(
op
=
tensor
.
erfcx
,
expected
=
expected_erfcx
,
good
=
_good_broadcast_unary_normal_float_no_complex_small_neg_range
,
grad
=
_grad_broadcast_unary_normal_small_neg_range
,
eps
=
2e-10
,
mode
=
mode_no_scipy
,
skip
=
skip_scipy12
)
ErfcxInplaceTester
=
makeBroadcastTester
(
op
=
inplace
.
erfcx_inplace
,
expected
=
expected_erfcx
,
good
=
_good_broadcast_unary_normal_float_no_complex_small_neg_range
,
grad
=
_grad_broadcast_unary_normal_small_neg_range
,
eps
=
2e-10
,
mode
=
mode_no_scipy
,
inplace
=
True
,
skip
=
skip_scipy12
)
ErfinvTester
=
makeBroadcastTester
(
ErfinvTester
=
makeBroadcastTester
(
op
=
tensor
.
erfinv
,
op
=
tensor
.
erfinv
,
expected
=
expected_erfinv
,
expected
=
expected_erfinv
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论