gpt4 book ai didi

python - 如何将预测作为二进制输出? - Python( tensorflow )

转载 作者:行者123 更新时间:2023-12-05 02:09:23 26 4
gpt4 key购买 nike

我正在学习使用电影评论作为 tensorflow 数据的文本分类,但是当我得到与标签不同(不是四舍五入,不是二进制)的输出预测时,我陷入了困境。

代码

predict = model.predict([test_review])

print("Prediction: " + str(predict[0])) # [1.8203685e-19]
print("Actual: " + str(test_labels[0])) # 0

预期的输出应该是:

Prediction: [0.]
Actual: 0

输出内容:

Prediction: [1.8203685e-19]
Actual: 0

输出预测应该是 0 或 1,代表评论好不好。

完整代码

import tensorflow as tf
from tensorflow import keras
import numpy as np

data = keras.datasets.imdb

(train_data, train_labels), (test_data, test_labels) = data.load_data(num_words = 10000)

word_index = data.get_word_index()
word_index = {k:(v + 3) for k, v in word_index.items()}

word_index['<PAD>'] = 0
word_index['<START>'] = 1
word_index['<UNK>'] = 2
word_index['<UNUSED>'] = 3

reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])

train_data = keras.preprocessing.sequence.pad_sequences(train_data, value = word_index['<PAD>'], padding = 'post', maxlen = 256)
test_data = keras.preprocessing.sequence.pad_sequences(test_data, value = word_index['<PAD>'], padding = 'post', maxlen = 256)

def decode_review(text):
""" decode the training and testing data into readable words"""
return ' '.join([reverse_word_index.get(i, '?') for i in text])

print("\n")
print(decode_review(test_data[0]))

model = keras.Sequential()
model.add(keras.layers.Embedding(10000, 16))
model.add(keras.layers.GlobalAveragePooling1D())
model.add(keras.layers.Dense(16, activation = 'relu'))
model.add(keras.layers.Dense(1, activation = 'sigmoid'))
model.summary()

model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

x_val = train_data[:10000]
x_train = train_data[10000:]

y_val = train_labels[:10000]
y_train = train_labels[10000:]

fitModel = model.fit(x_train, y_train, epochs = 40,
batch_size = 512,
validation_data = (x_val, y_val),
verbose = 1)

results = model.evaluate(test_data, test_labels)

test_review = test_data[0]
predict = model.predict([test_review])
print("Review: ")
print(decode_review(test_review))
print("Prediction: " + str(predict[0])) # [1.8203685e-19]
print("Actual: " + str(test_labels[0]))
print("\n[loss, accuracy]: ", results)

最佳答案

predict方法替换为predict_classes方法:

model.predict_classes([test_review])

关于python - 如何将预测作为二进制输出? - Python( tensorflow ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59946574/

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