Python 3.x Keras多类预测仅返回1个预测,具有softmax和分类交叉熵

Python 3.x Keras多类预测仅返回1个预测,具有softmax和分类交叉熵,python-3.x,tensorflow,machine-learning,keras,Python 3.x,Tensorflow,Machine Learning,Keras,我正在使用Keras和Tensorflow训练一个模型,该模型根据一些字母的图像预测匹配的字体。“我的文件夹”包含一个单独的文件夹中的数据,每个文件夹中的字母图像形式各异。我训练模型的代码如下所示: LETTER_IMAGES_FOLDER = "datasets" MODEL_FILENAME = "fonts_model.hdf5" MODEL_LABELS_FILENAME = "model_labels.dat" data = pd.read_csv('annotations.csv

我正在使用Keras和Tensorflow训练一个模型,该模型根据一些字母的图像预测匹配的字体。“我的文件夹”包含一个单独的文件夹中的数据,每个文件夹中的字母图像形式各异。我训练模型的代码如下所示:

LETTER_IMAGES_FOLDER = "datasets"
MODEL_FILENAME = "fonts_model.hdf5"
MODEL_LABELS_FILENAME = "model_labels.dat"


data = pd.read_csv('annotations.csv')

paths = list(data['Path'].values)
Y = list(data['Font'].values)

encoder = LabelEncoder()
encoder.fit(Y)
Y = encoder.transform(Y)
Y = np_utils.to_categorical(Y)

data = []

# loop over the input images
for image_file in paths:
    # Load the image and convert it to grayscale
    image = cv2.imread(image_file)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Add a third channel dimension to the image to make Keras happy
    image = np.expand_dims(image, axis=2)

    # Add the letter image and it's label to our training data
    data.append(image)

data = np.array(data, dtype="float") / 255.0

train_x, test_x, train_y, test_y = model_selection.train_test_split(data,Y,test_size = 0.1, random_state = 0)


# Save the mapping from labels to one-hot encodings.
# We'll need this later when we use the model to decode what it's predictions mean
with open(MODEL_LABELS_FILENAME, "wb") as f:
    pickle.dump(encoder, f)

# Build the neural network!
model = Sequential()

# First convolutional layer with max pooling
model.add(Conv2D(20, (5, 5), padding="same", input_shape=(100, 100, 1), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

# Second convolutional layer with max pooling
model.add(Conv2D(50, (5, 5), padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(500, activation="relu"))
print (len(encoder.classes_))
model.add(Dense(len(encoder.classes_), activation="softmax"))

# Ask Keras to build the TensorFlow model behind the scenes
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])

# Train the neural network
model.fit(train_x, train_y, validation_data=(test_x, test_y), batch_size=32, epochs=2, verbose=1)

# Save the trained model to disk
model.save(MODEL_FILENAME)
一旦创建了模型,我将使用它进行如下预测:

predictions = model.predict(letter_image)
print (predictions) # this has the length of 1
问题是“预测”总是一个大小为1的数组,我不知道为什么。我用的是softmax,分类的交叉熵,最后一层的稠密值大于1。有人能告诉我为什么我没有得到前n名的预测吗


我也尝试过使用二元交叉熵的sigmoid,但得到了相同的结果。我想我还遗漏了更多的东西。

你所说的“前五名”预测是什么意思,为什么你认为你应该得到这样的预测?你在文档中看到了吗?谢谢,我已经更新了这个问题。5是我选择的任意数字,将问题改为n。我假设我总是会按照概率的顺序得到不止一个结果,也许这是一个糟糕的假设?多类分类器确实会将单个测试数据分类到它所训练的
n
类中。但是预测输出应该是单个类。如果您希望它提供多个类,那么这种假设是不正确的。好的,谢谢。我认为多标签分类器可能更适合我的用例。你所说的“前五名”预测是什么意思,为什么你认为你应该得到这样的结果?你在文档中看到了吗?谢谢,我已经更新了这个问题。5是我选择的任意数字,将问题改为n。我假设我总是会按照概率的顺序得到不止一个结果,也许这是一个糟糕的假设?多类分类器确实会将单个测试数据分类到它所训练的
n
类中。但是预测输出应该是单个类。如果您希望它提供多个类,那么这种假设是不正确的。好的,谢谢。我认为多标签分类器可能更适合我的用例。