我已經使用此代碼進行類別檢測..import numpy as np# Words -> categorycategories = {word: key for key, words in data.items() for word in words}# Load the whole embedding matrixembeddings_index = {}with open('glove.6B.100d.txt', encoding="utf8") as f: for line in f: values = line.split() word = values[0] embed = np.array(values[1:], dtype=np.float32) embeddings_index[word] = embedprint('Loaded %s word vectors.' % len(embeddings_index))# Embeddings for available wordsdata_embeddings = {key: value for key, value in embeddings_index.items() if key in categories.keys()}# Processing the querydef process(query): query_embed = embeddings_index[query] scores = {} for word, embed in data_embeddings.items(): category = categories[word] dist = query_embed.dot(embed) dist /= len(data[category]) scores[category] = scores.get(category, 0) + dist return scores# Testingprint(process('pizza'))輸出{'service': 6.385544379552205, 'ambiance': 3.5752111077308655, 'Food': 12.912149047851562}有沒有辦法讓我只獲得最準確的類別,比如食物??
品類檢測。
慕田峪9158850
2023-01-04 11:28:22