Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
7885e618
提交
7885e618
authored
9月 11, 2012
作者:
Ian Goodfellow
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
a lot of pep8
上级
c7d06ac9
全部展开
显示空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
66 行增加
和
42 行删除
+66
-42
sigm.py
theano/tensor/nnet/sigm.py
+29
-17
randomstreams.py
theano/tensor/randomstreams.py
+2
-2
test_basic.py
theano/tensor/tests/test_basic.py
+0
-0
test_elemwise.py
theano/tensor/tests/test_elemwise.py
+35
-23
test_naacl09.py
theano/tensor/tests/test_naacl09.py
+0
-0
test_opt.py
theano/tensor/tests/test_opt.py
+0
-0
test_gradient.py
theano/tests/test_gradient.py
+0
-0
test_rop.py
theano/tests/test_rop.py
+0
-0
没有找到文件。
theano/tensor/nnet/sigm.py
浏览文件 @
7885e618
...
...
@@ -42,7 +42,7 @@ class ScalarSigmoid(scalar.UnaryScalarOp):
assert
rval
.
type
.
dtype
.
find
(
'float'
)
!=
-
1
return
[
rval
]
return
[
rval
]
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
x
,
=
inp
...
...
@@ -69,7 +69,7 @@ sigmoid = elemwise.Elemwise(scalar_sigmoid, name='sigmoid')
sigmoid_inplace
=
elemwise
.
Elemwise
(
ScalarSigmoid
(
scalar
.
transfer_type
(
0
)),
inplace_pattern
=
{
0
:
0
},
inplace_pattern
=
{
0
:
0
},
name
=
'sigmoid_inplace'
,
)
...
...
@@ -84,12 +84,15 @@ class ScalarSoftplus(scalar.UnaryScalarOp):
if
x
>
30.0
:
return
x
return
numpy
.
log1p
(
numpy
.
exp
(
x
))
def
impl
(
self
,
x
):
return
ScalarSoftplus
.
static_impl
(
x
)
def
grad
(
self
,
inp
,
grads
):
x
,
=
inp
gz
,
=
grads
return
[
gz
*
scalar_sigmoid
(
x
)]
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
x
,
=
inp
z
,
=
out
...
...
@@ -103,27 +106,29 @@ class ScalarSoftplus(scalar.UnaryScalarOp):
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'
)
scalar_softplus
=
ScalarSoftplus
(
scalar
.
upgrade_to_float
,
name
=
'scalar_softplus'
)
softplus
=
elemwise
.
Elemwise
(
scalar_softplus
,
name
=
'softplus'
)
pprint
.
assign
(
softplus
,
printing
.
FunctionPrinter
(
'softplus'
))
def
_skip_mul_1
(
r
):
if
r
.
owner
and
r
.
owner
.
op
==
tensor
.
mul
:
not_is_1
=
[
i
for
i
in
r
.
owner
.
inputs
if
not
_is_1
(
i
)
]
if
len
(
not_is_1
)
==
1
:
not_is_1
=
[
i
for
i
in
r
.
owner
.
inputs
if
not
_is_1
(
i
)]
if
len
(
not_is_1
)
==
1
:
return
not_is_1
[
0
]
logsigm_to_softplus
=
gof
.
PatternSub
(
(
tensor
.
log
,
(
sigmoid
,
'x'
)),
(
tensor
.
neg
,
(
softplus
,
(
tensor
.
neg
,
'x'
))),
allow_multiple_clients
=
True
,
allow_multiple_clients
=
True
,
skip_identities_fn
=
_skip_mul_1
)
...
...
@@ -139,21 +144,22 @@ def _is_1(expr):
log1msigm_to_softplus
=
gof
.
PatternSub
(
(
tensor
.
log
,
(
tensor
.
sub
,
dict
(
pattern
=
'y'
,
constraint
=
_is_1
),
dict
(
pattern
=
'y'
,
constraint
=
_is_1
),
(
sigmoid
,
'x'
))),
(
tensor
.
neg
,
(
softplus
,
'x'
)),
allow_multiple_clients
=
True
,
allow_multiple_clients
=
True
,
skip_identities_fn
=
_skip_mul_1
)
log1pexp_to_softplus
=
gof
.
PatternSub
(
(
tensor
.
log1p
,
(
tensor
.
exp
,
'x'
)),
(
softplus
,
'x'
),
allow_multiple_clients
=
True
)
allow_multiple_clients
=
True
)
opt
.
register_stabilize
(
logsigm_to_softplus
,
name
=
'logsigm_to_softplus'
)
opt
.
register_stabilize
(
log1msigm_to_softplus
,
name
=
'log1msigm_to_softplus'
)
opt
.
register_stabilize
(
log1pexp_to_softplus
,
name
=
'log1pexp_to_softplus'
)
opt
.
register_stabilize
(
logsigm_to_softplus
,
name
=
'logsigm_to_softplus'
)
opt
.
register_stabilize
(
log1msigm_to_softplus
,
name
=
'log1msigm_to_softplus'
)
opt
.
register_stabilize
(
log1pexp_to_softplus
,
name
=
'log1pexp_to_softplus'
)
def
is_1pexp
(
t
):
"""
...
...
@@ -247,7 +253,7 @@ def partition_num_or_denom(r, f):
else
:
neg_t
,
f_t
=
f_t
f_terms
.
append
(
f_t
)
neg
^=
neg_t
#
bit flip if neg_t is true
neg
^=
neg_t
#
bit flip if neg_t is true
return
f_terms
,
rest
,
neg
...
...
@@ -299,7 +305,8 @@ def local_exp_over_1_plus_exp(node):
#find all the exp() terms in the numerator
num
,
denom
=
node
.
inputs
num_exp_x
,
num_rest
,
num_neg
=
partition_num_or_denom
(
num
,
is_exp
)
denom_1pexp
,
denom_rest
,
denom_neg
=
partition_num_or_denom
(
denom
,
is_1pexp
)
denom_1pexp
,
denom_rest
,
denom_neg
=
partition_num_or_denom
(
denom
,
is_1pexp
)
sigmoids
=
[]
for
t
in
denom_1pexp
:
...
...
@@ -330,6 +337,7 @@ def local_exp_over_1_plus_exp(node):
else
:
return
[
new_num
/
tensor
.
mul
(
*
denom_rest
)]
def
parse_mul_tree
(
root
):
"""
Parse a tree of multiplications starting at the given root.
...
...
@@ -621,10 +629,13 @@ def local_inv_1_plus_exp(node):
if
nonconsts
[
0
]
.
owner
and
nonconsts
[
0
]
.
owner
.
op
==
tensor
.
exp
:
if
scalars
and
numpy
.
allclose
(
numpy
.
sum
(
scalars
),
1
):
return
opt
.
_fill_chain
(
sigmoid
(
tensor
.
neg
(
nonconsts
[
0
]
.
owner
.
inputs
[
0
])),
sigmoid
(
tensor
.
neg
(
nonconsts
[
0
]
.
owner
.
inputs
[
0
])),
scalar_inputs
)
# Registration is below, and conditional.
@gof.local_optimizer
([
tensor
.
sub
])
def
local_1msigmoid
(
node
):
"""
...
...
@@ -686,13 +697,14 @@ if 0:
assert
t0
.
owner
.
op
==
div
t0top
,
t0bot
=
t0
.
owner
.
inputs
t1top
,
t1bot
=
t1
.
owner
.
inputs
rval
.
append
(
div
(
mul
(
*
(
t0top
+
t1top
)),
mul
(
*
(
t0bot
+
t1bot
))))
rval
.
append
(
div
(
mul
(
*
(
t0top
+
t1top
)),
mul
(
*
(
t0bot
+
t1bot
))))
if
len
(
rval
)
>
100
:
# This loop can be exponentially long.
# aborting
return
[]
elif
len
(
node
.
outputs
)
>
1
:
elif
len
(
node
.
outputs
)
>
1
:
return
[]
else
:
return
[
node
.
outputs
[
0
]]
theano/tensor/randomstreams.py
浏览文件 @
7885e618
...
...
@@ -136,7 +136,7 @@ class RandomStreams(Component, raw_random.RandomStreamsBase):
"""
def
__init__
(
self
,
seed
=
None
,
no_warn
=
False
):
def
__init__
(
self
,
seed
=
None
,
no_warn
=
False
):
""":type seed: None or int
:param seed: a default seed to initialize the RandomState
...
...
@@ -146,7 +146,7 @@ class RandomStreams(Component, raw_random.RandomStreamsBase):
"""
if
not
no_warn
:
deprecation_warning
()
super
(
RandomStreams
,
self
)
.
__init__
(
no_warn
=
True
)
super
(
RandomStreams
,
self
)
.
__init__
(
no_warn
=
True
)
self
.
random_state_variables
=
[]
self
.
default_instance_seed
=
seed
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
7885e618
This source diff could not be displayed because it is too large. You can
view the blob
instead.
theano/tensor/tests/test_elemwise.py
浏览文件 @
7885e618
...
...
@@ -47,7 +47,8 @@ class test_DimShuffle(unittest_tools.InferShapeTester):
#test that DimShuffle.infer_shape work correctly
x
=
TensorType
(
'float64'
,
ib
)(
'x'
)
e
=
DimShuffle
(
ib
,
shuffle
)(
x
)
f
=
copy
(
linker
)
.
accept
(
FunctionGraph
([
x
],
[
e
.
shape
]))
.
make_function
()
f
=
copy
(
linker
)
.
accept
(
FunctionGraph
([
x
],
[
e
.
shape
]))
.
make_function
()
assert
all
(
f
(
numpy
.
ones
(
xsh
)))
==
all
(
zsh
)
# Test when we drop a axis that is not broadcastable
...
...
@@ -125,7 +126,8 @@ class test_Broadcast(unittest.TestCase):
x
=
TensorType
(
'float64'
,
[(
entry
==
1
)
for
entry
in
xsh
])(
'x'
)
y
=
TensorType
(
'float64'
,
[(
entry
==
1
)
for
entry
in
ysh
])(
'y'
)
e
=
Elemwise
(
scalar
.
add
)(
x
,
y
)
f
=
copy
(
linker
)
.
accept
(
FunctionGraph
([
x
,
y
],
[
e
.
shape
]))
.
make_function
()
f
=
copy
(
linker
)
.
accept
(
FunctionGraph
([
x
,
y
],
[
e
.
shape
]))
.
make_function
()
assert
tuple
(
f
(
xv
,
yv
))
==
tuple
(
zv
.
shape
)
def
with_linker_inplace
(
self
,
linker
):
...
...
@@ -154,7 +156,8 @@ class test_Broadcast(unittest.TestCase):
x
=
TensorType
(
'float64'
,
[(
entry
==
1
)
for
entry
in
xsh
])(
'x'
)
y
=
TensorType
(
'float64'
,
[(
entry
==
1
)
for
entry
in
ysh
])(
'y'
)
e
=
Elemwise
(
scalar
.
Add
(
scalar
.
transfer_type
(
0
)),
{
0
:
0
})(
x
,
y
)
f
=
copy
(
linker
)
.
accept
(
FunctionGraph
([
x
,
y
],
[
e
.
shape
]))
.
make_function
()
f
=
copy
(
linker
)
.
accept
(
FunctionGraph
([
x
,
y
],
[
e
.
shape
]))
.
make_function
()
xv
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
xsh
))
yv
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
ysh
))
zv
=
xv
+
yv
...
...
@@ -349,7 +352,8 @@ class test_CAReduce(unittest_tools.InferShapeTester):
e
=
tensor_op
(
x
,
axis
=
tosum
)
if
tosum
is
None
:
tosum
=
range
(
len
(
xsh
))
f
=
copy
(
linker
)
.
accept
(
FunctionGraph
([
x
],
[
e
.
shape
]))
.
make_function
()
f
=
copy
(
linker
)
.
accept
(
FunctionGraph
([
x
],
[
e
.
shape
]))
.
make_function
()
if
not
(
scalar_op
in
[
scalar
.
maximum
,
scalar
.
minimum
]
and
((
xsh
==
()
or
numpy
.
prod
(
xsh
)
==
0
))):
assert
all
(
f
(
xv
)
==
zv
.
shape
)
...
...
@@ -459,7 +463,8 @@ class test_Prod(unittest.TestCase):
# including zeros, as the case with zeros is important
# (and special cases: 1 zero in the row, more than 1 zero in the row)
x_val
=
numpy
.
asarray
([[
1
,
2
,
3
],[
4
,
5
,
6
],[
7
,
8
,
9
]],
dtype
=
'float32'
)
x_val
=
numpy
.
asarray
([[
1
,
2
,
3
],
[
4
,
5
,
6
],
[
7
,
8
,
9
]],
dtype
=
'float32'
)
x
=
theano
.
tensor
.
dmatrix
()
# now with verify_grad
unittest_tools
.
verify_grad
(
Prod
(
axis
=
1
),
[
x_val
],
mode
=
self
.
mode
)
...
...
@@ -471,26 +476,28 @@ class test_Prod(unittest.TestCase):
unittest_tools
.
verify_grad
(
fn
,
[
x_val
],
mode
=
self
.
mode
)
def
test_verify_grad_with_zeros
(
self
):
# including zeros, as the case with zeros is important
# (and special cases: 1 zero in the row, more than 1 zero in the row)
x_val
=
numpy
.
asarray
([[
1.
,
2.
,
3.
],[
0.
,
5.
,
6.
],[
0.
,
0.
,
9.
]],
dtype
=
'float32'
)
x_val
=
numpy
.
asarray
([[
1.
,
2.
,
3.
],
[
0.
,
5.
,
6.
],
[
0.
,
0.
,
9.
]],
dtype
=
'float32'
)
x
=
theano
.
tensor
.
dmatrix
()
# sanity check
x2
=
theano
.
tensor
.
dmatrix
()
p
=
Prod
(
axis
=
1
)(
x
)
p2
=
Prod
(
axis
=
1
)(
x2
)
fn
=
theano
.
function
([
x
,
x2
],[
p
-
p2
],
mode
=
self
.
mode
)
fn
=
theano
.
function
([
x
,
x2
],
[
p
-
p2
],
mode
=
self
.
mode
)
#print "hand computed diff for each row"
x2_val
=
numpy
.
asarray
([[
1.
,
2.
,
3.003
],
[
0.003
,
5.
,
6
],
[
0.
,
0.
,
9.01
]])
x2_val
=
numpy
.
asarray
([[
1.
,
2.
,
3.003
],
[
0.003
,
5.
,
6
],
[
0.
,
0.
,
9.01
]])
#print fn(x_val, x2_val)
fn2
=
theano
.
function
([
x
],[
theano
.
tensor
.
grad
(
p
.
sum
(),
x
)],
mode
=
self
.
mode
)
fn2
=
theano
.
function
([
x
],
[
theano
.
tensor
.
grad
(
p
.
sum
(),
x
)],
mode
=
self
.
mode
)
#print "real grad"
#print fn2(x_val)
fn3
=
theano
.
function
([
x
],[
p
],
mode
=
self
.
mode
)
assert
numpy
.
allclose
(
fn3
(
x_val
),
[
6.
,
0.
,
0.
])
fn3
=
theano
.
function
([
x
],
[
p
],
mode
=
self
.
mode
)
assert
numpy
.
allclose
(
fn3
(
x_val
),
[
6.
,
0.
,
0.
])
# now with verify_grad
unittest_tools
.
verify_grad
(
Prod
(
axis
=
1
),
[
x_val
],
mode
=
self
.
mode
)
...
...
@@ -511,10 +518,10 @@ class test_Prod(unittest.TestCase):
def
test_prod_without_zeros
(
self
):
x
=
theano
.
tensor
.
dmatrix
()
x_val
=
numpy
.
array
([[
1
,
2
,
3
],[
0
,
5
,
6
],[
0
,
0
,
9
]],
dtype
=
'float32'
)
x_val
=
numpy
.
array
([[
1
,
2
,
3
],
[
0
,
5
,
6
],
[
0
,
0
,
9
]],
dtype
=
'float32'
)
pwz
=
ProdWithoutZeros
(
axis
=
1
)(
x
)
fn
=
theano
.
function
([
x
],
pwz
,
mode
=
self
.
mode
)
assert
numpy
.
allclose
(
fn
(
x_val
),
[
6
,
30
,
9
])
assert
numpy
.
allclose
(
fn
(
x_val
),
[
6
,
30
,
9
])
pwz_a0
=
ProdWithoutZeros
(
axis
=
0
)(
x
)
fn_a0
=
theano
.
function
([
x
],
pwz_a0
,
mode
=
self
.
mode
)
...
...
@@ -522,25 +529,30 @@ class test_Prod(unittest.TestCase):
def
test_other_grad_tests
(
self
):
x
=
theano
.
tensor
.
dmatrix
()
x_val1
=
numpy
.
array
([[
1
,
2
,
3
],[
0
,
5
,
6
],[
0
,
0
,
9
]],
dtype
=
'float32'
)
x_val2
=
numpy
.
array
([[
1
,
2
,
0
],[
0
,
5
,
6
],[
7
,
8
,
9
],[
9
,
10
,
0
]],
dtype
=
'float32'
)
x_val1
=
numpy
.
array
([[
1
,
2
,
3
],
[
0
,
5
,
6
],
[
0
,
0
,
9
]],
dtype
=
'float32'
)
x_val2
=
numpy
.
array
([[
1
,
2
,
0
],
[
0
,
5
,
6
],
[
7
,
8
,
9
],
[
9
,
10
,
0
]],
dtype
=
'float32'
)
rng
=
rng
=
numpy
.
random
.
RandomState
(
43
)
p
=
Prod
(
axis
=
1
)
grad_p
=
theano
.
tensor
.
grad
(
p
(
x
)
.
sum
(),
x
)
grad_fn
=
theano
.
function
([
x
],
grad_p
,
mode
=
self
.
mode
)
assert
numpy
.
allclose
(
grad_fn
(
x_val1
),
[[
6.
,
3.
,
2.
],[
30.
,
0.
,
0.
],[
0.
,
0.
,
0.
]])
assert
numpy
.
allclose
(
grad_fn
(
x_val2
),
[[
0.
,
0.
,
2.
],
[
30.
,
0.
,
0.
],
[
72.
,
63.
,
56.
],
[
0.
,
0.
,
90.
]])
assert
numpy
.
allclose
(
grad_fn
(
x_val1
),
[[
6.
,
3.
,
2.
],
[
30.
,
0.
,
0.
],
[
0.
,
0.
,
0.
]])
assert
numpy
.
allclose
(
grad_fn
(
x_val2
),
[[
0.
,
0.
,
2.
],
[
30.
,
0.
,
0.
],
[
72.
,
63.
,
56.
],
[
0.
,
0.
,
90.
]])
p_axis0
=
Prod
(
axis
=
0
)
grad_p_axis0
=
theano
.
tensor
.
grad
(
p_axis0
(
x
)
.
sum
(),
x
)
grad_fn_axis0
=
theano
.
function
([
x
],
grad_p_axis0
,
mode
=
self
.
mode
)
assert
numpy
.
allclose
(
grad_fn_axis0
(
x_val2
),
[[
0.
,
400.
,
0.
],[
63.
,
160.
,
0.
],
[
0.
,
100.
,
0.
],
[
0.
,
80.
,
0.
]])
assert
numpy
.
allclose
(
grad_fn_axis0
(
x_val2
),
[[
0.
,
400.
,
0.
],
[
63.
,
160.
,
0.
],
[
0.
,
100.
,
0.
],
[
0.
,
80.
,
0.
]])
tensor
.
verify_grad
(
p
,
[
x_val1
],
rng
=
rng
,
mode
=
self
.
mode
)
def
test_mul_without_zeros_zeros
(
self
):
a
=
numpy
.
zeros
((
3
,
3
))
a
=
numpy
.
zeros
((
3
,
3
))
x
=
theano
.
tensor
.
dmatrix
()
...
...
@@ -655,6 +667,7 @@ class T_sum_dtype(unittest.TestCase):
idx
+=
1
class
T_mean_dtype
(
unittest
.
TestCase
):
def
test_mean_default_dtype
(
self
):
"""
...
...
@@ -710,6 +723,7 @@ class T_mean_dtype(unittest.TestCase):
idx
+=
1
class
T_prod_dtype
(
unittest
.
TestCase
):
def
test_prod_default_dtype
(
self
):
"""
...
...
@@ -761,6 +775,7 @@ class T_prod_dtype(unittest.TestCase):
idx
+=
1
class
T_prod_without_zeros_dtype
(
unittest
.
TestCase
):
def
test_prod_without_zeros_default_dtype
(
self
):
"""
...
...
@@ -844,11 +859,8 @@ if __name__ == '__main__':
"""
if
__name__
==
'__main__'
:
t
=
TestElemwise
(
'setUp'
)
t
.
setUp
()
t
.
test_infer_shape
()
theano/tensor/tests/test_naacl09.py
浏览文件 @
7885e618
差异被折叠。
点击展开。
theano/tensor/tests/test_opt.py
浏览文件 @
7885e618
This source diff could not be displayed because it is too large. You can
view the blob
instead.
theano/tests/test_gradient.py
浏览文件 @
7885e618
差异被折叠。
点击展开。
theano/tests/test_rop.py
浏览文件 @
7885e618
差异被折叠。
点击展开。
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论