使用 tensorflow 和 keras 如何找到给定字符串的 "category"

with tensorflow and keras how to find the "category" of a given string

你好ML/AI新手,

我问这个问题是因为我对机器学习一无所知,ai,e.t.c而且我不知道如何继续,不知道要问什么问题。就算我不小心找到了我也不知道的解决方案。

好的,我遵循了关于 "Text Classification" 的教程并且一切顺利,到这里没有任何问题。 https://www.youtube.com/watch?v=6g4O5UOH304&list=WL&index=8&t=0s

它对 IMDB 评论进行分类并检查评论是 "Positive" 或 "Negative"、“0”还是“1”

我的问题是

假设我有自己的数据集,类似于 IMDB,但我有几个类别作为数字,例如“1,2,3,4,5,6,7,8”而不是“0”和“1” ,9,10,11,12,...." 对于每个字符串。所以我需要它 return 这些数字之一(因为它正在学习,如果不能决定就说两个)

我该怎么办? link 与我需要的教程相关的教程也很棒。

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=3000)

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=250)
test_data = keras.preprocessing.sequence.pad_sequences(test_data, value=word_index["<PAD>"], padding="post", maxlen=250)

def decode_review(text):
    return " ".join([reverse_word_index.get(i, "?") for i in text])

model = keras.Sequential()

model.add(keras.layers.Embedding(10000, 6))
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)

print(results)


for index in range(20):
    test_review = test_data[index]
    predict = model.predict([test_review])
    if predict[0] > 0.8:
        print(decode_review(test_data[index]))
        print(str(predict[0]))
        print(str(test_labels[index]))

您的任务是一个多class class化问题,因此,您必须修改输出层。你有两种可能。

如果你有 1D 整数编码目标,你可以使用 sparse_categorical_crossentropy 作为损失函数,softmax 作为最后一个激活,最后一个密集输出的维度等于 class 的数量来预测

X = np.random.randint(0,10, (1000,100))
y = np.random.randint(0,3, 1000)

model = Sequential([
    Dense(128, input_dim = 100),
    Dense(3, activation='softmax'),
])
model.summary()
model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
history = model.fit(X, y, epochs=3)

否则,如果您对目标进行了单热编码,则可以使用 categorical_crossentropy、softmax 作为最后一个激活,最后一个密集输出的维度等于 class 的数量来预测

X = np.random.randint(0,10, (1000,100))
y = pd.get_dummies(np.random.randint(0,3, 1000)).values

model = Sequential([
    Dense(128, input_dim = 100),
    Dense(3, activation='softmax'),
])
model.summary()
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
history = model.fit(X, y, epochs=3)

使用 softmax 可以将输出解释为总和为 1 的概率分数

当你计算最终的预测时,为了获得预测的 class 你可以简单地以这种方式 np.argmax(model.predict(X), axis=1)

这些是 multiclass text classification 的一些基本教程: