【问题标题】:I am trying to train a model but got an error says (ValueError: Shapes (16, 16) and (16, 2) are incompatible)我正在尝试训练模型,但收到错误消息(ValueError: Shapes (16, 16) and (16, 2) are incompatible)
【发布时间】:2021-06-17 08:31:32
【问题描述】:
******I am working on voice emotion recognition project and when trying to fit the model I am getting the following error : got an error says (ValueError: Shapes (16, 16) and (16, 2) are incompatible)

首先我会在开始拟合模型之前显示以前的代码:******

from keras.callbacks import ReduceLROnPlateau
    lr_reduce = ReduceLROnPlateau(monitor='val_loss', factor=0.9, patience=20, min_lr=0.000001)
    
mcp_save = ModelCheckpoint('model/aug_noiseNshift_2class2_np.h5', save_best_only=True, monitor='val_loss', mode='min')
mymodel = model.fit(x_traincnn, y_train, batch_size=16, epochs=700, validation_data = (x_testcnn, y_test), callbacks=[mcp_save, lr_reduce])

我收到以下错误:

ValueError: in user code:

    /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:830 train_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:813 run_step  *
        outputs = model.train_step(data)
    /usr/local/lib/python3.7/dist-packages/keras/engine/training.py:771 train_step  *
        loss = self.compiled_loss(
    /usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py:201 __call__  *
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    /usr/local/lib/python3.7/dist-packages/keras/losses.py:142 __call__  *
        losses = call_fn(y_true, y_pred)
    /usr/local/lib/python3.7/dist-packages/keras/losses.py:246 call  *
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper  **
        return target(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/keras/losses.py:1631 categorical_crossentropy
        y_true, y_pred, from_logits=from_logits)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
        return target(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/keras/backend.py:4827 categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py:1161 assert_is_compatible_with
        raise ValueError("Shapes %s and %s are incompatible" % (self, other))

    ValueError: Shapes (16, 16) and (16, 2) are incompatible

你能帮忙吗?

【问题讨论】:

  • 这里是部分代码: from keras.utils import np_utils from sklearn.preprocessing import LabelEncoder X_train = np.array(X_train) y_train = np.array(y_train) X_test = np.array( X_test) y_test = np.array(y_test) lb = LabelEncoder() y_train = np_utils.to_categorical(lb.fit_transform(y_train)) y_test = np_utils.to_categorical(lb.fit_transform(y_test)) x_traincnn = np.expand_dims(X_train, 轴=2) x_testcnn = np.expand_dims(X_test, axis=2) print(X_test.shape , y_train.shape, X_train.shape, y_test.shape )
  • 输出:(288, 216) (1152, 16) (1152, 216) (288, 16)

标签: python pandas keras conv-neural-network voice-recognition


【解决方案1】:

问题是,我制作了一个模型 (16,2) 形状,而它必须是 (16,16)。我的输出有 16 个不同的类,而不是 2 个,我将模型的最后一层或输出层更改为 16 个输出。 我犯了一个简单的错误,因为我是初学者。

【讨论】:

    猜你喜欢
    • 2022-11-18
    • 2021-09-12
    • 2020-11-30
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2021-08-18
    • 2019-07-15
    • 2022-10-22
    相关资源
    最近更新 更多