Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
60a89d0b
提交
60a89d0b
authored
2月 16, 2010
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Moved sigmoid() and softmax() to new file, added some related optimizations and tests.
上级
525000a6
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
78 行增加
和
87 行删除
+78
-87
__init__.py
theano/tensor/nnet/__init__.py
+1
-0
nnet.py
theano/tensor/nnet/nnet.py
+2
-87
sigm.py
theano/tensor/nnet/sigm.py
+0
-0
test_sigm.py
theano/tensor/nnet/tests/test_sigm.py
+75
-0
没有找到文件。
theano/tensor/nnet/__init__.py
浏览文件 @
60a89d0b
from
nnet
import
*
from
sigm
import
softplus
,
sigmoid
,
sigmoid_inplace
,
scalar_sigmoid
theano/tensor/nnet/nnet.py
浏览文件 @
60a89d0b
...
...
@@ -4,89 +4,14 @@
"""
from
theano
import
gof
from
theano
import
scalar
from
theano
import
printing
from
theano.printing
import
pprint
from
theano.tensor
import
basic
as
tensor
from
theano.tensor
import
elemwise
from
theano.tensor
import
opt
from
theano.compile
import
optdb
import
numpy
############
#
# SCALAR OPS
#
class
ScalarSigmoid
(
scalar
.
UnaryScalarOp
):
@staticmethod
def
st_impl
(
x
):
if
x
<
-
30.0
:
return
0.0
if
x
>
30.0
:
return
1.0
return
1.0
/
(
1.0
+
numpy
.
exp
(
-
x
))
def
impl
(
self
,
x
):
return
ScalarSigmoid
.
st_impl
(
x
)
def
grad
(
self
,
(
x
,),
(
gz
,)):
y
=
scalar_sigmoid
(
x
)
return
[
gz
*
y
*
(
1.0
-
y
)]
def
c_code
(
self
,
node
,
name
,
(
x
,),
(
z
,),
sub
):
if
node
.
inputs
[
0
]
.
type
==
scalar
.
float32
:
# These constants were obtained by looking at the output of python commands like:
# for i in xrange(750):
# print i, repr( theano._asarray(1.0, dtype=dt) / (theano._asarray(1.0, dtype=dt) + numpy.exp(-theano._asarray([i,-i], dtype=dt))))
# the boundary checks prevent us from generating inf
return
"""
%(z)
s =
%(x)
s < -88.0f ? 0.0 :
%(x)
s > 15.0f ? 1.0f : 1.0f /(1.0f + exp(-
%(x)
s));"""
%
locals
()
elif
node
.
inputs
[
0
]
.
type
==
scalar
.
float64
:
return
"""
%(z)
s =
%(x)
s < -709.0 ? 0.0 :
%(x)
s > 19.0 ? 1.0 : 1.0 /(1.0+exp(-
%(x)
s));"""
%
locals
()
else
:
raise
NotImplementedError
(
'only floatingpoint is implemented'
)
def
c_code_cache_version
(
self
):
v
=
super
(
ScalarSigmoid
,
self
)
.
c_code_cache_version
()
if
v
:
return
(
2
,)
+
v
else
:
return
v
scalar_sigmoid
=
ScalarSigmoid
(
scalar
.
upgrade_to_float
,
name
=
'scalar_sigmoid'
)
sigmoid
=
elemwise
.
Elemwise
(
scalar_sigmoid
,
name
=
'sigmoid'
)
pprint
.
assign
(
sigmoid
,
printing
.
FunctionPrinter
(
'sigmoid'
))
class
ScalarSoftplus
(
scalar
.
UnaryScalarOp
):
@staticmethod
def
static_impl
(
x
):
if
x
<
-
30.0
:
return
0.0
if
x
>
30.0
:
return
x
return
numpy
.
log1p
(
numpy
.
exp
(
x
))
def
impl
(
self
,
x
):
return
ScalarSoftplus
.
static_impl
(
x
)
def
grad
(
self
,
(
x
,),
(
gz
,)):
return
[
gz
*
scalar_sigmoid
(
x
)]
def
c_code
(
self
,
node
,
name
,
(
x
,),
(
z
,),
sub
):
if
node
.
inputs
[
0
]
.
type
==
scalar
.
float32
:
# These constants were obtained by looking at the output of python commands like:
# for i in xrange(750):
# print i, repr( numpy.log1p(numpy.exp(theano._asarray([i,-i], dtype=dt))))
# the boundary checks prevent us from generating inf
return
"""
%(z)
s =
%(x)
s < -103.0f ? 0.0 :
%(x)
s > 14.0f ?
%(x)
s : log1p(exp(
%(x)
s));"""
%
locals
()
elif
node
.
inputs
[
0
]
.
type
==
scalar
.
float64
:
return
"""
%(z)
s =
%(x)
s < -745.0 ? 0.0 :
%(x)
s > 16.0 ?
%(x)
s : log1p(exp(
%(x)
s));"""
%
locals
()
else
:
raise
NotImplementedError
(
'only floatingpoint is implemented'
)
def
c_code_cache_version
(
self
):
v
=
super
(
ScalarSoftplus
,
self
)
.
c_code_cache_version
()
if
v
:
return
(
2
,)
+
v
else
:
return
v
scalar_softplus
=
ScalarSoftplus
(
scalar
.
upgrade_to_float
,
name
=
'scalar_softplus'
)
softplus
=
elemwise
.
Elemwise
(
scalar_softplus
,
name
=
'softplus'
)
pprint
.
assign
(
softplus
,
printing
.
FunctionPrinter
(
'softplus'
))
from
.sigm
import
sigmoid
############
...
...
@@ -1351,6 +1276,7 @@ def categorical_crossentropy(coding_dist, true_dist):
raise
TypeError
(
'rank mismatch between coding and true distributions'
)
from
theano
import
scalar
class
Prepend_scalar_constant_to_each_row
(
gof
.
Op
):
def
__init__
(
self
,
val
=
0
):
...
...
@@ -1440,14 +1366,3 @@ prepend_scalar_to_each_row = Prepend_scalar_to_each_row()
prepend_0_to_each_row
=
Prepend_scalar_constant_to_each_row
(
0.
)
prepend_1_to_each_row
=
Prepend_scalar_constant_to_each_row
(
1.
)
logsigm_to_softplus
=
gof
.
PatternSub
(
(
tensor
.
log
,
(
sigmoid
,
'x'
)),
(
tensor
.
neg
,
(
softplus
,
(
tensor
.
neg
,
'x'
))),
allow_multiple_clients
=
True
)
log1msigm_to_softplus
=
gof
.
PatternSub
(
(
tensor
.
log
,
(
tensor
.
sub
,
tensor
.
constant
([[
1.0
]]),
(
sigmoid
,
'x'
))),
(
tensor
.
neg
,
(
softplus
,
'x'
)),
allow_multiple_clients
=
True
)
opt
.
register_specialize
(
logsigm_to_softplus
,
name
=
'logsigm_to_softplus'
)
opt
.
register_specialize
(
log1msigm_to_softplus
,
name
=
'log1msigm_to_softplus'
)
theano/tensor/nnet/sigm.py
0 → 100644
浏览文件 @
60a89d0b
差异被折叠。
点击展开。
theano/tensor/nnet/tests/test_sigm.py
0 → 100644
浏览文件 @
60a89d0b
import
unittest
import
theano
from
theano
import
tensor
as
T
from
theano
import
gof
import
numpy
from
theano.tests
import
unittest_tools
as
utt
from
theano.tensor.tests
import
test_basic
as
TT
from
theano.tensor.nnet
import
*
class
T_sigmoid
(
unittest
.
TestCase
):
def
setUp
(
self
):
utt
.
seed_rng
()
def
test_elemwise
(
self
):
utt
.
verify_grad
(
sigmoid
,
[
numpy
.
random
.
rand
(
3
,
4
)])
class
T_softplus
(
unittest
.
TestCase
):
def
setUp
(
self
):
utt
.
seed_rng
()
def
test_elemwise
(
self
):
utt
.
verify_grad
(
softplus
,
[
numpy
.
random
.
rand
(
3
,
4
)])
class
T_sigmoid_opts
(
unittest
.
TestCase
):
def
test_exp_over_1_plus_exp
(
self
):
m
=
theano
.
config
.
mode
if
m
==
'FAST_COMPILE'
:
m
=
'FAST_RUN'
x
=
T
.
dvector
()
# tests exp_over_1_plus_exp
f
=
theano
.
function
([
x
],
T
.
exp
(
x
)
/
(
1
+
T
.
exp
(
x
)),
mode
=
m
)
#theano.printing.debugprint(f)
assert
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
==
[
sigmoid
]
# tests inv_1_plus_exp
f
=
theano
.
function
([
x
],
T
.
fill
(
x
,
1.0
)
/
(
1
+
T
.
exp
(
-
x
)),
mode
=
m
)
#theano.printing.debugprint(f)
assert
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
==
[
sigmoid
]
# tests inv_1_plus_exp with neg
f
=
theano
.
function
([
x
],
T
.
fill
(
x
,
-
1.0
)
/
(
1
+
T
.
exp
(
-
x
)),
mode
=
m
)
#theano.printing.debugprint(f)
assert
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
==
[
sigmoid
,
T
.
inplace
.
neg_inplace
]
# tests double inv_1_plus_exp with neg
f
=
theano
.
function
([
x
],
(
T
.
fill
(
x
,
-
1.0
)
*
T
.
exp
(
x
))
/
((
1
+
T
.
exp
(
x
))
*
(
1
+
T
.
exp
(
-
x
))),
mode
=
m
)
#theano.printing.debugprint(f)
assert
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
==
[
sigmoid
,
T
.
mul
]
def
test_1msigmoid
(
self
):
m
=
theano
.
config
.
mode
if
m
==
'FAST_COMPILE'
:
m
=
'FAST_RUN'
x
=
T
.
fmatrix
()
# tests exp_over_1_plus_exp
f
=
theano
.
function
([
x
],
1
-
T
.
exp
(
x
)
/
(
1
+
T
.
exp
(
x
)),
mode
=
m
)
theano
.
printing
.
debugprint
(
f
)
assert
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
==
[
tensor
.
neg
,
sigmoid_inplace
]
# tests inv_1_plus_exp
f
=
theano
.
function
([
x
],
1
-
T
.
fill
(
x
,
1.0
)
/
(
1
+
T
.
exp
(
-
x
)),
mode
=
m
)
theano
.
printing
.
debugprint
(
f
)
assert
[
node
.
op
for
node
in
f
.
maker
.
env
.
toposort
()]
==
[
tensor
.
neg
,
sigmoid_inplace
]
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论