Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
529bb53d
提交
529bb53d
authored
5月 05, 2008
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
moved grad() from gradient to tensor, as per #102
上级
5d01f64b
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
72 行增加
和
61 行删除
+72
-61
_test_gradient.py
_test_gradient.py
+0
-37
_test_sparse.py
_test_sparse.py
+2
-2
_test_tensor.py
_test_tensor.py
+41
-4
gradient.py
gradient.py
+0
-18
tensor.py
tensor.py
+29
-0
没有找到文件。
_test_gradient.py
浏览文件 @
529bb53d
...
...
@@ -228,43 +228,6 @@ class _test_grad_sources_inputs(unittest.TestCase):
self
.
failUnless
(
g
[
a1
.
inputs
[
1
]]
==
11
)
class
_test_grad
(
unittest
.
TestCase
):
class
O
(
gof
.
op
.
Op
):
def
__init__
(
self
):
self
.
inputs
=
[
gof
.
result
.
Result
(),
gof
.
result
.
Result
()]
self
.
outputs
=
[
gof
.
result
.
Result
(),
gof
.
result
.
Result
()]
self
.
gval0
=
gof
.
result
.
Result
()
self
.
gval1
=
gof
.
result
.
Result
()
def
grad
(
self
,
(
x0
,
x1
),
(
gz0
,
gz1
)):
return
self
.
gval0
,
self
.
gval1
def
test_1param
(
self
):
"""grad: Test passing a single result param"""
a1
=
_test_grad
.
O
()
self
.
failUnless
(
a1
.
gval0
is
grad
(
a1
.
outputs
[
0
],
a1
.
inputs
[
0
]))
def
test_Nparam
(
self
):
"""grad: Test passing multiple result params"""
a1
=
_test_grad
.
O
()
g0
,
g1
=
grad
(
a1
.
outputs
[
0
],
a1
.
inputs
)
self
.
failUnless
(
a1
.
gval0
is
g0
)
self
.
failUnless
(
a1
.
gval1
is
g1
)
def
test_1None_rval
(
self
):
"""grad: Test returning a single None from grad"""
a1
=
_test_grad
.
O
()
self
.
failUnless
(
None
is
grad
(
a1
.
outputs
[
0
],
a1
.
outputs
[
1
]))
self
.
failUnless
(
None
is
grad
(
a1
.
outputs
[
0
],
'wtf'
))
def
test_NNone_rval
(
self
):
"""grad: Test returning some Nones from grad"""
a1
=
_test_grad
.
O
()
g0
,
g1
,
g2
=
grad
(
a1
.
outputs
[
0
],
a1
.
inputs
+
[
'wtf'
])
self
.
failUnless
(
a1
.
gval0
is
g0
)
self
.
failUnless
(
a1
.
gval1
is
g1
)
self
.
failUnless
(
None
is
g2
)
def
matrix
():
return
tensor
.
Tensor
(
'float64'
,
[
0
,
0
])
...
...
_test_sparse.py
浏览文件 @
529bb53d
...
...
@@ -258,7 +258,7 @@ class _testCase_dot(unittest.TestCase):
y
=
dense_from_sparse
(
dot
(
w
.
T
,
xw
))
diff
=
x
-
y
loss
=
tensor
.
sum
(
tensor
.
sqr
(
diff
))
gw
=
gradient
.
grad
(
loss
,
w
)
gw
=
tensor
.
grad
(
loss
,
w
)
trainfn
=
compile
.
Function
([
x
,
w
],
[
y
,
loss
,
gw
])
x
=
numpy
.
asarray
([[
1.
,
2
],
[
3
,
4
],
[
2
,
1
]])
...
...
@@ -284,7 +284,7 @@ class _testCase_dot(unittest.TestCase):
y
=
dense_from_sparse
(
dot
(
w
.
T
,
xw
))
diff
=
x
-
y
loss
=
tensor
.
sum
(
tensor
.
sqr
(
diff
))
gw
=
gradient
.
grad
(
loss
,
w
)
gw
=
tensor
.
grad
(
loss
,
w
)
trainfn
=
compile
.
Function
([
x
,
w
],
[
y
,
loss
,
gw
])
x
=
xorig
...
...
_test_tensor.py
浏览文件 @
529bb53d
...
...
@@ -506,7 +506,7 @@ def verify_grad(testcase, op_cls, pt, n_tests=1, rng=numpy.random, eps=0.0000001
num_grad
=
gradient
.
numeric_grad
(
cost_fn
,
pt
)
symbolic_grad
=
grad
ient
.
grad
(
cost
,
tensor_pt
,
astensor
(
1.0
,
name
=
'g_cost'
))
symbolic_grad
=
grad
(
cost
,
tensor_pt
,
astensor
(
1.0
,
name
=
'g_cost'
))
if
0
:
print
'-------'
print
'----------'
...
...
@@ -840,7 +840,7 @@ class T_subtensor(unittest.TestCase):
n
=
astensor
(
numpy
.
random
.
rand
(
2
,
3
))
z
=
scal
.
constant
(
0
)
t
=
n
[
z
:,
z
]
gn
=
grad
ient
.
grad
(
sum
(
exp
(
t
)),
n
)
gn
=
grad
(
sum
(
exp
(
t
)),
n
)
gval
=
eval_outputs
([
gn
])
s0
=
'array([ 2.05362099, 0. , 0. ])'
s1
=
'array([ 1.55009327, 0. , 0. ])'
...
...
@@ -850,7 +850,7 @@ class T_subtensor(unittest.TestCase):
def
test_grad_0d
(
self
):
n
=
astensor
(
numpy
.
random
.
rand
(
2
,
3
))
t
=
n
[
1
,
0
]
gn
=
grad
ient
.
grad
(
sum
(
exp
(
t
)),
n
)
gn
=
grad
(
sum
(
exp
(
t
)),
n
)
gval
=
eval_outputs
([
gn
])
g0
=
repr
(
gval
[
0
,:])
g1
=
repr
(
gval
[
1
,:])
...
...
@@ -1084,7 +1084,7 @@ class _testCase_matinv(unittest.TestCase):
# Sum of squared errors
ssdiff
=
sum
((
diff
**
2.0
))
g_b
=
grad
ient
.
grad
(
ssdiff
,
b
)
g_b
=
grad
(
ssdiff
,
b
)
# compilation to function
# [a,b] are the inputs, [ssdiff,g_b] are the outputs
...
...
@@ -1500,6 +1500,43 @@ class T_stdlib(unittest.TestCase):
self
.
failUnless
(
t
.
data
==
1.0
)
self
.
failUnless
(
t
.
data
is
not
tt
.
data
)
class
_test_grad
(
unittest
.
TestCase
):
class
O
(
gof
.
op
.
Op
):
def
__init__
(
self
):
self
.
inputs
=
[
scalar
(
'a'
),
scalar
(
'c'
)]
self
.
outputs
=
[
scalar
(
'b'
),
scalar
(
'd'
)]
self
.
gval0
=
scalar
(
'e'
)
self
.
gval1
=
scalar
(
'f'
)
def
grad
(
self
,
(
x0
,
x1
),
(
gz0
,
gz1
)):
return
self
.
gval0
,
self
.
gval1
def
test_1param
(
self
):
"""grad: Test passing a single result param"""
a1
=
_test_grad
.
O
()
self
.
failUnless
(
a1
.
gval0
is
grad
(
a1
.
outputs
[
0
],
a1
.
inputs
[
0
]))
def
test_Nparam
(
self
):
"""grad: Test passing multiple result params"""
a1
=
_test_grad
.
O
()
g0
,
g1
=
grad
(
a1
.
outputs
[
0
],
a1
.
inputs
)
self
.
failUnless
(
a1
.
gval0
is
g0
)
self
.
failUnless
(
a1
.
gval1
is
g1
)
def
test_1None_rval
(
self
):
"""grad: Test returning a single None from grad"""
a1
=
_test_grad
.
O
()
self
.
failUnless
(
None
is
grad
(
a1
.
outputs
[
0
],
a1
.
outputs
[
1
]))
self
.
failUnless
(
None
is
grad
(
a1
.
outputs
[
0
],
'wtf'
))
def
test_NNone_rval
(
self
):
"""grad: Test returning some Nones from grad"""
a1
=
_test_grad
.
O
()
g0
,
g1
,
g2
=
grad
(
a1
.
outputs
[
0
],
a1
.
inputs
+
[
'wtf'
])
self
.
failUnless
(
a1
.
gval0
is
g0
)
self
.
failUnless
(
a1
.
gval1
is
g1
)
self
.
failUnless
(
None
is
g2
)
...
...
gradient.py
浏览文件 @
529bb53d
...
...
@@ -108,24 +108,6 @@ def grad_sources_inputs(sources, graph_inputs):
gmap
[
r
]
=
g_r
return
gmap
def
grad
(
cost
,
param
,
g_cost
=
1.0
):
"""
@type cost: L{Result}
@type param: L{Result} or list of L{Result}s.
@rtype: L{Result} or list of L{Result}s (depending upon I{param})
@return: symbolic expression of gradient of I{cost} wrt I{param}.
If I{param} is a list, then return a list containing the gradient of I{cost} wrt
each element of the list.
"""
inputs
=
gof
.
graph
.
inputs
([
cost
])
gmap
=
grad_sources_inputs
([(
cost
,
g_cost
)],
inputs
)
if
isinstance
(
param
,
list
):
return
[
gmap
.
get
(
p
,
None
)
for
p
in
param
]
else
:
return
gmap
.
get
(
param
,
None
)
class
numeric_grad
:
def
__init__
(
self
,
f
,
pt
,
eps
=
1.0e-7
):
"""Return the gradient of f at pt.
...
...
tensor.py
浏览文件 @
529bb53d
...
...
@@ -11,6 +11,7 @@ import gof.result
import
gof.op
import
blas
# for gemm, dot
import
gradient
import
elemwise
as
s2t
import
scalar
as
scal
...
...
@@ -1078,3 +1079,31 @@ class Gemm(_Op):
"""
%
dict
(
locals
(),
**
sub
)
gemm
=
gof
.
op
.
constructor
(
Gemm
)
#########################
# Gradient
#########################
def
grad
(
cost
,
wrt
,
g_cost
=
None
):
"""
@type cost: L{Result}
@type wrt: L{Result} or list of L{Result}s.
@type g_cost: L{Result} broadcastable to size of I{cost}, or None
@param g_cost: an expression for the gradient through cost. The default is
{{{ones_like(cost)}}}
@rtype: L{Result} or list of L{Result}s (depending upon I{wrt})
@return: symbolic expression of gradient of I{cost} with respect to I{wrt}.
If I{wrt} is a list, then return a list containing the gradient of I{cost} wrt
each element of the list.
"""
if
g_cost
is
None
:
g_cost
=
ones_like
(
cost
)
inputs
=
gof
.
graph
.
inputs
([
cost
])
gmap
=
gradient
.
grad_sources_inputs
([(
cost
,
g_cost
)],
inputs
)
if
isinstance
(
wrt
,
list
):
return
[
gmap
.
get
(
p
,
None
)
for
p
in
wrt
]
else
:
return
gmap
.
get
(
wrt
,
None
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论