我是机器学习和Keras的新手,并且正在弄些代码。我试图做一个图像分类器,可以确定图片是否是猫。 我的问题是,当我将test_set_y和train_set_y传递给model.fit()时,数组形状不匹配。
我已经在堆栈溢出中搜索了相同的问题,许多解决方案都包括对标签进行一次热编码。但是,在对标签一键编码后,问题仍然存在。
def load_dataset():
train_dataset = h5py.File('cat/train_catvnoncat.h5', "r")
train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels
test_dataset = h5py.File('cat/test_catvnoncat.h5', "r")
test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels
classes = np.array(test_dataset["list_classes"][:]) # the list of classes
train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes
train_dataset = h5py.File('cat/train_catvnoncat.h5', "r")
test_dataset = h5py.File('cat/test_catvnoncat.h5', "r")
# Loading the data (cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
# Example of a picture
index = 78
example = train_set_x_orig[index]
plt.imshow(train_set_x_orig[index])
plt.show()
print("y = " + str(train_set_y[:, index]) + ", it's a '" + classes[np.squeeze(train_set_y[:, index])].decode("utf-8") + "' picture.")
print(train_set_x_orig.shape, test_set_x_orig.shape, train_set_y.shape, test_set_y.shape)
# One hot encode the labels------
train_set_y = to_categorical(train_set_y, num_classes=2)
test_set_y = to_categorical(test_set_y, num_classes=2)
print(train_set_y.shape, test_set_y.shape)
train_set_y = np.reshape(train_set_y, (209, 2))
test_set_y = np.reshape(test_set_y, (50, 2))
print(train_set_y.shape, test_set_y.shape)
# CNN ---------
# Forming model
model = Sequential()
# Adding layers
model.add(Conv2D(64, kernel_size=5, strides=1, padding="Same", activation="relu", input_shape=(64, 64, 3)))
model.add(MaxPooling2D(padding="same"))
model.add(Conv2D(128, kernel_size=5, strides=1, padding="same", activation="relu"))
model.add(MaxPooling2D(padding="same"))
model.add(Dropout(0.3))
model.add(Flatten())
model.add(Dense(256, activation="relu"))
model.add(Dropout(0.3))
model.add(Dense(512, activation="relu"))
model.add(Dropout(0.3))
model.add(Dense(10, activation="softmax"))
# Compiling the model
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
# Training the model
model.fit(train_set_x_orig, train_set_y, batch_size=50, epochs=30, validation_data=(test_set_x_orig, test_set_y))
# Evaluate
train_loss_score = model.evaluate(train_set_x_orig, train_set_y)
test_loss_score = model.evaluate(test_set_x_orig, test_set_y)
print(train_loss_score)
print(test_loss_score)
我希望模型能够训练,最后给我带来损失和得分,但是我得到“ ValueError:检查目标时出错:期望density_3的形状为(10,)但形状为(2,)的数组”
答案 0 :(得分:1)
看看您的数据,您有两个是one_hot编码的类:
train_set_y = to_categorical(train_set_y, num_classes=2)
但是在您的模型中,您输出的张量为10:
model.add(Dense(10, activation="softmax"))
这是不一致的!
将最后一层更改为:
model.add(Dense(2, activation="softmax"))
它将起作用!