In using keras Lambda, how do I handle "TypeError: Object arrays are not currently supported"?

In using keras Lambda, how do I handle "TypeError: Object arrays are not currently supported"?

我正在使用 Keras,我想制作一个图层,将 [a0, a1][b0, b1, b2] 作为输入并给出 [a0*b0, a0*b1, a0*b2, a1*b0, a1*b1, a1*b2] 作为输出。我尝试使用Lambda,但我无法成功。这是我的代码:

import numpy as np
from keras.models import Input
from keras.layers import Lambda

def mix(A):
    reshaped = [np.reshape(A[m], (1,np.size(A[m]))) for m in range(len(A))]
    mixed = reshaped[-1]

    for i in range(len(A)-1):
        mixed = np.matmul(np.transpose(reshaped[-i-2]), mixed)
        mixed = np.reshape(mixed, (1,np.size(mixed)))

    return np.reshape(mixed, np.size(mixed))

a = Input(shape=(2,))
b = Input(shape=(3,))
c = Lambda(mix)([a, b])

这是我得到的错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-07bbf930b48b> in <module>()
      1 a = Input(shape=(2,))
      2 b = Input(shape=(3,))
----> 3 c = Lambda(mix)([a, b])

~\Anaconda3\envs\mind\lib\site-packages\keras\engine\base_layer.py in __call__(self, inputs, **kwargs)
    455             # Actually call the layer,
    456             # collecting output(s), mask(s), and shape(s).
--> 457             output = self.call(inputs, **kwargs)
    458             output_mask = self.compute_mask(inputs, previous_mask)
    459 

~\Anaconda3\envs\mind\lib\site-packages\keras\layers\core.py in call(self, inputs, mask)
    685         if has_arg(self.function, 'mask'):
    686             arguments['mask'] = mask
--> 687         return self.function(inputs, **arguments)
    688 
    689     def compute_mask(self, inputs, mask=None):

<ipython-input-31-bbc21320d8af> in mix(A)
      4 
      5     for i in range(len(A)-1):
----> 6         mixed = np.matmul(np.transpose(reshaped[-i-2]), mixed)
      7         mixed = np.reshape(mixed, (1,np.size(mixed)))
      8 

TypeError: Object arrays are not currently supported

但是如果我输入:

a = np.array([1,2])
b = np.array([3,4,5])
print(mix([a,b]))

然后我得到:

[ 3  4  5  6  8 10]

这正是我想要的。但我不知道如何将它正确地放入 Lambda

谁能告诉我如何处理这个问题?我是 Keras 的新手,所以我不知道 LambdaInput 或其他东西的内部结构。


根据 Abhijit 的评论,我将代码更改为:

import numpy as np
import tensorflow as tf
from keras.models import Input
from keras.layers import Lambda

def mix(A):
    reshaped = [tf.reshape(A[m], (1,tf.size(A[m]))) for m in range(len(A))]
    mixed = reshaped[-1]

    for i in range(len(A)-1):
        mixed = tf.matmul(tf.transpose(reshaped[-i-2]), mixed)
        mixed = tf.reshape(mixed, (1,tf.size(mixed)))

    return tf.reshape(mixed, [tf.size(mixed)])

a = Input(shape=(2,))
b = Input(shape=(3,))
c = Lambda(mix)([a, b])

现在我没有收到任何错误,但我认为我的神经网络不正确。因为正在执行:

model = Model(inputs=[a,b], outputs=c)
print(model.summary())

我得到:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_22 (InputLayer)           (None, 2)            0                                            
__________________________________________________________________________________________________
input_23 (InputLayer)           (None, 3)            0                                            
__________________________________________________________________________________________________
lambda_3 (Lambda)               (None,)              0           input_22[0][0]                   
                                                                 input_23[0][0]                   
==================================================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
__________________________________________________________________________________________________

但看到图层lambda_3。输出形状不应该是 (None, 6) 吗?

除了你需要使用 Keras 后端函数(即 keras.backend.*)或直接使用后端函数(即 tf.*th.*)之外,我认为你正在mix 的定义不必要的复杂。可以像这样简单得多:

from keras import backend as K

def mix(ts):
    t0 = K.expand_dims(ts[0], axis=-1)
    t1 = K.expand_dims(ts[1], axis=1)
    return K.batch_flatten(t0 * t1)

a = Input(shape=(2,))
b = Input(shape=(3,))
c = Lambda(mix)([a, b])

model = Model(inputs=[a,b], outputs=c)

这是测试:

# the reshapes are necessary to make them a batch
a = np.array([1,2]).reshape(1,2)
b = np.array([3,4,5]).reshape(1,3)
print(model.predict([a, b]))

# output
[[ 3.  4.  5.  6.  8. 10.]]

此外,有时 Lambda 层可以自动推断输出形状。但是,如果您愿意,可以显式设置其输出形状:

c = Lambda(mix, output_shape=(6,))([a, b])

模型摘要:

Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_9 (InputLayer)            (None, 2)            0                                            
__________________________________________________________________________________________________
input_10 (InputLayer)           (None, 3)            0                                            
__________________________________________________________________________________________________
lambda_5 (Lambda)               (None, 6)            0           input_9[0][0]                    
                                                                 input_10[0][0]                   
==================================================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
__________________________________________________________________________________________________