Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4336f227
提交
4336f227
authored
3月 13, 2008
作者:
bergstrj@iro.umontreal.ca
浏览文件
操作
浏览文件
下载
差异文件
gradient.py written and tested
上级
30d14420
c1d9d010
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
316 行增加
和
80 行删除
+316
-80
_test_gradient.py
_test_gradient.py
+275
-47
gradient.py
gradient.py
+41
-33
没有找到文件。
_test_gradient.py
浏览文件 @
4336f227
...
@@ -4,11 +4,282 @@
...
@@ -4,11 +4,282 @@
#
#
import
unittest
import
unittest
import
numpy
import
numpy
import
compile
import
tensor
import
tensor_ops
as
T
import
tensor_ops
as
T
import
tensor
import
gof
from
gradient
import
*
from
gradient
import
*
import
gradient
class
posneg
(
T
.
TensorOp
):
nout
=
2
def
impl
(
self
,
x
):
return
x
,
-
x
def
grad
(
self
,
x
,
(
gpos
,
gneg
)):
return
gpos
-
gneg
class
posnegzero
(
T
.
TensorOp
):
nout
=
3
def
impl
(
self
,
x
):
return
x
,
-
x
,
0.0
def
grad
(
self
,
x
,
(
gpos
,
gneg
,
gzero
)):
return
gpos
-
gneg
class
_test_grad_sources_inputs
(
unittest
.
TestCase
):
def
test_retNone1
(
self
):
"""Test that it is not ok to return None from op.grad()"""
class
retNone
(
gof
.
op
.
Op
):
def
__init__
(
self
,
arg
):
self
.
inputs
=
[
gof
.
result
.
ResultBase
()]
self
.
outputs
=
[
gof
.
result
.
ResultBase
()]
def
grad
(
self
,
x
,
gz
):
pass
a
=
retNone
(
5
)
try
:
grad_sources_inputs
([(
a
.
out
,
1
)],
None
)
except
ValueError
,
e
:
self
.
failUnless
(
e
[
0
]
is
gradient
.
_msg_retNone
)
return
self
.
fail
()
def
test_retNone1_b
(
self
):
"""Test that it is ok to return [None] from op.grad()"""
class
retNone
(
gof
.
op
.
Op
):
def
__init__
(
self
,
arg
):
self
.
inputs
=
arg
self
.
outputs
=
[
gof
.
result
.
ResultBase
()]
def
grad
(
self
,
x
,
gz
):
return
[
None
]
i
=
gof
.
result
.
ResultBase
()
a
=
retNone
([
i
])
g
=
grad_sources_inputs
([(
a
.
out
,
1
)],
None
)
self
.
failUnless
(
not
i
in
g
)
def
test_wrong_rval_len1
(
self
):
"""Test that it is not ok to return the wrong number of gradients"""
class
retNone
(
gof
.
op
.
Op
):
def
__init__
(
self
,
arg
):
self
.
inputs
=
arg
self
.
outputs
=
[
gof
.
result
.
ResultBase
()]
def
grad
(
self
,
inputs
,
gz
):
return
[
None
]
i
=
gof
.
result
.
ResultBase
()
j
=
gof
.
result
.
ResultBase
()
a1
=
retNone
([
i
])
g
=
grad_sources_inputs
([(
a1
.
out
,
1
)],
None
)
a2
=
retNone
([
i
,
j
])
try
:
g
=
grad_sources_inputs
([(
a2
.
out
,
1
)],
None
)
except
ValueError
,
e
:
self
.
failUnless
(
e
[
0
]
is
gradient
.
_msg_badlen
)
return
self
.
fail
()
def
test_stop_on_all_none
(
self
):
"""Test that op.grad() is not called when output grads are all None"""
class
retNone
(
gof
.
op
.
Op
):
def
__init__
(
self
,
arg
,
tst
):
self
.
inputs
=
arg
self
.
outputs
=
[
gof
.
result
.
ResultBase
()]
self
.
tst
=
tst
def
grad
(
self
,
inputs
,
gz
):
self
.
tst
.
fail
()
i
=
gof
.
result
.
ResultBase
()
a1
=
retNone
([
i
],
self
)
g
=
grad_sources_inputs
([(
a1
.
out
,
None
)],
None
)
def
test_no_invalid_graph
(
self
):
"""Test that bprop fails on an invalid graph"""
raise
NotImplementedError
()
def
test_1in_1out
(
self
):
"""Test grad is called correctly for a 1-to-1 op"""
gval
=
gof
.
result
.
ResultBase
()
class
O
(
gof
.
op
.
Op
):
def
__init__
(
self
):
self
.
inputs
=
[
gof
.
result
.
ResultBase
()]
self
.
outputs
=
[
gof
.
result
.
ResultBase
()]
def
grad
(
self
,
x
,
gz
):
return
gval
a1
=
O
()
g
=
grad_sources_inputs
([(
a1
.
outputs
[
0
],
1
)],
None
)
self
.
failUnless
(
g
[
a1
.
inputs
[
0
]]
is
gval
)
def
test_1in_Nout
(
self
):
"""Test grad is called correctly for a 1-to-many op"""
gval
=
gof
.
result
.
ResultBase
()
class
O
(
gof
.
op
.
Op
):
def
__init__
(
self
):
self
.
inputs
=
[
gof
.
result
.
ResultBase
()]
self
.
outputs
=
[
gof
.
result
.
ResultBase
(),
gof
.
result
.
ResultBase
()]
def
grad
(
self
,
x
,
(
gz1
,
gz2
)):
return
gval
a1
=
O
()
g
=
grad_sources_inputs
([(
a1
.
outputs
[
0
],
1
)],
None
)
self
.
failUnless
(
g
[
a1
.
inputs
[
0
]]
is
gval
)
def
test_Nin_1out
(
self
):
"""Test grad is called correctly for a many-to-1 op"""
gval0
=
gof
.
result
.
ResultBase
()
gval1
=
gof
.
result
.
ResultBase
()
class
O
(
gof
.
op
.
Op
):
def
__init__
(
self
):
self
.
inputs
=
[
gof
.
result
.
ResultBase
(),
gof
.
result
.
ResultBase
()]
self
.
outputs
=
[
gof
.
result
.
ResultBase
()]
def
grad
(
self
,
(
x0
,
x1
),
gz
):
return
(
gval0
,
gval1
)
a1
=
O
()
g
=
grad_sources_inputs
([(
a1
.
outputs
[
0
],
1
)],
None
)
self
.
failUnless
(
g
[
a1
.
inputs
[
0
]]
is
gval0
)
self
.
failUnless
(
g
[
a1
.
inputs
[
1
]]
is
gval1
)
def
test_Nin_Nout
(
self
):
"""Test grad is called correctly for a many-to-many op"""
gval0
=
gof
.
result
.
ResultBase
()
gval1
=
gof
.
result
.
ResultBase
()
class
O
(
gof
.
op
.
Op
):
def
__init__
(
self
):
self
.
inputs
=
[
gof
.
result
.
ResultBase
(),
gof
.
result
.
ResultBase
()]
self
.
outputs
=
[
gof
.
result
.
ResultBase
(),
gof
.
result
.
ResultBase
()]
def
grad
(
self
,
(
x0
,
x1
),
(
gz0
,
gz1
)):
return
gval0
,
gval1
a1
=
O
()
g
=
grad_sources_inputs
([(
a1
.
outputs
[
0
],
1
)],
None
)
self
.
failUnless
(
g
[
a1
.
inputs
[
0
]]
is
gval0
)
self
.
failUnless
(
g
[
a1
.
inputs
[
1
]]
is
gval1
)
def
test_some_None_ograds
(
self
):
"""Test grad is called when some output gradients are None"""
class
O
(
gof
.
op
.
Op
):
def
__init__
(
self
,
arg
,
tst
):
self
.
inputs
=
arg
self
.
outputs
=
[
gof
.
result
.
ResultBase
(),
gof
.
result
.
ResultBase
()]
self
.
tst
=
tst
def
grad
(
self
,
inputs
,
g_out
):
return
[
1
]
i
=
gof
.
result
.
ResultBase
()
a1
=
O
([
i
],
self
)
g
=
grad_sources_inputs
([(
a1
.
outputs
[
0
],
1
)],
None
)
self
.
failUnless
(
g
[
i
]
is
1
)
def
test_some_None_igrads
(
self
):
"""Test that traversal works properly when an op return some None"""
class
O
(
gof
.
op
.
Op
):
def
__init__
(
self
,
arg
,
tst
,
grad_ok
):
self
.
inputs
=
arg
self
.
outputs
=
[
gof
.
result
.
ResultBase
(),
gof
.
result
.
ResultBase
()]
self
.
tst
=
tst
self
.
grad_ok
=
grad_ok
def
grad
(
self
,
inputs
,
g_out
):
if
not
self
.
grad_ok
:
self
.
tst
.
fail
()
else
:
return
[
1
,
None
]
i
=
gof
.
result
.
ResultBase
()
j
=
gof
.
result
.
ResultBase
()
k
=
gof
.
result
.
ResultBase
()
a1
=
O
([
i
,
j
],
self
,
True
)
a2
=
O
([
a1
.
outputs
[
1
],
k
],
self
,
True
)
g
=
grad_sources_inputs
([(
a2
.
outputs
[
0
],
1
)],
None
)
self
.
failUnless
(
g
[
i
]
is
1
and
j
not
in
g
and
k
not
in
g
)
a1
=
O
([
i
,
j
],
self
,
True
)
a2
=
O
([
k
,
a1
.
outputs
[
1
]],
self
,
True
)
g
=
grad_sources_inputs
([(
a2
.
outputs
[
0
],
1
)],
None
)
self
.
failUnless
(
g
[
k
]
is
1
and
i
not
in
g
and
j
not
in
g
)
def
test_inputs
(
self
):
"""Test that passing inputs shortens the traversal"""
class
O
(
gof
.
op
.
Op
):
def
__init__
(
self
,
arg
,
tst
,
grad_ok
):
self
.
inputs
=
arg
self
.
outputs
=
[
gof
.
result
.
ResultBase
(),
gof
.
result
.
ResultBase
()]
self
.
tst
=
tst
self
.
grad_ok
=
grad_ok
def
grad
(
self
,
inputs
,
(
g0
,
g1
)):
if
not
self
.
grad_ok
:
self
.
tst
.
fail
()
else
:
if
g1
:
return
[
g0
,
g0
+
g1
]
else
:
return
[
g0
,
g0
]
i
=
gof
.
result
.
ResultBase
()
j
=
gof
.
result
.
ResultBase
()
k
=
gof
.
result
.
ResultBase
()
a1
=
O
([
i
,
j
],
self
,
True
)
a2
=
O
([
k
,
a1
.
outputs
[
1
]],
self
,
True
)
g
=
grad_sources_inputs
([(
a2
.
outputs
[
0
],
1
),
(
a1
.
outputs
[
1
],
4
),
(
a1
.
outputs
[
0
],
3
),
(
a1
.
outputs
[
0
],
3
)],
a1
.
outputs
)
self
.
failUnless
(
g
[
a2
.
inputs
[
0
]]
==
1
)
self
.
failUnless
(
g
[
a2
.
inputs
[
1
]]
==
5
)
self
.
failUnless
(
g
[
a1
.
outputs
[
0
]]
==
6
)
self
.
failUnless
(
g
[
a1
.
outputs
[
1
]]
==
5
)
self
.
failUnless
(
a1
.
inputs
[
0
]
not
in
g
)
self
.
failUnless
(
a1
.
inputs
[
1
]
not
in
g
)
def
test_multiple_sources
(
self
):
"""Test that passing multiple sources works"""
class
O
(
gof
.
op
.
Op
):
def
__init__
(
self
,
arg
,
tst
,
grad_ok
):
self
.
inputs
=
arg
self
.
outputs
=
[
gof
.
result
.
ResultBase
(),
gof
.
result
.
ResultBase
()]
self
.
tst
=
tst
self
.
grad_ok
=
grad_ok
def
grad
(
self
,
inputs
,
(
g0
,
g1
)):
if
not
self
.
grad_ok
:
self
.
tst
.
fail
()
else
:
if
g1
:
return
[
g0
,
g0
+
g1
]
else
:
return
[
g0
,
g0
]
i
=
gof
.
result
.
ResultBase
()
j
=
gof
.
result
.
ResultBase
()
k
=
gof
.
result
.
ResultBase
()
a1
=
O
([
i
,
j
],
self
,
True
)
a2
=
O
([
k
,
a1
.
outputs
[
1
]],
self
,
True
)
g
=
grad_sources_inputs
([(
a2
.
outputs
[
0
],
1
),
(
a1
.
outputs
[
1
],
4
),
(
a1
.
outputs
[
0
],
3
),
(
a1
.
outputs
[
0
],
3
)],
None
)
self
.
failUnless
(
g
[
a2
.
inputs
[
0
]]
==
1
)
self
.
failUnless
(
g
[
a2
.
inputs
[
1
]]
==
5
)
self
.
failUnless
(
g
[
a1
.
outputs
[
0
]]
==
6
)
self
.
failUnless
(
g
[
a1
.
outputs
[
1
]]
==
5
)
self
.
failUnless
(
g
[
a1
.
inputs
[
0
]]
==
6
)
self
.
failUnless
(
g
[
a1
.
inputs
[
1
]]
==
11
)
class
_test_grad
(
unittest
.
TestCase
):
class
O
(
gof
.
op
.
Op
):
def
__init__
(
self
):
self
.
inputs
=
[
gof
.
result
.
ResultBase
(),
gof
.
result
.
ResultBase
()]
self
.
outputs
=
[
gof
.
result
.
ResultBase
(),
gof
.
result
.
ResultBase
()]
self
.
gval0
=
gof
.
result
.
ResultBase
()
self
.
gval1
=
gof
.
result
.
ResultBase
()
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
():
def
matrix
():
return
tensor
.
Tensor
(
'float64'
,
[
0
,
0
])
return
tensor
.
Tensor
(
'float64'
,
[
0
,
0
])
...
@@ -17,14 +288,11 @@ def matrices(n):
...
@@ -17,14 +288,11 @@ def matrices(n):
return
[
matrix
()
for
i
in
xrange
(
n
)]
return
[
matrix
()
for
i
in
xrange
(
n
)]
class
_testNone
(
unitTest
.
TestCase
):
def
test0
(
self
):
class
_testCase_matinv
:
# (unittest.TestCase):
class
_testCase_matinv
:
# (unittest.TestCase):
def
setUp
(
self
):
def
setUp
(
self
):
numpy
.
random
.
seed
(
1
)
numpy
.
random
.
seed
(
1
)
def
matinv
(
self
,
dim
):
def
matinv
(
self
,
dim
):
# symbolic program
# symbolic program
a
,
b
=
matrices
(
2
)
a
,
b
=
matrices
(
2
)
...
@@ -55,15 +323,6 @@ class _testCase_matinv:# (unittest.TestCase):
...
@@ -55,15 +323,6 @@ class _testCase_matinv:# (unittest.TestCase):
class
_testCase_old
:
#(unittest.TestCase):
class
_testCase_old
:
#(unittest.TestCase):
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
):
def
setUp
(
self
):
numpy
.
random
.
seed
(
1
)
numpy
.
random
.
seed
(
1
)
...
@@ -143,37 +402,6 @@ class _testCase_old:#(unittest.TestCase):
...
@@ -143,37 +402,6 @@ class _testCase_old:#(unittest.TestCase):
self
.
assertEqual
(
max
,
min
)
self
.
assertEqual
(
max
,
min
)
self
.
assertEqual
(
max
,
0.0
)
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
):
def
tearDown
(
self
):
core
.
pop_mode
()
core
.
pop_mode
()
...
...
gradient.py
浏览文件 @
4336f227
import
gof
import
gof
,
gof
.
result
_msg_retNone
=
'op.grad(...) returned None, consider returning [None]'
_msg_badlen
=
'op.grad(...) returned wrong number of gradients'
def
_unpack_result
(
lst
):
def
_unpack_result
(
lst
):
if
len
(
lst
)
>
1
:
if
len
(
lst
)
>
1
:
return
lst
return
lst
else
else
:
return
lst
[
0
]
return
lst
[
0
]
def
_pack_result
(
arg
):
def
_pack_result
(
arg
):
if
gof
.
result
.
is_result
(
arg
):
return
[
arg
]
if
isinstance
(
arg
,
gof
.
result
.
ResultBase
):
return
arg
return
[
arg
]
else
:
return
arg
def
grad_sources_inputs
(
sources
,
inputs
):
def
grad_sources_inputs
(
sources
,
graph_
inputs
):
"""Return a dictionary mapping each result necessary for a source to its gradient
"""Return a dictionary mapping each result necessary for a source to its gradient
sources - a list of gradient sources (explained below)
sources - a list of gradient sources (explained below)
inputs - a list of results considered to be constant
graph_
inputs - a list of results considered to be constant
A gradient source is a pair (r, g_r), in which r is a result, and g_r is a
A gradient source is a pair (r, g_r), in which r is a result, and g_r is a
result that is a gradient wrt r.
result that is a gradient wrt r.
...
@@ -49,33 +54,37 @@ def grad_sources_inputs(sources, inputs):
...
@@ -49,33 +54,37 @@ def grad_sources_inputs(sources, inputs):
None instead of a result instance.
None instead of a result instance.
"""
"""
gmap
=
{}
gmap
=
{}
for
(
r
,
g_r
)
in
self
.
sources
:
for
(
r
,
g_r
)
in
sources
:
if
r
in
gmap
:
if
g_r
is
not
None
:
gmap
[
r
]
=
gmap
[
r
]
+
dr
if
r
in
gmap
:
else
:
gmap
[
r
]
=
gmap
[
r
]
+
g_r
gmap
[
r
]
=
dr
else
:
gmap
[
r
]
=
g_r
outputs
=
gmap
.
keys
()
graph_outputs
=
gmap
.
keys
()
if
inputs
is
None
:
if
graph_
inputs
is
None
:
inputs
=
gof
.
graph
.
inputs
(
outputs
)
graph_inputs
=
gof
.
graph
.
inputs
(
graph_
outputs
)
for
op
in
gof
.
graph
.
io_toposort
(
inputs
,
outputs
)
.
__reversed__
():
for
op
in
gof
.
graph
.
io_toposort
(
graph_inputs
,
graph_outputs
)
.
__reversed__
():
g_outputs
=
[
gmap
[
o
]
for
o
in
self
.
outputs
]
g_outputs
=
[
gmap
.
get
(
o
,
None
)
for
o
in
op
.
outputs
]
if
all
(
map
(
lambda
x
:
x
is
None
,
g_outputs
)):
continue
#if all output gradients are None, continue
output_arg
=
unpack_singleton
(
g_outputs
)
if
all
(
map
(
lambda
x
:
x
is
None
,
g_outputs
)):
continue
input_arg
=
unpack_singleton
(
op
.
inputs
)
output_arg
=
_unpack_result
(
g_outputs
)
input_arg
=
_unpack_result
(
op
.
inputs
)
op_grad
=
op
.
grad
(
input_arg
,
output_arg
)
op_grad
=
op
.
grad
(
input_arg
,
output_arg
)
if
op_grad
is
None
:
if
op_grad
is
None
:
raise
Exception
(
'If you really mean for grad(...) to return None,
raise
ValueError
(
_msg_retNone
,
op
.
__class__
)
please return [None]'
,
op
.
__class__
)
g_inputs
=
_pack_result
(
op_grad
)
g_inputs
=
pack_singleton
(
op_grad
)
if
len
(
g_inputs
)
!=
len
(
op
.
inputs
):
assert
len
(
g_inputs
)
==
len
(
op
.
inputs
)
raise
ValueError
(
_msg_badlen
,
op
.
__class__
,
for
r
,
g_r
in
zip
(
self
.
inputs
,
g_inputs
):
len
(
g_inputs
),
len
(
op
.
inputs
))
for
r
,
g_r
in
zip
(
op
.
inputs
,
g_inputs
):
if
g_r
is
not
None
:
if
g_r
is
not
None
:
if
r
in
gmap
:
if
r
in
gmap
:
gmap
[
r
]
=
gmap
[
r
]
+
g_r
gmap
[
r
]
=
gmap
[
r
]
+
g_r
...
@@ -83,17 +92,16 @@ def grad_sources_inputs(sources, inputs):
...
@@ -83,17 +92,16 @@ def grad_sources_inputs(sources, inputs):
gmap
[
r
]
=
g_r
gmap
[
r
]
=
g_r
return
gmap
return
gmap
def
diff
(
cost
,
param
):
def
grad
(
cost
,
param
):
"""Return symbolic expression of gradient of <cost> wrt <param>.
"""Return symbolic expression of gradient of <cost> wrt <param>.
If <param> is a list, then return a list containing the gradient of cost wrt
If <param> is a list, then return a list containing the gradient of cost wrt
each element of the list.
each element of the list.
"""
"""
inputs
=
gof
.
graph
.
inputs
([
cost
])
inputs
=
gof
.
graph
.
inputs
([
cost
])
gmap
=
grad_sources_inputs
([(
cost
,
1.0
)],
inputs
)
gmap
=
grad_sources_inputs
([(
cost
,
1.0
)],
inputs
)
if
isinstance
(
param
,
lst
):
if
isinstance
(
param
,
l
i
st
):
return
[
gmap
[
p
]
for
p
in
param
]
return
[
gmap
.
get
(
p
,
None
)
for
p
in
param
]
else
:
else
:
return
gmap
[
param
]
return
gmap
.
get
(
param
,
None
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论