python - 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. 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))
Thank you very much in advance :)
解决方案
您应该删除一次性对标签进行编码的行。
在行中:
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
您已经对值进行了一次热编码,使其形状为(batch_size, 2, 2)
,但最后一层(密集)输出单个数字,即 shape (batch_size, 1)
。还binary_crossentropy
计算 logits as(batch_size, 1)
和标签 as形状的损失(batch_size, 1)
(对于您的数据集)。
推荐阅读
- linux - 使用文件路径中的通配符检查文件路径是否存在的 Bash 行
- python - 如何使用 python 制作 800x800 控制台窗口
- performance-testing - LoadRunner_如何将一个巨大的 json 响应从第一个动作传递到第二个动作的请求正文中?
- java - 当我们使用 testng 运行测试用例时,上一个类再次运行
- java - 如何解决这个 ClassNotFoundexception?
- xamarin.forms - Xamarin Forms 标签不会在 WPF 平台上换行
- matlab - Matlab 与 Julia 与 Fortran 中的速度
- google-apps-script - 自动与已填写 Google 表单的电子邮件共享视图
- list - 如何从列表内部访问实例方法?
- python - 如何重塑没有列的数组?