简体   繁体   English

model的形状不兼容怎么解决?

[英]How to solve the shapes of model are incompatible?

This is my train & test split shape:这是我的训练和测试拆分形状:

print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
----------------------
(120000, 72)
(12000, 72)
(120000, 6)
(12000, 6)

I reshape the data for CNN:我为 CNN 重塑数据:

X_train = X_train.reshape(len(X_train), X_train.shape[1], 1)
X_test = X_test.reshape(len(X_test), X_test.shape[1], 1)
X_train.shape, X_test.shape
-------------------------------------------------------------------
((120000, 72, 1), (12000, 72, 1))

Making the deep learning function:制作深度学习 function:

def model():
    model = Sequential()
    model.add(Conv1D(filters=64, kernel_size=6, activation='relu', 
                    padding='same', input_shape=(72, 1)))
    model.add(BatchNormalization())
    
    # adding a pooling layer
    model.add(MaxPooling1D(pool_size=(3), strides=2, padding='same'))
    
    model.add(Conv1D(filters=64, kernel_size=6, activation='relu', 
                    padding='same', input_shape=(72, 1)))
    model.add(BatchNormalization())
    model.add(MaxPooling1D(pool_size=(3), strides=2, padding='same'))
    
    model.add(Conv1D(filters=64, kernel_size=6, activation='relu', 
                    padding='same', input_shape=(72, 1)))
    model.add(BatchNormalization())
    model.add(MaxPooling1D(pool_size=(3), strides=2, padding='same'))
    
    model.add(Flatten())
    model.add(Dense(64, activation='relu'))
    model.add(Dense(64, activation='relu'))
    model.add(Dense(3, activation='softmax'))
    
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

When i fit it shows error: Shapes (32, 6) and (32, 3) are incompatible当我适合时它显示错误:Shapes (32, 6) and (32, 3) are incompatible

model = model()
model.summary()
logger = CSVLogger('logs.csv', append=True)
his = model.fit(X_train, y_train, epochs=30, batch_size=32, 
          validation_data=(X_test, y_test), callbacks=[logger])

---------------------------------------------------------------
 ValueError: Shapes (32, 6) and (32, 3) are incompatible

What problem and how can i solve it?什么问题,我该如何解决?

Your targets are 6 dimensional, but your prediction is 3 dimensional, thus the mismatch.您的目标是 6 维的,但您的预测是 3 维的,因此不匹配。 You should have Dense(6) as the last layer of your model, not Dense(3)您应该将 Dense(6) 作为 model 的最后一层,而不是 Dense(3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决 tensorflow CNN 中“:形状不兼容”的错误? - How can I solve error of ": Incompatible shapes" in tensorflow CNN? ValueError:LSTM 模型中的形状不兼容 - ValueError: Shapes are incompatible in LSTM model Keras 2 输入 model,不兼容的形状 - Keras 2 input model, incompatible Shapes 如何解决“变量在检查点中可用,但与模型变量的形状不兼容”? - How to solve "Variable is available in checkpoint, but has an incompatible shape with model variable"? Tensorflow RNN 模型形状不兼容错误 - Tensorflow RNN Model Shapes are Incompatible Error 形状与 Keras 功能 Model 和 VGG16 model 不兼容 - Shapes are incompatible with Keras Functional Model and VGG16 model 在拟合tensorflow2模型时,精度不相容 - Incompatible shapes for Accuracy when fitting tensorflow2 model 加载具有自定义图层的模型时,Keras中的形状不兼容 - Incompatible shapes in Keras when loading a model with custom layer 尝试使用 Keras 训练模型,得到一个 ValueError 说形状不兼容 - Trying to train model using Keras, getting a ValueError saying shapes are incompatible TensorFlow GradCAM - model.fit() - ValueError:形状(无,1)和(无,2)不兼容 - TensorFlow GradCAM - model.fit() - ValueError: Shapes (None, 1) and (None, 2) are incompatible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM