gpt4 book ai didi

python - 如何在 Keras 中正确连接 Dense 层和 Lambda 层?

转载 作者:行者123 更新时间:2023-11-30 08:53:18 25 4
gpt4 key购买 nike

我正在使用 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 层?

最佳答案

在这种情况下,您需要设置 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])

关于python - 如何在 Keras 中正确连接 Dense 层和 Lambda 层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53169455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com