keras ValueError:形状(None,3)和(None,2)不兼容

soat7uwm  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(184)

这就是我要运行的代码

X_train = data1/255.0
from sklearn.preprocessing import LabelBinarizer
lb = LabelBinarizer()
trainY =lb.fit_transform(label)
from tensorflow.keras.models import Model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import Activation
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Denseenter 
from tensorflow.keras.layers import concatenate
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import SGD
Model = Sequential()
shape = (100,100, 1)
Model.add(Conv2D(32,(3,3),padding="same",input_shape=shape))
Model.add(Activation("relu"))
Model.add(Conv2D(32,(3,3), padding="same"))
Model.add(Activation("relu"))
Model.add(MaxPooling2D(pool_size=(2,2)))
Model.add(Conv2D(64,(3,3), padding="same"))
Model.add(Activation("relu"))
Model.add(MaxPooling2D(pool_size=(2,2)))
Model.add(Flatten())
Model.add(Dense(512))
Model.add(Activation("relu"))
Model.add(Dense(2))
Model.add(Activation("softmax"))
Model.summary()
Model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
print("start training")
Model.fit(X_train,trainY,batch_size=5,epochs=10)

但我收到此错误:输出图形与目标图形兼容
ValueError:形状(None,3)和(None,2)不兼容

jw5wzhpr

jw5wzhpr1#

形状(无,3)属于来自LabelBinarizertrainY
Shape(无,2)是模型输出的形状。
损失函数需要两个形状相同的数组来计算误差。如ValueError所述,这两个数组的形状不同。
请考虑检查二值化器的输出形状,或更改网络最后一层中的神经元数量,以匹配数据集标签列中的唯一值数量。

相关问题