Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
3770c2a7
提交
3770c2a7
authored
6月 28, 2017
作者:
Aleksandar Botev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added the tri-gamma function.
上级
d9263f62
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
128 行增加
和
2 行删除
+128
-2
basic_scipy.py
theano/scalar/basic_scipy.py
+100
-2
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
+18
-0
没有找到文件。
theano/scalar/basic_scipy.py
浏览文件 @
3770c2a7
...
@@ -298,8 +298,18 @@ class Psi(UnaryScalarOp):
...
@@ -298,8 +298,18 @@ class Psi(UnaryScalarOp):
else
:
else
:
super
(
Psi
,
self
)
.
impl
(
x
)
super
(
Psi
,
self
)
.
impl
(
x
)
def
grad
(
self
,
inputs
,
outputs_gradients
):
def
L_op
(
self
,
inputs
,
outputs
,
grads
):
raise
NotImplementedError
()
x
,
=
inputs
gz
,
=
grads
if
x
.
type
in
complex_types
:
raise
NotImplementedError
()
if
outputs
[
0
]
.
type
in
discrete_types
:
if
x
.
type
in
discrete_types
:
return
[
x
.
zeros_like
(
dtype
=
theano
.
config
.
floatX
)]
else
:
return
[
x
.
zeros_like
()]
return
[
gz
*
tri_gamma
(
x
)]
def
c_support_code
(
self
):
def
c_support_code
(
self
):
return
(
return
(
...
@@ -365,6 +375,94 @@ class Psi(UnaryScalarOp):
...
@@ -365,6 +375,94 @@ class Psi(UnaryScalarOp):
psi
=
Psi
(
upgrade_to_float
,
name
=
'psi'
)
psi
=
Psi
(
upgrade_to_float
,
name
=
'psi'
)
class
TriGamma
(
UnaryScalarOp
):
"""
Second derivative of log gamma function.
"""
@staticmethod
def
st_impl
(
x
):
return
scipy
.
special
.
polygamma
(
1
,
x
)
def
impl
(
self
,
x
):
if
imported_scipy_special
:
return
TriGamma
.
st_impl
(
x
)
else
:
super
(
TriGamma
,
self
)
.
impl
(
x
)
def
grad
(
self
,
inputs
,
outputs_gradients
):
raise
NotImplementedError
()
def
c_support_code
(
self
):
# The implementation has been copied from
# http://people.sc.fsu.edu/~jburkardt/cpp_src/asa121/asa121.html
return
(
"""
// For GPU support
#ifdef WITHIN_KERNEL
#define DEVICE WITHIN_KERNEL
#else
#define DEVICE
#endif
#ifndef ga_double
#define ga_double double
#endif
#ifndef _TRIGAMMAFUNCDEFINED
#define _TRIGAMMAFUNCDEFINED
DEVICE double _tri_gamma(ga_double x) {
double a = 0.0001;
double b = 5.0;
double b2 = 0.1666666667;
double b4 = -0.03333333333;
double b6 = 0.02380952381;
double b8 = -0.03333333333;
double value;
double y;
double z;
if (x <= 0) {
return 0.0;
}
if ( x <= a ) {
value = 1.0 / x / x;
return value;
}
value = 0.0;
z = x;
while ( z < b ) {
value += 1.0 / z / z;
z += 1.0;
}
y = 1.0 / z / z;
value += 0.5 * y + (1.0 + y * (b2 + y * (b4 + y * (b6 + y * b8 )))) / z;
return value;
}
#endif
"""
)
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
x
,
=
inp
z
,
=
out
if
node
.
inputs
[
0
]
.
type
in
float_types
:
return
"""
%(z)
s =
_tri_gamma(
%(x)
s);"""
%
locals
()
raise
NotImplementedError
(
'only floating point is implemented'
)
tri_gamma
=
TriGamma
(
upgrade_to_float
,
name
=
'tri_gamma'
)
class
Chi2SF
(
BinaryScalarOp
):
class
Chi2SF
(
BinaryScalarOp
):
"""
"""
Compute (1 - chi2_cdf(x)) ie. chi2 pvalue (chi2 'survival function').
Compute (1 - chi2_cdf(x)) ie. chi2 pvalue (chi2 'survival function').
...
...
theano/tensor/basic.py
浏览文件 @
3770c2a7
...
@@ -2253,6 +2253,11 @@ def psi(a):
...
@@ -2253,6 +2253,11 @@ def psi(a):
"""derivative of log gamma function"""
"""derivative of log gamma function"""
@_scal_elemwise
def
tri_gamma
(
a
):
"""second derivative of the log gamma function"""
@_scal_elemwise
@_scal_elemwise
def
chi2sf
(
x
,
k
):
def
chi2sf
(
x
,
k
):
"""chi squared survival function"""
"""chi squared survival function"""
...
...
theano/tensor/inplace.py
浏览文件 @
3770c2a7
...
@@ -275,6 +275,11 @@ def psi_inplace(a):
...
@@ -275,6 +275,11 @@ def psi_inplace(a):
"""derivative of log gamma function"""
"""derivative of log gamma function"""
@_scal_inplace
def
tri_gamma_inplace
(
a
):
"""second derivative of the log gamma function"""
@_scal_inplace
@_scal_inplace
def
chi2sf_inplace
(
x
,
k
):
def
chi2sf_inplace
(
x
,
k
):
"""chi squared survival function"""
"""chi squared survival function"""
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
3770c2a7
...
@@ -1706,6 +1706,7 @@ if imported_scipy_special:
...
@@ -1706,6 +1706,7 @@ if imported_scipy_special:
expected_gamma
=
scipy
.
special
.
gamma
expected_gamma
=
scipy
.
special
.
gamma
expected_gammaln
=
scipy
.
special
.
gammaln
expected_gammaln
=
scipy
.
special
.
gammaln
expected_psi
=
scipy
.
special
.
psi
expected_psi
=
scipy
.
special
.
psi
expected_tri_gamma
=
partial
(
scipy
.
special
.
polygamma
,
1
)
expected_chi2sf
=
scipy
.
stats
.
chi2
.
sf
expected_chi2sf
=
scipy
.
stats
.
chi2
.
sf
expected_j0
=
scipy
.
special
.
j0
expected_j0
=
scipy
.
special
.
j0
expected_j1
=
scipy
.
special
.
j1
expected_j1
=
scipy
.
special
.
j1
...
@@ -1870,6 +1871,23 @@ PsiInplaceTester = makeBroadcastTester(
...
@@ -1870,6 +1871,23 @@ PsiInplaceTester = makeBroadcastTester(
inplace
=
True
,
inplace
=
True
,
skip
=
skip_scipy
)
skip
=
skip_scipy
)
_good_broadcast_unary_tri_gamma
=
_good_broadcast_unary_psi
TriGammaTester
=
makeBroadcastTester
(
op
=
tensor
.
tri_gamma
,
expected
=
expected_tri_gamma
,
good
=
_good_broadcast_unary_psi
,
eps
=
2e-8
,
mode
=
mode_no_scipy
,
skip
=
skip_scipy
)
TriGammaInplaceTester
=
makeBroadcastTester
(
op
=
inplace
.
tri_gamma_inplace
,
expected
=
expected_tri_gamma
,
good
=
_good_broadcast_unary_tri_gamma
,
eps
=
2e-8
,
mode
=
mode_no_scipy
,
inplace
=
True
,
skip
=
skip_scipy
)
# chi2sf takes two inputs, a value (x) and a degrees of freedom (k).
# chi2sf takes two inputs, a value (x) and a degrees of freedom (k).
# not sure how to deal with that here...
# not sure how to deal with that here...
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论