Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
9b63b9f6
提交
9b63b9f6
authored
1月 20, 2012
作者:
Olivier Delalleau
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #342 from pascanur/jacobian_hessian
Jacobian/Hessian
上级
77679d7b
5b44b006
全部展开
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
146 行增加
和
1 行删除
+146
-1
__init__.py
theano/tensor/__init__.py
+2
-1
tensor_grad.py
theano/tensor/tensor_grad.py
+0
-0
test_2nd_order_grads.py
theano/tensor/tests/test_2nd_order_grads.py
+144
-0
没有找到文件。
theano/tensor/__init__.py
浏览文件 @
9b63b9f6
...
@@ -51,4 +51,5 @@ def shared(*args, **kw):
...
@@ -51,4 +51,5 @@ def shared(*args, **kw):
import
nnet
# used for softmax, sigmoid, etc.
import
nnet
# used for softmax, sigmoid, etc.
from
tensor_grad
import
Rop
,
Lop
,
grad
,
numeric_grad
,
verify_grad
from
tensor_grad
import
Rop
,
Lop
,
grad
,
numeric_grad
,
verify_grad
,
\
jacobian
,
hessian
theano/tensor/tensor_grad.py
浏览文件 @
9b63b9f6
差异被折叠。
点击展开。
theano/tensor/tests/test_2nd_order_grads.py
0 → 100644
浏览文件 @
9b63b9f6
"""
Test for jacobian/hessian functions in Theano
"""
import
unittest
from
theano.tests
import
unittest_tools
as
utt
from
theano
import
function
import
theano
from
theano
import
tensor
import
numpy
utt
.
seed_rng
()
def
test001_jacobian_vector
():
x
=
tensor
.
vector
()
y
=
x
*
2
rng
=
numpy
.
random
.
RandomState
(
seed
=
utt
.
fetch_seed
())
# test when the jacobian is called with a tensor as wrt
Jx
=
tensor
.
jacobian
(
y
,
x
)
f
=
theano
.
function
([
x
],
Jx
)
vx
=
rng
.
uniform
(
size
=
(
10
,))
.
astype
(
theano
.
config
.
floatX
)
assert
numpy
.
allclose
(
f
(
vx
),
numpy
.
eye
(
10
)
*
2
)
# test when the jacobian is called with a tuple as wrt
Jx
=
tensor
.
jacobian
(
y
,
(
x
,))
assert
isinstance
(
Jx
,
tuple
)
f
=
theano
.
function
([
x
],
Jx
[
0
])
vx
=
rng
.
uniform
(
size
=
(
10
,))
.
astype
(
theano
.
config
.
floatX
)
assert
numpy
.
allclose
(
f
(
vx
),
numpy
.
eye
(
10
)
*
2
)
# test when the jacobian is called with a list as wrt
Jx
=
tensor
.
jacobian
(
y
,
[
x
])
assert
isinstance
(
Jx
,
list
)
f
=
theano
.
function
([
x
],
Jx
[
0
])
vx
=
rng
.
uniform
(
size
=
(
10
,))
.
astype
(
theano
.
config
.
floatX
)
assert
numpy
.
allclose
(
f
(
vx
),
numpy
.
eye
(
10
)
*
2
)
# test when the jacobian is called with a list of two elements
z
=
tensor
.
vector
()
y
=
x
*
z
Js
=
tensor
.
jacobian
(
y
,
[
x
,
z
])
f
=
theano
.
function
([
x
,
z
],
Js
)
vx
=
rng
.
uniform
(
size
=
(
10
,))
.
astype
(
theano
.
config
.
floatX
)
vz
=
rng
.
uniform
(
size
=
(
10
,))
.
astype
(
theano
.
config
.
floatX
)
vJs
=
f
(
vx
,
vz
)
evx
=
numpy
.
zeros
((
10
,
10
))
evz
=
numpy
.
zeros
((
10
,
10
))
numpy
.
fill_diagonal
(
evx
,
vx
)
numpy
.
fill_diagonal
(
evz
,
vz
)
assert
numpy
.
allclose
(
vJs
[
0
],
evz
)
assert
numpy
.
allclose
(
vJs
[
1
],
evx
)
def
test002_jacobian_matrix
():
x
=
tensor
.
matrix
()
y
=
2
*
x
.
sum
(
axis
=
0
)
rng
=
numpy
.
random
.
RandomState
(
seed
=
utt
.
fetch_seed
())
ev
=
numpy
.
zeros
((
10
,
10
,
10
))
for
dx
in
xrange
(
10
):
ev
[
dx
,
:,
dx
]
=
2.
# test when the jacobian is called with a tensor as wrt
Jx
=
tensor
.
jacobian
(
y
,
x
)
f
=
theano
.
function
([
x
],
Jx
)
vx
=
rng
.
uniform
(
size
=
(
10
,
10
))
.
astype
(
theano
.
config
.
floatX
)
assert
numpy
.
allclose
(
f
(
vx
),
ev
)
# test when the jacobian is called with a tuple as wrt
Jx
=
tensor
.
jacobian
(
y
,
(
x
,))
assert
isinstance
(
Jx
,
tuple
)
f
=
theano
.
function
([
x
],
Jx
[
0
])
vx
=
rng
.
uniform
(
size
=
(
10
,
10
))
.
astype
(
theano
.
config
.
floatX
)
assert
numpy
.
allclose
(
f
(
vx
),
ev
)
# test when the jacobian is called with a list as wrt
Jx
=
tensor
.
jacobian
(
y
,
[
x
])
assert
isinstance
(
Jx
,
list
)
f
=
theano
.
function
([
x
],
Jx
[
0
])
vx
=
rng
.
uniform
(
size
=
(
10
,
10
))
.
astype
(
theano
.
config
.
floatX
)
assert
numpy
.
allclose
(
f
(
vx
),
ev
)
# test when the jacobian is called with a list of two elements
z
=
tensor
.
matrix
()
y
=
(
x
*
z
)
.
sum
(
axis
=
1
)
Js
=
tensor
.
jacobian
(
y
,
[
x
,
z
])
f
=
theano
.
function
([
x
,
z
],
Js
)
vx
=
rng
.
uniform
(
size
=
(
10
,
10
))
.
astype
(
theano
.
config
.
floatX
)
vz
=
rng
.
uniform
(
size
=
(
10
,
10
))
.
astype
(
theano
.
config
.
floatX
)
vJs
=
f
(
vx
,
vz
)
evx
=
numpy
.
zeros
((
10
,
10
,
10
))
evz
=
numpy
.
zeros
((
10
,
10
,
10
))
for
dx
in
xrange
(
10
):
evx
[
dx
,
dx
,
:]
=
vx
[
dx
,
:]
evz
[
dx
,
dx
,
:]
=
vz
[
dx
,
:]
assert
numpy
.
allclose
(
vJs
[
0
],
evz
)
assert
numpy
.
allclose
(
vJs
[
1
],
evx
)
def
test003_jacobian_scalar
():
x
=
tensor
.
scalar
()
y
=
x
*
2
rng
=
numpy
.
random
.
RandomState
(
seed
=
utt
.
fetch_seed
())
# test when the jacobian is called with a tensor as wrt
Jx
=
tensor
.
jacobian
(
y
,
x
)
f
=
theano
.
function
([
x
],
Jx
)
vx
=
numpy
.
cast
[
theano
.
config
.
floatX
](
rng
.
uniform
())
assert
numpy
.
allclose
(
f
(
vx
),
2
)
# test when the jacobian is called with a tuple as wrt
Jx
=
tensor
.
jacobian
(
y
,
(
x
,))
assert
isinstance
(
Jx
,
tuple
)
f
=
theano
.
function
([
x
],
Jx
[
0
])
vx
=
numpy
.
cast
[
theano
.
config
.
floatX
](
rng
.
uniform
())
assert
numpy
.
allclose
(
f
(
vx
),
2
)
# test when the jacobian is called with a list as wrt
Jx
=
tensor
.
jacobian
(
y
,
[
x
])
assert
isinstance
(
Jx
,
list
)
f
=
theano
.
function
([
x
],
Jx
[
0
])
vx
=
numpy
.
cast
[
theano
.
config
.
floatX
](
rng
.
uniform
())
assert
numpy
.
allclose
(
f
(
vx
),
2
)
# test when the jacobian is called with a list of two elements
z
=
tensor
.
scalar
()
y
=
x
*
z
Jx
=
tensor
.
jacobian
(
y
,
[
x
,
z
])
f
=
theano
.
function
([
x
,
z
],
Jx
)
vx
=
numpy
.
cast
[
theano
.
config
.
floatX
](
rng
.
uniform
())
vz
=
numpy
.
cast
[
theano
.
config
.
floatX
](
rng
.
uniform
())
vJx
=
f
(
vx
,
vz
)
assert
numpy
.
allclose
(
vJx
[
0
],
vz
)
assert
numpy
.
allclose
(
vJx
[
1
],
vx
)
def
test004_hessian
():
x
=
tensor
.
vector
()
y
=
tensor
.
sum
(
x
**
2
)
Hx
=
tensor
.
hessian
(
y
,
x
)
f
=
theano
.
function
([
x
],
Hx
)
vx
=
numpy
.
arange
(
10
)
.
astype
(
theano
.
config
.
floatX
)
assert
numpy
.
allclose
(
f
(
vx
),
numpy
.
eye
(
10
)
*
2
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论