ValueError: ..与层不兼容:期望输入形状的轴-1值为20,但接收到的输入形状为(None, 20,637).



我击中了我的模型上的错误信息;'ValueError:层sequential_5的输入0与层不兼容:期望输入形状的轴-1值为20,但接收到的输入形状为(None, 20,637)'

我不知道如何解决这个问题。代码如下;

print(str(audio_train.shape)+''+str(y_train.shape)+''+str(audio_valid.shape))

(700, 20, 637) (700, 2) (236, 20, 637)

model=Sequential()
###first layer
model.add(Dense(100,input_shape=(20,)))
model.add(Activation('relu'))
model.add(Dropout(0.3))
###second layer
model.add(Dense(200))
model.add(Activation('relu'))
model.add(Dropout(0.3))
###third layer
model.add(Dense(100))
model.add(Activation('relu'))
model.add(Dropout(0.3))
###final layer
model.add(Dense(2))
model.add(Activation('softmax'))
model.summary()
adam = tf.keras.optimizers.Adam(learning_rate=0.0001)
model.compile(optimizer = adam, loss = 'categorical_crossentropy', metrics = ['accuracy'])
#fitting the model
Au_model = model.fit(audio_train, y_train, batch_size = 32, epochs = 10, validation_data = (audio_valid, y_valid), verbose=1)

错误信息如下;

ValueError: in user code:
C:Usersserenanaconda3libsite-packagestensorflowpythonkerasenginetraining.py:855 train_function  *
return step_function(self, iterator)
C:Usersserenanaconda3libsite-packagestensorflowpythonkerasenginetraining.py:845 step_function  **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:Usersserenanaconda3libsite-packagestensorflowpythondistributedistribute_lib.py:1285 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:Usersserenanaconda3libsite-packagestensorflowpythondistributedistribute_lib.py:2833 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:Usersserenanaconda3libsite-packagestensorflowpythondistributedistribute_lib.py:3608 _call_for_each_replica
return fn(*args, **kwargs)
C:Usersserenanaconda3libsite-packagestensorflowpythonkerasenginetraining.py:838 run_step  **
outputs = model.train_step(data)
C:Usersserenanaconda3libsite-packagestensorflowpythonkerasenginetraining.py:795 train_step
y_pred = self(x, training=True)
C:Usersserenanaconda3libsite-packagestensorflowpythonkerasenginebase_layer.py:1013 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
C:Usersserenanaconda3libsite-packagestensorflowpythonkerasengineinput_spec.py:251 assert_input_compatibility
raise ValueError(
ValueError: Input 0 of layer sequential_5 is incompatible with the layer: expected axis -1 of input shape to have value 20 but received input with shape (None, 20, 637)

谢谢你的建议。

变化

model.add(Dense(100,input_shape=(20,)))

model.add(Dense(100,input_shape=(20, 637)))

如果你想更程序化一点,你也可以使用input_shape=(audio_train.shape[1], audio_train.shape[2])

相关内容

  • 没有找到相关文章

最新更新