#python #tensorflow #keras #conv-neural-network #lstm
Вопрос:
Я хотел бы создать глубокую сеть, используя две параллельные сети. Одна из них-2D сеть CNN, а другая-LSTM. Входными данными для обеих сетей является двумерный массив чисел с плавающей запятой. Уникальный входной 2D-массив в большую сеть передается каждой из ветвей. Я не знаю, как ввести 2D-массив в ветвь LSTM.
Это мой код:
def conv_branch(X, F1, s=1):
# Convolutional component
X = Conv2D(F1, (3, 3), strides = (s,s), kernel_initializer = glorot_uniform(seed=0))(X)
# Output layer
X = Flatten()(X)
X = Dense(F1, activation='relu', kernel_initializer = glorot_uniform(seed=0))(X)
return X
def LSTM_branch(X, F1, s=2):
X = Flatten()(X)
X = LSTM(32, return_sequences=True)(X)
X = LSTM(32, return_sequences=True)(X)
# Fully connected component
X = Dense(F1, activation='relu', kernel_initializer = glorot_uniform(seed=0))(X)
return X
def concat_stage(X_conv, X_lstm, filters):
# Retrieve Filters
F1, F2, F3 = filters
# Concatenation layer
X = Add()([X_conv, X_lstm])
# Fully connected components
X = Dense(F1, activation='relu', kernel_initializer = glorot_uniform(seed=0))(X)
X = Dense(F2, activation='relu', kernel_initializer = glorot_uniform(seed=0))(X)
X = Dense(F3, activation='relu', kernel_initializer = glorot_uniform(seed=0))(X)
# Output layer
X = Dense(4, activation='linear', kernel_initializer = glorot_uniform(seed=0))(X)
return X
def ConvLSTM(input_shape = (15, 3, 1)):
# Define the input
X = Input(input_shape)
# Conv branch
X_conv = conv_branch(X, 32, s=2)
# LSTM branch
X_lstm = LSTM_branch(X, 32, s=2)
# Concatenation
X_out = concat_stage(X_conv, X_lstm, filters=[64, 32, 16])
# Create model
model = Model(inputs = X, outputs = X_out)
return model
model = ConvLSTM()
model.summary()
В этом и заключается ошибка:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-34-5adcf778f629> in <module>
57 return model
58
---> 59 model = ConvLSTM()
60 model.summary()
<ipython-input-34-5adcf778f629> in ConvLSTM(input_shape)
47
48 # LSTM branch
---> 49 X_lstm = LSTM_branch(X, 32, s=2)
50
51 # Concatenation
<ipython-input-34-5adcf778f629> in LSTM_branch(X, F1, s)
13
14 X = Flatten()(X)
---> 15 X = LSTM(32, return_sequences=True)(X)
16 X = LSTM(32, return_sequences=True)(X)
17
c:python36libsite-packageskeraslayersrecurrent.py in __call__(self, inputs, initial_state, constants, **kwargs)
498
499 if initial_state is None and constants is None:
--> 500 return super(RNN, self).__call__(inputs, **kwargs)
501
502 # If any of `initial_state` or `constants` are specified and are Keras
c:python36libsite-packageskerasenginetopology.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.
c:python36libsite-packageskerasenginetopology.py in assert_input_compatibility(self, inputs)
472 self.name ': expected ndim='
473 str(spec.ndim) ', found ndim='
--> 474 str(K.ndim(x)))
475 if spec.max_ndim is not None:
476 ndim = K.ndim(x)
ValueError: Input 0 is incompatible with layer lstm_8: expected ndim=3, found ndim=2
Заранее спасибо за любую помощь
Комментарии:
1.
LSTM
ожидает ввода3D
тензора с формой[batch, timesteps, feature]
. Можете ли вы поделиться автономным кодом для репликации вашей проблемы, чтобы мы могли попытаться вам помочь. Спасибо!