[英]Matmul with different rank
I have 3 tensor 我有3个张量
X
shape (1, c, h, w)
, assume (1, 20, 40, 50)
X
形(1, c, h, w)
,假设(1, 20, 40, 50)
Fx
shape (num, w, N)
, assume (1000, 50, 10)
Fx
形状(num, w, N)
,假设(1000, 50, 10)
Fy
shape (num, N, h)
, assume (1000, 10, 40)
Fy
形状(num, N, h)
,假设(1000, 10, 40)
What I want to do is Fy * (X * Fx)
( *
means matmul
) 我想做的是
Fy * (X * Fx)
( *
表示matmul
)
X * Fx
shape (num, c, h, N)
, assume (1000, 20, 40, 10)
X * Fx
形状(num, c, h, N)
,假设(1000, 20, 40, 10)
Fy * (X * Fx)
shape (num, c, N, N)
, assume (1000, 20, 10, 10)
Fy * (X * Fx)
形状(num, c, N, N)
,假设(1000, 20, 10, 10)
I am using tf.tile
and tf.expand_dims
to do it 我正在使用
tf.tile
和tf.expand_dims
来完成它
but I think it use a lot of memory( tile
copy data right?), and slow 但我认为它使用了大量内存(
tile
复制数据对吗?),而且速度慢
try to find better way that faster and use small memory to accomplish 试着找到更好的方式,更快,并使用小内存来完成
# X: (1, c, h, w)
# Fx: (num, w, N)
# Fy: (num, N, h)
X = tf.tile(X, [tf.shape(Fx)[0], 1, 1, 1]) # (num, c, h, w)
Fx_ex = tf.expand_dims(Fx, axis=1) # (num, 1, w, N)
Fx_ex = tf.tile(Fx_ex, [1, c, 1, 1]) # (num, c, w, N)
tmp = tf.matmul(X, Fxt_ex) # (num, c, h, N)
Fy_ex = tf.expand_dims(Fy, axis=1) # (num, 1, N, h)
Fy_ex = tf.tile(Fy_ex, [1, c, 1, 1]) # (num, c, N, h)
res = tf.matmul(Fy_ex, tmp) # (num, c, N, N)
A case for the mythical einsum
, I guess: 我想是一个神话般的
einsum
案例:
>>> import numpy as np
>>> X = np.random.rand(1, 20, 40, 50)
>>> Fx = np.random.rand(100, 50, 10)
>>> Fy = np.random.rand(100, 10, 40)
>>> np.einsum('nMh,uchw,nwN->ncMN', Fy, X, Fx).shape
(100, 20, 10, 10)
It's should work almost the same in tf
as in numpy
(using uppercase indices isn't allowed in some tf
versions, I saw). 它应该在
tf
和numpy
的工作方式几乎相同(在某些tf
版本中不允许使用大写索引,我看到了)。 Although this admittedly exceeds a regex in unreadability if you've never seen the notation before. 虽然如果你以前从未见过这种表示法,这肯定超过了不可读性的正则表达式。
For otherone may interested 对于其他人可能感兴趣
I think the answer of @phg maybe work 我认为@phg的答案可能有用
But in my case num
h
w
are dynamic, ie None
但对我来说
num
h
w
是动态的,即None
So tf.einsum
in tensorflow r1.0 will raise error, since there are more than one None
shape in one tensor 因此,
tf.einsum
量流r1.0中的tf.einsum将引发误差,因为在一个张量中存在多个None
形状
fortunately, there is a issue and pull request 幸运的是,有一个问题和拉请求
seems can handle situation that there are more than one None
shape 似乎可以处理有多个
None
形状的情况
Need to build from source(master branch) 需要从源码构建(主分支)
I will report the result after I re-build tensorflow 我将在重新构建tensorflow后报告结果
BTW, in tf.einsum
only accept lowercase 顺便说一句,在
tf.einsum
只接受小写
Report 报告
Yes, The newest version of tensorflow (master branch) accept dynamic shape for tf.einsum
是的,最新版本的tensorflow(主分支)接受
tf.einsum
动态形状
and it is huge speed improvement after using tf.einsum
, really awesome 使用
tf.einsum
后速度大大提升,非常棒
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.