Ошибка значения: Вход 0 слоя conv1d_8 несовместим со слоем: : ожидаемый min_ndim=3, найден ndim=2. Полная полученная форма: (Нет, 101)

#tensorflow #keras #deep-learning

Вопрос:

У меня очень простая архитектура, для которой у меня есть 2 входа: 1-й вход принимает встроенный текст, который имеет 339 затемнений для каждого входа, а другой ввод принимает функции, которые являются одним горячим кодом с 101 размером. Ошибка возникает из-за этого второго ввода. Я сначала попробовал inputs_other = Ввод(форма=(101,)), inputs_other = Ввод(batch_input_shape=(101,1)), inputs_other = Ввод(форма=(101,1,нет)), все это дало мне аналогичные ошибки.

 inputs_essay = Input(shape=(max_length_essay,))
embedding_essay = Embedding(vocab_size, 300, weights=[embedding_matrix], trainable=False)(inputs_essay)
lstm1 = LSTM(100,activation='tanh')(embedding_essay)
flat1 = Flatten()(lstm1)
inputs_other = Input(shape=(101,1))
conv1 = Conv1D(filters=512, kernel_size=4, activation='sigmoid')(inputs_other)
conv2 = Conv1D(filters=256, kernel_size=4, activation='tanh')(conv1)
flat2 = Flatten()(conv2)
merged1 = concatenate([flat1, flat2])
dense1 = Dense(512, activation='tanh',kernel_initializer=tf.keras.initializers.HeUniform())(merged1)
drop1 = Dropout(0.5)(dense1)
dense2 = Dense(258, activation='relu',kernel_initializer=tf.keras.initializers.HeUniform())(drop1)
drop2 = Dropout(0.5)(dense2)
dense3 = Dense(64, activation='relu',kernel_initializer=tf.keras.initializers.HeUniform())(drop2)
outputs = Dense(2, activation='softmax')(dense1)
inputs=[inputs_essay, inputs_other]

log_dir="logs\fit\"   datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir,histogram_freq=1, write_graph=True,write_grads=True)
rlrop=ReduceLROnPlateau(monitor='val_accuracy',factor=0.9,patience=1)
lrschedule = LearningRateScheduler(changeLearningRate, verbose=1)
optimizer = tf.keras.optimizers.Adam(0.01)
earlystop = EarlyStopping(monitor='val_accuracy', patience=2, verbose=1)#, min_delta=0.03
filepath="/content/drive/MyDrive/LSTM_Assignment/model_save/best_model_1-{epoch:02d}-{val_loss:.4f}.hdf5"
checkpoint = ModelCheckpoint(filepath=filepath, monitor='val_accuracy', save_best_only=True,  verbose=1, mode='auto') #
callback_list = [Metrics(validation_data=(X_test,y_test)), lrschedule, earlystop, checkpoint,rlrop, terminateNan, tensorboard_callback]

model = Model(inputs=inputs, outputs=outputs)
    # compile
model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy',auc])
    # summarize
print(model.summary())
# plot_model(model, show_shapes=True, to_file='multichannel.png')
model.fit([X_train_essay, X_train_other],y_train,epochs=10, validation_data=([X_test_essay, X_test_other],y_test), batch_size=16) #, callbacks=callback_list

 

Вот обратная связь:

 
Model: "model_1"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_9 (InputLayer)            [(None, 339)]        0                                            
__________________________________________________________________________________________________
input_10 (InputLayer)           [(None, 101, 1)]     0                                            
__________________________________________________________________________________________________
embedding_4 (Embedding)         (None, 339, 300)     15112800    input_9[0][0]                    
__________________________________________________________________________________________________
conv1d_8 (Conv1D)               (None, 98, 512)      2560        input_10[0][0]                   
__________________________________________________________________________________________________
lstm_4 (LSTM)                   (None, 100)          160400      embedding_4[0][0]                
__________________________________________________________________________________________________
conv1d_9 (Conv1D)               (None, 95, 256)      524544      conv1d_8[0][0]                   
__________________________________________________________________________________________________
flatten_8 (Flatten)             (None, 100)          0           lstm_4[0][0]                     
__________________________________________________________________________________________________
flatten_9 (Flatten)             (None, 24320)        0           conv1d_9[0][0]                   
__________________________________________________________________________________________________
concatenate_4 (Concatenate)     (None, 24420)        0           flatten_8[0][0]                  
                                                                 flatten_9[0][0]                  
__________________________________________________________________________________________________
dense_16 (Dense)                (None, 512)          12503552    concatenate_4[0][0]              
__________________________________________________________________________________________________
dense_19 (Dense)                (None, 2)            1026        dense_16[0][0]                   
==================================================================================================
Total params: 28,304,882
Trainable params: 13,192,082
Non-trainable params: 15,112,800
__________________________________________________________________________________________________
None
Epoch 1/10
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-32-5b447e95f1a0> in <module>()
     32 print(model.summary())
     33 # plot_model(model, show_shapes=True, to_file='multichannel.png')
---> 34 model.fit([X_train_essay, X_train_other],y_train,epochs=10, validation_data=([X_test_essay, X_test_other],y_test), batch_size=16) #, callbacks=callback_list

9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    984           except Exception as e:  # pylint:disable=broad-except
    985             if hasattr(e, "ag_error_metadata"):
--> 986               raise e.ag_error_metadata.to_exception(e)
    987             else:
    988               raise

ValueError: in user code:

    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:855 train_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:845 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1285 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2833 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3608 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:838 run_step  **
        outputs = model.train_step(data)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:795 train_step
        y_pred = self(x, training=True)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py:1030 __call__
        outputs = call_fn(inputs, *args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/functional.py:421 call
        inputs, training=training, mask=mask)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/functional.py:556 _run_internal_graph
        outputs = node.layer(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py:1013 __call__
        input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_spec.py:235 assert_input_compatibility
        str(tuple(shape)))

    ValueError: Input 0 of layer conv1d_8 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: (None, 101)
 

Комментарии:

1. Как ясно сказано в ошибке tf.keras.layers.Conv1D , ожидается входной 3D тензор формы с формой: batch_shape (steps, input_dim) . Измените свой X_train_other таким, каким он должен быть 3D . Также вы можете изменить функцию активации на relu из sigmoid в conv1 слое. Спасибо!