Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
41518f9e
提交
41518f9e
authored
11月 06, 2014
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move tests to theano/tensor, call verify_grad
Calling theano.grad on the graph built from theano.scalar.clip is not working, so we have to use theano.tensor.clip instead.
上级
7ef9e747
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
58 行增加
和
38 行删除
+58
-38
test_basic.py
theano/scalar/tests/test_basic.py
+1
-36
basic.py
theano/tensor/basic.py
+4
-2
test_basic.py
theano/tensor/tests/test_basic.py
+53
-0
没有找到文件。
theano/scalar/tests/test_basic.py
浏览文件 @
41518f9e
...
@@ -20,7 +20,7 @@ from theano.tests import unittest_tools as utt
...
@@ -20,7 +20,7 @@ from theano.tests import unittest_tools as utt
from
theano.scalar.basic
import
(
floats
,
float32
,
float64
,
from
theano.scalar.basic
import
(
floats
,
float32
,
float64
,
ints
,
int8
,
int32
,
complex64
,
ints
,
int8
,
int32
,
complex64
,
ComplexError
,
IntDiv
,
TrueDiv
,
ComplexError
,
IntDiv
,
TrueDiv
,
Composite
,
add
,
div_proxy
,
clip
,
Composite
,
add
,
div_proxy
,
and_
,
eq
,
neq
,
invert
,
mul
,
Scalar
)
and_
,
eq
,
neq
,
invert
,
mul
,
Scalar
)
from
theano.scalar.basic
import
(
from
theano.scalar.basic
import
(
true_div
,
inv
,
log
,
log2
,
log10
,
log1p
,
exp
,
exp2
,
expm1
,
sqrt
,
deg2rad
,
true_div
,
inv
,
log
,
log2
,
log10
,
log1p
,
exp
,
exp2
,
expm1
,
sqrt
,
deg2rad
,
...
@@ -62,41 +62,6 @@ class test_ScalarOps(unittest.TestCase):
...
@@ -62,41 +62,6 @@ class test_ScalarOps(unittest.TestCase):
):
):
self
.
assertTrue
(
fn
(
a
,
b
)
==
a
%
b
,
(
a
,))
self
.
assertTrue
(
fn
(
a
,
b
)
==
a
%
b
,
(
a
,))
def
test_clip_grad
(
self
):
# This is testing for the issue #633
x
,
y
=
floats
(
'xy'
)
a
=
theano
.
tensor
.
clip
(
x
,
y
,
x
)
g
=
theano
.
gradient
.
grad
(
a
,
x
)
fn
=
gof
.
DualLinker
()
.
accept
(
FunctionGraph
([
x
,
y
],
[
g
]))
.
make_function
()
# Test the other way around as well
a2
=
theano
.
tensor
.
clip
(
x
,
x
,
y
)
g2
=
theano
.
gradient
.
grad
(
a2
,
x
)
fn2
=
gof
.
DualLinker
()
.
accept
(
FunctionGraph
([
x
,
y
],
[
g2
]))
.
make_function
()
# Test for the equal case too .
a3
=
theano
.
tensor
.
clip
(
x
,
x
,
x
)
g3
=
theano
.
gradient
.
grad
(
a3
,
x
)
fn3
=
gof
.
DualLinker
()
.
accept
(
FunctionGraph
([
x
],
[
g3
]))
.
make_function
()
rng
=
np
.
random
.
RandomState
(
utt
.
fetch_seed
())
ntests
=
50
for
i
in
xrange
(
ntests
):
xval
=
rng
.
rand
(
1
)
# To ensure that the min < x .
yval_mn
=
rng
.
rand
(
1
)
-
1.0
# To ensure that the max > x.
yval_mx
=
rng
.
rand
(
1
)
+
1.0
aval
=
fn
(
xval
,
yval_mn
)
aval2
=
fn2
(
xval
,
yval_mx
)
aval3
=
fn3
(
xval
)
self
.
assertTrue
(
aval
==
1.
)
self
.
assertTrue
(
aval2
==
1.
)
self
.
assertTrue
(
aval3
==
1.
)
class
test_composite
(
unittest
.
TestCase
):
class
test_composite
(
unittest
.
TestCase
):
...
...
theano/tensor/basic.py
浏览文件 @
41518f9e
...
@@ -2997,8 +2997,10 @@ def clip(x, min, max):
...
@@ -2997,8 +2997,10 @@ def clip(x, min, max):
"""clip x to be between min and max.
"""clip x to be between min and max.
:note: When `x` is equal to the boundaries, the output is considered
:note: When `x` is equal to the boundaries, the output is considered
to be `x`, so at these points, the gradient will flow through `x`,
to be `x`, so at these points, the gradient of the cost wrt the output
not through `min` nor `max`.
will be propagated to `x`, not to `min` nor `max`. In other words,
on these points, the gradient wrt `x` will be equal to the gradient wrt
the output, and the gradient wrt `min` and `max` will be zero.
"""
"""
# see decorator for function body
# see decorator for function body
# for grep: clamp, bound
# for grep: clamp, bound
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
41518f9e
...
@@ -2484,6 +2484,59 @@ class T_Clip(unittest.TestCase):
...
@@ -2484,6 +2484,59 @@ class T_Clip(unittest.TestCase):
c
=
tensor
.
scalar
()
c
=
tensor
.
scalar
()
self
.
assertRaises
(
TypeError
,
clip
,
a
,
b
,
c
)
self
.
assertRaises
(
TypeError
,
clip
,
a
,
b
,
c
)
def
test_clip_repeat_grad
(
self
):
# This is testing for the issue #633
x
,
y
=
tensor
.
vectors
(
'xy'
)
a
=
clip
(
x
,
y
,
x
)
g
=
theano
.
gradient
.
grad
(
a
.
sum
(),
x
)
fn
=
theano
.
function
([
x
,
y
],
[
g
])
# Test the other way around as well
a2
=
clip
(
x
,
x
,
y
)
g2
=
theano
.
gradient
.
grad
(
a2
.
sum
(),
x
)
fn2
=
theano
.
function
([
x
,
y
],
[
g2
])
# Test for the equal case too
a3
=
theano
.
tensor
.
clip
(
x
,
x
,
x
)
g3
=
theano
.
gradient
.
grad
(
a3
.
sum
(),
x
)
fn3
=
theano
.
function
([
x
],
[
g3
])
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
nvals
=
50
xval
=
rng
.
rand
(
nvals
)
# To ensure that the min < x
yval_mn
=
rng
.
rand
(
nvals
)
-
1.0
# To ensure that the max > x
yval_mx
=
rng
.
rand
(
nvals
)
+
1.0
aval
,
=
fn
(
xval
,
yval_mn
)
aval2
,
=
fn2
(
xval
,
yval_mx
)
aval3
,
=
fn3
(
xval
)
self
.
assertTrue
(
numpy
.
all
(
aval
==
1.
))
self
.
assertTrue
(
numpy
.
all
(
aval2
==
1.
))
self
.
assertTrue
(
numpy
.
all
(
aval3
==
1.
))
def
test_clip_repeat_verify_grad
(
self
):
# Additional tests for issue gh-633
utt
.
verify_grad
(
op
=
lambda
x
:
clip
(
x
,
0
,
x
),
pt
=
[
rand_nonzero
((
3
,
7
))])
utt
.
verify_grad
(
op
=
lambda
x
:
clip
(
x
,
x
,
0
),
pt
=
[
rand_nonzero
((
3
,
7
))])
utt
.
verify_grad
(
op
=
lambda
x
:
clip
(
0
,
x
,
x
),
pt
=
[
rand_nonzero
((
3
,
7
))])
utt
.
verify_grad
(
op
=
lambda
x
:
clip
(
x
,
x
,
x
),
pt
=
[
rand_nonzero
((
3
,
7
))])
# TODO: consider moving this function / functionality to gradient.py
# TODO: consider moving this function / functionality to gradient.py
# rationale: it's tricky, and necessary everytime you want to verify
# rationale: it's tricky, and necessary everytime you want to verify
# gradient numerically
# gradient numerically
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论