arrays - 检查目标 : expected dense_3 to have shape (10, 时出错,但得到形状为 (2,) 的数组?即使标签是 one-hot 编码的

标签 arrays machine-learning keras artificial-intelligence classification

我是机器学习和 Keras 的新手,我正在摆弄一些代码。我正在尝试制作一个图像分类器,可以确定图片是否是猫。 我的问题是,当我将 test_set_y 和 train_set_y 传递到 model.fit() 时,数组形状不匹配。

我在堆栈溢出中搜索了同样的问题,许多解决方案都包括对标签进行one-hot编码。然而,在我对标签进行一次性编码后,问题仍然存在。

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:检查目标时出错:预期dense_3具有形状(10,)但得到形状为(2,)的数组”

最佳答案

看看你的数据,你有两个类,你是 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"))

它会起作用的!

关于arrays - 检查目标 : expected dense_3 to have shape (10, 时出错,但得到形状为 (2,) 的数组?即使标签是 one-hot 编码的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56993476/

相关文章:

C++ 多维数组程序在输出中显示垃圾值

machine-learning - 使用梯度下降实现 SARSA

python - Scikit learn - 如何使用 SVM 和随机森林进行文本分类?

c++ - 深度神经网络的图像识别精度,float 还是 double?

python - keras中的全梯度下降

python - 导入 Keras 会中断多处理

java - 在java中显示来自二维数组数据的图像

我可以使用 &array[0][0][0] 传递声明为 double ***array 的 3D 数组吗?

debugging - 如何在 TensorFlow 和 Keras 的损失函数中打印中间变量?

C++ & 位于新数组前面