对keras点层感到困惑.点积如何计算? [英] Confused about keras Dot Layer. How is the Dot product computed?
问题描述
我阅读了有关点层的所有文章,但都没有解释如何做,因此将计算输出形状!似乎太标准了! 使用沿特定轴的值如何精确计算?
I read all posts about the Dot Layer but none explains how this and so the output shape is computed! It seems so standard though! How exactly are the values computed with a along a specific axis?
val = np.random.randint(2, size=(2, 3, 4))
a = K.variable(value=val)
val2 = np.random.randint(2, size=(2, 2, 3))
b = K.variable(value=val)
print("a")
print(val)
print("b")
print(val2)
out = Dot(axes = 2)([a,b])
print(out.shape)
print("DOT")
print(K.eval(out))
我得到:
a
[[[0 1 1 1]
[1 1 0 0]
[0 0 1 1]]
[[1 1 1 0]
[0 0 1 0]
[0 1 0 0]]]
b
[[[1 0 1]
[1 0 1]]
[[1 0 1]
[1 1 0]]]
(2, 3, 3)
DOT
[[[ 3. 1. 2.]
[ 1. 2. 0.]
[ 2. 0. 2.]]
[[ 3. 1. 1.]
[ 1. 1. 0.]
[ 1. 0. 1.]]]
我无法用数学和代数矩阵的专业知识来理解这是怎么算出来的?
I cannot understand with my mathematical and algebraic matrix know-how how the heck this is computed?
推荐答案
Dot
产品的工作原理如下.在内部,它正在调用 K.batch_dot
.
Here's how the Dot
product works. Internally it is calling K.batch_dot
.
首先,我认为您可能打算这样做,
First, I think you might have intended to do,
val = np.random.randint(2, size=(2, 3, 4))
a = K.variable(value=val)
val2 = np.random.randint(2, size=(2, 2, 3))
b = K.variable(value=val2) # You have val here
但是幸运的是,您曾经(或者也可能是最初的意图.无论如何要指出)
But fortunately, you had (or could have been your initial intention too. Anyway just pointing out)
b = K.variable(value=val)
如果您具有所需的代码,则将引发错误,因为您希望点积所在的尺寸不匹配.继续
If you had the intended code, it will throw an error because the dimension you want the dot product on, doesn't match. Moving on,
您有
a.shape = (2,3,4)
b.shape = (2,3,4)
首先,您仅在批次尺寸上执行逐元素点.因此该维度保持不变.
First you are only performing element-wise dot over the batch dimension. So that dimension stays that way.
现在,您可以忽略a
和b
的第一维,并考虑两个矩阵(3,4)
和(3,4)
之间的点积,并在最后一个轴上进行点积,结果为
Now you can ignore the first dimension of both a
and b
and consider the dot product between two matrices (3,4)
and (3,4)
and do the dot product over the last axis, which results in a (3,3)
matrix. Now add the batch dimension you get a,
(2,3,3)张量
(2, 3, 3) tensor
现在以您为例.你有
a
[[[0 1 1 1]
[1 1 0 0]
[0 0 1 1]]
[[1 1 1 0]
[0 0 1 0]
[0 1 0 0]]]
b
[[[0 1 1 1]
[1 1 0 0]
[0 0 1 1]]
[[1 1 1 0]
[0 0 1 0]
[0 1 0 0]]]
然后您执行以下两个点积.
Then you do the following two dot products.
# 1st sample
[0 1 1 1] . [0 1 1 1]
[1 1 0 0] . [1 1 0 0]
[0 0 1 1] . [0 0 1 1]
# 2nd sample
[1 1 1 0] . [1 1 1 0]
[0 0 1 0] . [0 0 1 0]
[0 1 0 0] . [0 1 0 0]
这给出了
# 1st sample
[3 1 2]
[1 2 0]
[2 0 2]
# 2nd sample
[ 3 1 1]
[ 1 1 0]
[ 1 0 1]
最后,通过添加缺少的批次尺寸,
Finally by adding the missing batch dimension you get,
[[[ 3. 1. 2.]
[ 1. 2. 0.]
[ 2. 0. 2.]]
[[ 3. 1. 1.]
[ 1. 1. 0.]
[ 1. 0. 1.]]]
这篇关于对keras点层感到困惑.点积如何计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!