Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
4ecb20b1
提交
4ecb20b1
authored
10月 03, 2012
作者:
Jeremiah Lowin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added docstring and examples for tensordot
上级
9153516b
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
80 行增加
和
3 行删除
+80
-3
basic.py
theano/tensor/basic.py
+80
-3
没有找到文件。
theano/tensor/basic.py
浏览文件 @
4ecb20b1
...
...
@@ -7169,10 +7169,15 @@ tensordot_grad = TensorDotGrad
class
TensorDot
(
Op
):
"""Compute tensor-tensor products over the given axes.
See numpy documentation for details.
(http://docs.scipy.org/doc/numpy/reference/generated/numpy.tensordot.html)
"""
Compute tensor-tensor dot products along specified axes.
Given two tensors A and B, TensorDot takes the product of elements
in A and B and sums that result over the provided axes.
See documentation for theano.tensor.tensordot for more detail,
or the NumPy documentation at:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.tensordot.html
"""
@classmethod
...
...
@@ -7266,6 +7271,78 @@ class TensorDot(Op):
def
tensordot
(
x
,
y
=
None
,
axes
=
2
):
"""
Compute tensor-tensor dot products along specified axes.
Given two tensors A and B, TensorDot takes the product of elements
in A and B and sums that result over the provided axes.
:param x: the first tensor variable
:param y: the second tensor variable
:param axes: an integer or array. If an integer, the number of axes
to sum over. If an array, it must have two array
elements containing the axes to sum over in each tensor.
If an integer i, it is converted to an array containing
the last i dimensions of the first tensor and the first
i dimensions of the second tensor:
axes = [range(x.ndim - i, x.ndim), range(i)]
If an array, its two elements must contain compatible axes
of the two tensors. For example, [[1, 2], [2, 0]] means sum
over the 2nd and 3rd axes of x and the 3rd and 1st axes of y.
(Remember axes are zero-indexed!) The 2nd axis of x and the
3rd axis of y must have the same shape; the same is true for
the 3rd axis of x and the 1st axis of y.
:returns: a tensor with shape equal to the concatenation of x's shape
(less any dimensions that were summed over) and y's shape
(less any dimensions that were summed over).
It may be helpful to consider an example to see what tensordot does.
Theano's implementation is identical to NumPy's. Here x has shape (2, 3, 4)
and y has shape (5, 6, 4, 3). The axes to sum over are [[1, 2], [3, 2]] --
note that x.shape[1] == y.shape[3] and x.shape[2] == y.shape[2]; these axes
are compatible. The resulting tensor will have shape (2, 5, 6) -- the
dimensions that are not being summed:
x = np.random.random((2,3,4))
y = np.random.random((5,6,4,3))
#tensordot
z = np.tensordot(x, y, [[1,2],[3,2]])
#loop replicating tensordot
x0, x1, x2 = x.shape
y0, y1, _, _ = y.shape
zloop = np.zeros((x0,y0,y1))
#loop over non-summed indices -- these exist
#in the tensor product.
for i in range(x0):
for j in range(y0):
for k in range(y1):
#loop over summed indices -- these don't exist
#in the tensor product.
for l in range(x1):
for m in range(x2):
zloop[i,j,k] += x[i,l,m] * y[j,k,m,l]
np.all(z == zloop) #true
In an extreme case, no axes may be specified. The resulting tensor
will have shape equal to the concatenation of the shapes of x and y:
z = np.tensordot(x, y, 0)
print(x.shape) #(2,3,4)
print(y.shape) #(5,6,4,3)
print(z.shape) #(2,3,4,5,6,4,3)
See the documentation of np.tensordot for more examples.
"""
if
y
is
None
:
raise
NotImplementedError
(
'The interface to tensordot has changed from '
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论