Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
9153516b
提交
9153516b
authored
10月 03, 2012
作者:
Jeremiah Lowin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
removed corner case, wrote grad for tensors more clearly, added more comments
上级
545d6f5c
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
42 行增加
和
23 行删除
+42
-23
basic.py
theano/tensor/basic.py
+42
-23
没有找到文件。
theano/tensor/basic.py
浏览文件 @
9153516b
...
...
@@ -6943,48 +6943,67 @@ class Dot(Op):
x
,
y
=
inp
gz
,
=
grads
xdim
,
ydim
=
x
.
type
.
ndim
,
y
.
type
.
ndim
xdim
,
ydim
,
gdim
=
x
.
type
.
ndim
,
y
.
type
.
ndim
,
gz
.
type
.
ndim
#grad is scalar
if
g
z
.
type
.
n
dim
==
0
:
#grad is scalar
, so x is scalar or vector and y is same as x
if
gdim
==
0
:
xgrad
=
gz
*
y
ygrad
=
gz
*
x
#x is scalar
#x is scalar, y is not scalar, grad.shape == y.shape
elif
xdim
==
0
:
xgrad
=
(
gz
*
y
)
.
sum
()
ygrad
=
x
*
gz
#y is scalar
#x is not scalar, y is scalar, grad.shape == x.shape
elif
ydim
==
0
:
xgrad
=
y
*
gz
ygrad
=
(
gz
*
x
)
.
sum
()
#x is vector, y is matrix
#x is vector, y is matrix, grad is vector
elif
xdim
==
1
and
ydim
==
2
:
xgrad
=
dot
(
gz
,
y
.
T
)
ygrad
=
outer
(
x
.
T
,
gz
)
#x is matrix, y is vector
#x is matrix, y is vector, grad is vector
elif
xdim
==
2
and
ydim
==
1
:
xgrad
=
outer
(
gz
,
y
.
T
)
ygrad
=
dot
(
x
.
T
,
gz
)
#x is matrix, y is matrix
#x is matrix, y is matrix, grad is matrix
elif
xdim
==
ydim
==
2
:
xgrad
=
dot
(
gz
,
y
.
T
)
ygrad
=
dot
(
x
.
T
,
gz
)
#x is tensor, y is vector (corner case)
elif
xdim
>
2
and
ydim
==
1
:
xgrad
=
tensordot
(
y
,
gz
,
0
)
.
transpose
(
range
(
xdim
)[
1
:]
+
[
0
])
ygrad
=
tensordot
(
x
,
gz
,
[
range
(
xdim
-
1
)]
*
2
)
#x or y is tensor
# x or y is tensor, grad is tensor
#
# the grad has the same dim as the dot product output, namely
# (x.shape[:-1] + y.shape[:-2] + [y.shape[-1]]). To get the grad
# wrt x or y, a tensordot is used to sum out non-compatible dims.
#
# for grad x:
# gradient is a tensordot over y and grad, summing out all but
# the second-to-last dim of y and transposed such that the first
# resulting dim goes last.
#
# for grad y:
# gradient is a tensordot over x and grad, summing out all but
# the last dim of x and transposed such that the first resulting
# dim goes last.
else
:
sum0
,
sum1
=
range
(
xdim
),
range
(
xdim
-
1
)
sum0
.
pop
(
-
1
)
dims
=
range
(
ydim
)
dims
[
-
1
:
-
1
]
=
[
dims
.
pop
(
0
)]
ygrad
=
tensordot
(
x
,
gz
,
[
sum0
,
sum1
])
.
transpose
(
dims
)
sum0
,
sum1
=
range
(
ydim
),
range
(
xdim
-
1
,
xdim
+
ydim
-
2
)
sum0
.
pop
(
-
2
)
dims
=
range
(
xdim
)[
1
:]
+
[
0
]
xgrad
=
tensordot
(
y
,
gz
,
[
sum0
,
sum1
])
.
transpose
(
dims
)
x_axes0
=
range
(
ydim
-
2
)
x_axes1
=
range
(
xdim
-
1
,
gdim
)
x_tdims
=
range
(
1
,
xdim
)
+
[
0
]
if
ydim
>=
2
:
x_axes0
+=
[
ydim
-
1
]
xgrad
=
tensordot
(
y
,
gz
,
[
x_axes0
,
x_axes1
])
.
transpose
(
x_tdims
)
y_axes0
=
range
(
xdim
-
1
)
y_axes1
=
range
(
xdim
-
1
)
y_tdims
=
range
(
1
,
ydim
-
1
)
+
[
0
]
if
ydim
>=
2
:
y_tdims
+=
[
ydim
-
1
]
ygrad
=
tensordot
(
x
,
gz
,
[
y_axes0
,
y_axes1
])
.
transpose
(
y_tdims
)
rval
=
xgrad
,
ygrad
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论