如何在Keras中将Dense层正确连接到Lambda层?

时间:2018-11-06 09:52:50

标签: python machine-learning keras keras-layer

我正在使用Keras。在以下代码中,model[a0, a1][b0, b1, b2]作为输入,并以[a0*b0, a0*b1, a0*b2, a1*b0, a1*b1, a1*b2]作为输出:

from keras import backend as K
from keras.models import Model
from keras.models import Input
from keras.layers import Dense

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)

这是测试:

u = np.array([1,2]).reshape(1,2)
v = np.array([3,4,5]).reshape(1,3)
print(model.predict([u,v]))

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

但是,如果我尝试将Dense层连接到Lambda层,则会收到错误消息:

from keras import backend as K
from keras.models import Model
from keras.models import Input
from keras.layers import Dense

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])
d = Dense(2)(c)

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

这是我得到的错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-0f7f977a1e79> in <module>()
      7 b = Input(shape=(3,))
      8 c = Lambda(mix)([a, b])
----> 9 d = Dense(2)(c)
     10 
     11 model = Model(inputs=[a,b], outputs=d)

~\Anaconda3\envs\mind\lib\site-packages\keras\engine\base_layer.py in __call__(self, inputs, **kwargs)
    429                                          'You can build it manually via: '
    430                                          '`layer.build(batch_input_shape)`')
--> 431                 self.build(unpack_singleton(input_shapes))
    432                 self.built = True
    433 

~\Anaconda3\envs\mind\lib\site-packages\keras\layers\core.py in build(self, input_shape)
    864                                       name='kernel',
    865                                       regularizer=self.kernel_regularizer,
--> 866                                       constraint=self.kernel_constraint)
    867         if self.use_bias:
    868             self.bias = self.add_weight(shape=(self.units,),

~\Anaconda3\envs\mind\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name + '` call to the ' +
     90                               'Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

~\Anaconda3\envs\mind\lib\site-packages\keras\engine\base_layer.py in add_weight(self, name, shape, dtype, initializer, regularizer, trainable, constraint)
    247         if dtype is None:
    248             dtype = K.floatx()
--> 249         weight = K.variable(initializer(shape),
    250                             dtype=dtype,
    251                             name=name,

~\Anaconda3\envs\mind\lib\site-packages\keras\initializers.py in __call__(self, shape, dtype)
    207             scale /= max(1., fan_out)
    208         else:
--> 209             scale /= max(1., float(fan_in + fan_out) / 2)
    210         if self.distribution == 'normal':
    211             # 0.879... = scipy.stats.truncnorm.std(a=-2, b=2, loc=0., scale=1.)

TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

如何正确地将Dense层连接到Lambda层?

1 个答案:

答案 0 :(得分:2)

在这种情况下,您需要设置Lambda层的输出形状,因为它不能自动推断。手动通过output_shape

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

或更妙的是,传递一个函数以根据图层的输入张量的形状计算输出形状:

def mix_output_shape(input_shape):
    # input_shape[0] is the shape of first input tensor
    # input_shape[1] is the shape of second input tensor
    return (input_shape[0][0], input_shape[0][1] * input_shape[1][1])

# ...
c = Lambda(mix, mix_output_shape)([a, b])