gpt4 book ai didi

python - 如何在 Keras 中获得预测值?

转载 作者:太空狗 更新时间:2023-10-29 20:46:54 24 4
gpt4 key购买 nike

在Keras中测试样本评估是这样完成的

score = model.evaluate(testx, testy, verbose=1)

这不会返回预测值。有一个方法 predict 返回预测值

model.predict(testx, verbose=1)

返回

[ 
[.57 .21 .21]
[.19 .15 .64]
[.23 .16 .60]
.....
]

testy 是一个热编码,它的值是这样的

[
[1 0 0]
[0 0 1]
[0 0 1]
]

testy 之类的预测值如何或如何将预测值转换为一个热编码?

注意:我的模型是这样的

# setup the model, add layers
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(classes, activation='softmax'))

# compile model
model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy'])

# fit the model
model.fit(trainx, trainy, batch_size=batch_size, epochs=iterations, verbose=1, validation_data=(testx, testy))

最佳答案

返回的值是每个类别的概率。这些值可能很有用,因为它们表示模型的置信度。

如果你只对概率最大的类感兴趣:

例如[.19 .15 .64] = 2(因为列表中索引2最大)

让模型来吧

Tensorflow 模型有一个内置方法,可以返回最高类别概率的索引。

model.predict_classes(testx, verbose=1)

手动执行

argmax是返回序列中最高值索引的通用函数。

import tensorflow as tf

# Create a session
sess = tf.InteractiveSession()

# Output Values
output = [[.57, .21, .21], [.19, .15, .64], [.23, .16, .60]]

# Index of top values
indexes = tf.argmax(output, axis=1)
print(indexes.eval()) # prints [0 2 2]

关于python - 如何在 Keras 中获得预测值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45587378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com