[英]TENSORFLOW Can't find a solution for: ValueError: logits and labels must have the same shape ((None, 1) vs (None, 2, 2))
Im completely new to CNN and Im creating a CNN for image recognition.我对 CNN 完全陌生,我正在创建一个用于图像识别的 CNN。 Im trying to adapt the Cats vs dogs structure for my exercise but an error is popping up and I don't know how to solve it:我试图为我的锻炼调整猫与狗的结构,但出现了一个错误,我不知道如何解决它:
Here is my code:这是我的代码:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
img_width, img_height = 64, 64
img_rows, img_cols = 64, 64
# Prepare data to feed the NN
num_classes = 2
# Ask keras which format to use depending on used backend and arrange data as expected
if K.image_data_format() == 'channels_first':
X_train = x_train.reshape(X_train.shape[0], 3, img_rows, img_cols)
X_test = x_test.reshape(X_test.shape[0], 3, img_rows, img_cols)
input_shape = (3, img_width, img_height)
else:
X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 3)
X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 3)
input_shape = (img_width, img_height, 3)
# Incoming data is in uint8. Cast the input data images to be floats in range [0.0-1.0]
X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255
print('x_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
img_width, img_height = 64, 64
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
batch_size = 100
epochs = 10
model.fit(X_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(X_test, y_test))
And the error:和错误:
ValueError: logits and labels must have the same shape ((None, 1) vs (None, 2, 2)) ValueError:logits 和标签必须具有相同的形状 ((None, 1) vs (None, 2, 2))
Thank you very much in advance:)非常感谢您提前:)
You should remove the lines where you one-hot encoded the labels.您应该删除一次性对标签进行编码的行。
In the line:在行中:
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
you have one-hot encoded the values making their shapes to (batch_size, 2, 2)
, but the last layer (Dense) outputs a single number ie of shape (batch_size, 1)
.您已经对值进行了一次热编码,使其形状为(batch_size, 2, 2)
,但最后一层 (Dense) 输出单个数字,即形状(batch_size, 1)
。 Also binary_crossentropy
calculates loss for shapes of logits as (batch_size, 1)
and labels as (batch_size, 1)
(for your dataset). binary_crossentropy
还计算 logits 形状的损失为(batch_size, 1)
和标签为(batch_size, 1)
(对于您的数据集)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.