Keras lambda函数点积匹配

Keras lambda函数点积匹配,keras,keras-layer,Keras,Keras Layer,我有以下功能: def transpose_dot(vects): x, y = vects # <x,x> + <y,y> - 2<x,y> return K.dot(x, K.transpose(y)) 结果如下: [[ 1. 1. 1. 2.] [ 1. 2. 2. 4.] [ 1. 2. 2. 4.] [ 2. 4. 4. 8.] 但是,当尝试将其用作Lambda层的函数时,它不起作用 np_

我有以下功能:

def transpose_dot(vects):
    x, y = vects
    # <x,x> + <y,y> - 2<x,y>

    return K.dot(x, K.transpose(y))
结果如下:

[[ 1.  1.  1.  2.]
 [ 1.  2.  2.  4.]
 [ 1.  2.  2.  4.]
 [ 2.  4.  4.  8.]
但是,当尝试将其用作
Lambda
层的函数时,它不起作用

np_x = [[1, 0], [1, 1], [1, 1], [2, 2]]
features = np.array([np_x])
test_input = Input(shape=np.array(np_x).shape)
dot_layer= Lambda(transpose_dot, output_shape=(4,4))([test_input, test_input])
x = Model(inputs=test_input, outputs=dot_layer)
x.predict(features, batch_size=1)
结果

self.fn() if output_subset is None else\
ValueError: Shape mismatch: x has 2 cols (and 4 rows) but y has 4 rows (and 2 cols)
Apply node that caused the error: Dot22(Reshape{2}.0, Reshape{2}.0)
Toposort index: 11
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)]
Inputs shapes: [(4, 2), (4, 2)]
Inputs strides: [(8, 4), (8, 4)]
Inputs values: ['not shown', 'not shown']
Outputs clients: [[Reshape{4}(Dot22.0, MakeVector{dtype='int64'}.0)]]
你知道我错过了什么吗

编辑:
在“我发现了我的错误”的帮助下,添加了错误消息的输出。
函数期望得到(n,m)。但是当使用Lambda函数时,它希望得到(样本数,n,m)。

在公司的帮助下,我发现了我的错误。
函数期望得到(n,m)。但是当使用Lambda函数时,它希望得到(samples,n,m)。

错误消息是什么?你有一个)在Lambda行中丢失…@NassimBen,我添加了错误消息,基本上它抱怨形状,但是'x有2列(和4行),但是y有4列(和2列)`错误消息是什么?Lambda行中缺少a…@NassimBen,我添加了错误消息,基本上它抱怨形状,但是'x有2列(和4行),而y有4列(和2列)`
self.fn() if output_subset is None else\
ValueError: Shape mismatch: x has 2 cols (and 4 rows) but y has 4 rows (and 2 cols)
Apply node that caused the error: Dot22(Reshape{2}.0, Reshape{2}.0)
Toposort index: 11
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)]
Inputs shapes: [(4, 2), (4, 2)]
Inputs strides: [(8, 4), (8, 4)]
Inputs values: ['not shown', 'not shown']
Outputs clients: [[Reshape{4}(Dot22.0, MakeVector{dtype='int64'}.0)]]