Рекомендательная система Tensorflow с собственными данными. Проблемы с рекомендуемыми результатами указаны за пределами Python

Программы на Python
Ответить Пред. темаСлед. тема
Гость
 Рекомендательная система Tensorflow с собственными данными. Проблемы с рекомендуемыми результатами указаны за пределами

Сообщение Гость »


I want to make system recommendations using custom data with TensorFlow recommenders, I have followed the tutorial on the official website (https://www.tensorflow.org/recommenders), and I also followed the tutorial at here
problem:
  • The recommended results are given outside of the options provided by the dataset. For example, in my dataset, there are only 3 items, say 1, 2, and 3. However, the recommendations given are items 1, 4, 6, 9, 11, and so on.
My environment: Dataset (sample.csv):
Изображение
The full code:

Код: Выделить всё

from typing import Dict, Text

import numpy as np
import pandas as pd
import tensorflow as tf

import tensorflow_datasets as tfds
import tensorflow_recommenders as tfrs

DATA_URL = "sample.csv"
df = pd.read_csv(DATA_URL)
ratings = tf.data.Dataset.from_tensor_slices(df[['item_id', 'user_id']].values)
items = tf.data.Dataset.from_tensor_slices(df[['item_id']].values)

user_id_vocabulary = tf.keras.layers.IntegerLookup()
user_id_vocabulary.adapt(ratings)

items_vocabulary = tf.keras.layers.IntegerLookup()
items_vocabulary.adapt(items)

class CustomModel(tfrs.Model):
# We derive from a custom base class to help reduce boilerplate. Under the hood,
# these are still plain Keras Models.

def __init__(
self,
user_model: tf.keras.Model,
item_model: tf.keras.Model,
task: tfrs.tasks.Retrieval):
super().__init__()

# Set up user and movie representations.
self.user_model = user_model
self.item_model = item_model

# Set up a retrieval task.
self.task = task

def compute_loss(self, features: Dict[Text, tf.Tensor], training=False) -> tf.Tensor:
# Define how the loss is computed.

user_embeddings = self.user_model(features[0])
item_embeddings = self.item_model(features[1])

return self.task(user_embeddings, item_embeddings)

users_model = tf.keras.Sequential([user_id_vocabulary, tf.keras.layers.Embedding(user_id_vocabulary.vocabulary_size(),64)])

items_model = tf.keras.Sequential([items_vocabulary,tf.keras.layers.Embedding(items_vocabulary.vocabulary_size(),64)])

task = tfrs.tasks.Retrieval(metrics=tfrs.metrics.FactorizedTopK(
items.batch(128).map(items_model)))

model = CustomModel(users_model,items_model,task)
model.compile(optimizer=tf.keras.optimizers.Adagrad(0.5))
model.fit(ratings.batch(8), epochs=3)

# Use brute-force search to set up retrieval using the trained representations.
index = tfrs.layers.factorized_top_k.BruteForce(model.user_model)
index.index_from_dataset(
items.batch(100).map(items_model))

# Get some recommendations.
_, titles = index(np.array([1]))
print(f"Top 3 recommendations for user 1: {titles[0, :3]}")

# print every recommendations.
titlesNumpy = titles.numpy()
for i in titlesNumpy[0]:
print(i)
The result:

Код: Выделить всё

Epoch 1/3
WARNING:tensorflow:5 out of the last 13 calls to  triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing.  For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
3/3 [==============================] - 1s 60ms/step - factorized_top_k/top_1_categorical_accuracy: 0.0000e+00 - factorized_top_k/top_5_categorical_accuracy: 0.0000e+00 - factorized_top_k/top_10_categorical_accuracy: 0.1667 - factorized_top_k/top_50_categorical_accuracy: 1.0000 - factorized_top_k/top_100_categorical_accuracy: 1.0000 - loss: 1.3775 - regularization_loss: 0.0000e+00 - total_loss: 1.3775
Epoch 2/3
3/3 [==============================] - 0s 60ms/step - factorized_top_k/top_1_categorical_accuracy: 0.3333 - factorized_top_k/top_5_categorical_accuracy: 0.3333 - factorized_top_k/top_10_categorical_accuracy: 0.3333 - factorized_top_k/top_50_categorical_accuracy: 1.0000 - factorized_top_k/top_100_categorical_accuracy: 1.0000 - loss: 1.0671 - regularization_loss: 0.0000e+00 - total_loss: 1.0671
Epoch 3/3
3/3 [==============================] - 0s 61ms/step - factorized_top_k/top_1_categorical_accuracy: 0.3333 - factorized_top_k/top_5_categorical_accuracy: 0.3333 - factorized_top_k/top_10_categorical_accuracy: 0.6667 - factorized_top_k/top_50_categorical_accuracy: 1.0000 - factorized_top_k/top_100_categorical_accuracy: 1.0000 - loss: 0.4586 - regularization_loss: 0.0000e+00 - total_loss: 0.4586
Top 3 recommendations for user 1: [1 4 6]
1
4
6
9
11
14
0
3
8
13
how to solve the problem? what's wrong with my code?


Источник: https://stackoverflow.com/questions/740 ... ed-results
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»