Я пытаюсь создать модель с несколькими входами с помощью Functional API в Keras с такой структурой:
Существует три входа: Team_1_In
, Team_2_In
, Home_In
.Где Team_1_In
и Team_2_In
проходят через слой Embedding
, затем слои BatchNormalization
и Flatten
.Проблема в том, что я пытаюсь добавить слой Flatten
после BatchNormalization
Я получаю эту ошибку:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last) <ipython-input-46-8354b255cfd1> in <module>
15 batch_normalization_2 = BatchNormalization()(team_2_strength)
16
---> 17 flatten_1 = Flatten()(batch_normalization_1)
18 flatten_2 = Flatten()(batch_normalization_2)
19
~/conda/lib/python3.6/site-packages/keras/engine/topology.py in
__call__(self, inputs, **kwargs)
573 # Raise exceptions in case the input is not compatible
574 # with the input_spec specified in the layer constructor.
--> 575 self.assert_input_compatibility(inputs)
576
577 # Collect input shapes to build layer.
~/conda/lib/python3.6/site-packages/keras/engine/topology.py in assert_input_compatibility(self, inputs)
488 self.name + ': expected min_ndim=' +
489 str(spec.min_ndim) + ', found ndim=' +
--> 490 str(K.ndim(x)))
491 # Check dtype.
492 if spec.dtype is not None:
ValueError: Input 0 is incompatible with layer flatten_10: expected min_ndim=3, found ndim=2
Я пытался играть с параметром оси слоя BatchNormalization
, но это не помогло,Вот мой код:
# create embedding layer
from keras.layers import Embedding
from keras.layers import BatchNormalization, Flatten, Dense
from numpy import unique
# Create an embedding layer
team_lookup = Embedding(input_dim=n_teams,
output_dim=1,
input_length=1,
name='Team-Strength')
# create model with embedding layer
from keras.layers import Input, Embedding, Flatten
from keras.models import Model
# Create an input layer for the team ID
teamid_in = Input(shape=(1,))
# Lookup the input in the team strength embedding layer
strength_lookup = team_lookup(teamid_in)
# Flatten the output
strength_lookup_flat = Flatten()(strength_lookup)
# Combine the operations into a single, re-usable model
team_strength_model = Model(teamid_in, strength_lookup_flat, name='Team-Strength-Model')
# Create an Input for each team
team_in_1 = Input(shape=(1,), name='Team-1-In')
team_in_2 = Input(shape=(1,), name='Team-2-In')
# Create an input for home vs away
home_in = Input(shape=(1,), name='Home-In')
# Lookup the team inputs in the team strength model
team_1_strength = team_strength_model(team_in_1)
team_2_strength = team_strength_model(team_in_2)
batch_normalization_1 = BatchNormalization()(team_1_strength)
batch_normalization_2 = BatchNormalization()(team_2_strength)
flatten_1 = Flatten()(batch_normalization_1)
flatten_2 = Flatten()(batch_normalization_2)
# Combine the team strengths with the home input using a Concatenate layer, then add a Dense layer
out = Concatenate()([flatten_1, flatten_2, home_in])
out = Dense(1)(out)