Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4394e165
提交
4394e165
authored
3月 12, 2008
作者:
james@mackie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added _test_gradient.py
上级
eede9fcb
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
176 行增加
和
0 行删除
+176
-0
_test_gradient.py
_test_gradient.py
+176
-0
没有找到文件。
_test_gradient.py
0 → 100644
浏览文件 @
4394e165
#
# UNIT TEST
#
import
unittest
import
numpy
import
compile
import
tensor
import
tensor_ops
as
T
from
gradient
import
*
def
matrix
():
return
tensor
.
Tensor
(
'float64'
,
[
0
,
0
])
def
matrices
(
n
):
return
[
matrix
()
for
i
in
xrange
(
n
)]
class
_testCase
(
unittest
.
TestCase
):
def
setUp
(
self
):
numpy
.
random
.
seed
(
1
)
def
matinv
(
self
,
dim
):
# symbolic program
a
,
b
=
matrices
(
2
)
ab
=
T
.
dot
(
a
,
b
)
diff
=
ab
-
tensor
.
tensor
(
numpy
.
identity
(
dim
))
ssdiff
=
T
.
sum
((
diff
**
2.0
))
g
=
grad
(
ssdiff
,
None
,
tensor
.
tensor
(
numpy
.
ones
(
1
)))
# compilation to function
fn
=
compile
.
Function
([
a
,
b
],
[
ssdiff
,
g
(
b
)])
# use the function
w
=
numpy
.
random
.
rand
(
dim
,
dim
)
wi
=
numpy
.
random
.
rand
(
dim
,
dim
)
for
i
in
xrange
(
300
):
ssd
,
gw
=
fn
(
w
,
wi
)
#print ssdiff
if
i
==
0
:
str0
=
str
(
ssd
)
wi
-=
0.4
*
gw
return
str0
,
str
(
ssd
)
def
test_matinv
(
self
):
"""Matrix inversion by gradient descent (eval mode)"""
self
.
assertEqual
((
'2.67327580893'
,
'0.000438649434819'
),
self
.
matinv
(
3
))
class
_testCase_old
:
class
posneg
(
T
.
_TensorOp
):
nout
=
2
def
impl
(
x
):
return
x
,
-
x
def
grad
(
x
,
gpos
,
gneg
):
return
gpos
-
gneg
class
posnegzero
(
T
.
_TensorOp
):
nout
=
3
def
impl
(
x
):
return
x
,
-
x
,
0.0
def
grad
(
x
,
gpos
,
gneg
,
gzero
):
return
gpos
-
gneg
def
setUp
(
self
):
numpy
.
random
.
seed
(
1
)
def
test_grad_wrt_ndarray_pointer
(
self
):
"""Grad indexing by un-wrapped ndarray"""
a
=
numpy
.
ones
((
4
,
4
))
b
=
numpy
.
ones
((
4
,
4
))
c
=
numpy
.
ones
((
4
,
4
))
expr
=
core
.
sum
(
core
.
dot
(
core
.
add
(
a
,
b
),
c
))
g
=
grad
(
expr
)
g
[
a
]
def
test_bprop_call_order
(
self
):
"""Ensure call before bprop is illegal"""
a
=
numpy
.
ones
((
3
,
3
,
3
))
b
=
core
.
exp
(
a
)
gb
=
Grad
({
b
:
wrappers
.
wrap
(
a
)})
try
:
gb
(
a
)
self
.
assertEqual
(
'should have raised'
,
0
)
except
Exception
,
e
:
self
.
assertEqual
(
str
(
e
),
'Grad.__call__ only makes sense after a bprop'
)
return
self
.
assertEqual
(
'should have caught, returned'
,
0
)
def
test_undefined_grad0
(
self
):
"""Make sure posneg works with fully specified gradients"""
a
=
numpy
.
ones
((
3
,
3
,
3
))
b
,
c
=
_testCase
.
posneg
(
a
)
g
=
Grad
({
b
:
wrappers
.
wrap
(
a
),
c
:
wrappers
.
wrap
(
a
)})
g
.
bprop
()
max
=
numpy
.
max
(
g
(
a
))
min
=
numpy
.
min
(
g
(
a
))
self
.
assertEqual
(
max
,
min
)
self
.
assertEqual
(
max
,
0.0
)
def
test_undefined_grad1
(
self
):
"""Propagate undefined values through posneg's first gradient"""
a
=
numpy
.
ones
((
3
,
3
,
3
))
b
,
c
=
_testCase
.
posneg
(
a
)
gb
=
Grad
({
b
:
wrappers
.
wrap
(
a
)})
try
:
gb
.
bprop
()
self
.
assertEqual
(
'should have raised'
,
0
)
except
UndefinedError
:
return
self
.
assertEqual
(
"Should have been error"
,
0
)
def
test_undefined_grad2
(
self
):
"""Propagate undefined values through posneg's second gradient"""
a
=
numpy
.
ones
((
3
,
3
,
3
))
b
,
c
=
_testCase
.
posneg
(
a
)
gc
=
Grad
({
c
:
wrappers
.
wrap
(
a
)})
try
:
gc
.
bprop
()
self
.
assertEqual
(
'should have raised'
,
0
)
except
UndefinedError
:
return
self
.
assertEqual
(
"Should have been error"
,
0
)
def
test_undefined_grad3
(
self
):
"""Ignore undefined values properly"""
a
=
numpy
.
ones
((
3
,
3
,
3
))
b
,
c
,
d
=
_testCase
.
posnegzero
(
a
)
#print b, c, d
g
=
Grad
({
b
:
wrappers
.
wrap
(
a
),
c
:
wrappers
.
wrap
(
a
)})
g
.
bprop
()
max
=
numpy
.
max
(
g
(
a
))
min
=
numpy
.
min
(
g
(
a
))
self
.
assertEqual
(
max
,
min
)
self
.
assertEqual
(
max
,
0.0
)
def
test_repeat_bprop
(
self
):
"""Refuse to repeat bprop"""
a
=
numpy
.
ones
((
3
,
3
,
3
))
b
,
c
,
d
=
_testCase
.
posnegzero
(
a
)
#print b, c, d
g
=
Grad
({
b
:
wrappers
.
wrap
(
a
),
c
:
wrappers
.
wrap
(
a
)})
g
.
bprop
()
try
:
g
.
bprop
()
self
.
assertEqual
(
'should have raised'
)
except
Exception
,
e
:
self
.
assertEqual
(
str
(
e
),
'bprop has already been done. Consider calling with maybe_redo=True.'
)
return
self
.
assertEqual
(
'should have caught'
)
def
test_repeat_bprop1
(
self
):
"""Force repeat bprop"""
a
=
numpy
.
ones
((
3
,
3
,
3
))
z
=
numpy
.
zeros
((
3
,
3
,
3
))
b
,
c
,
d
=
_testCase
.
posnegzero
(
a
)
#print b, c, d
g
=
Grad
({
b
:
wrappers
.
wrap
(
a
),
c
:
wrappers
.
wrap
(
z
)})
g
.
bprop
()
g
.
bprop
(
maybe_redo
=
True
)
max
=
numpy
.
max
(
g
(
a
))
min
=
numpy
.
min
(
g
(
a
))
self
.
assertEqual
(
max
,
min
)
self
.
assertEqual
(
max
,
2.0
)
def
tearDown
(
self
):
core
.
pop_mode
()
if
__name__
==
'__main__'
:
unittest
.
main
()
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论