tensorflow / tensorflow/recommenders
How to add more features to the item_model
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2k
- Forks
- 300
- PR merge metrics
- No merged PRs in 30d
Description
I added new features to the user model according to the method in the tutorial, but there are errors when using the same method to add new features to the item model. What is the reason, or how to add more item features to the item model.
The code is as follows:
user_model:
`class UserModel(tf.keras.Model):
def __init__(self):
super().__init__()
# user_embedding用户id层
self.user_embedding = tf.keras.Sequential( [
tf.keras.layers.experimental.preprocessing.StringLookup(
vocabulary=unique_user_ids, mask_token = None),
tf.keras.layers.Embedding(len(unique_user_ids) + 1, 32),
])
# 时间戳特征层
self.timestamp_embedding = tf.keras.Sequential([
tf.keras.layers.experimental.preprocessing.Discretization(timestamp_buckets.tolist()),
tf.keras.layers.Embedding(len(timestamp_buckets) + 1, 32),
])
self.normalized_timestamp = tf.keras.layers.experimental.preprocessing.Normalization()
self.normalized_timestamp.adapt(bhv_time)
# 购买能力特征
self.normalized_age = tf.keras.layers.experimental.preprocessing.Normalization()
self.normalized_age.adapt(bhv_value)
def call(self, inputs):
# 输入为字典类型
return tf.concat([
self.user_embedding(inputs['user_id']),
self.timestamp_embedding(inputs['bhv_time']),
self.normalized_timestamp(inputs['bhv_time']),
self.normalized_age(inputs['bhv_value']),
], axis = 1)`
item_model:
`class ItemModel(tf.keras.Model):
def __init__(self):
super().__init__()
max_tokens = 1000 # 最大标签数
self.title_embedding = tf.keras.Sequential([
tf.keras.layers.experimental.preprocessing.StringLookup(
vocabulary = unique_item_titles, mask_token = None),
tf.keras.layers.Embedding(len(unique_item_titles) + 1, 32),
])
self.title_vectorizer = tf.keras.layers.experimental.preprocessing.TextVectorization(
max_tokens = max_tokens) # 文本转换为向量
self.title_text_embedding = tf.keras.Sequential([
self.title_vectorizer,
tf.keras.layers.Embedding(max_tokens, 32, mask_zero = True),
tf.keras.layers.GlobalAveragePooling1D(), # 全局均值池化
])
# self.title_vectorizer.adapt(titles)
def call(self, inputs):
# 输入为字典类型
return tf.concat([
self.title_embedding(inputs['item_id']),
self.title_text_embedding(inputs['title']),
], axis = 1)`
`class ItemlensModel(tfrs.models.Model):
def __init__(self,):
super().__init__()
# 查询模型
# self.query_model = UserModel()
self.query_model = tf.keras.Sequential([
UserModel(),
tf.keras.layers.Dense(32)
],name = 'query_name')
# 候选者模型
# self.candidate_model = ItemModel()
self.candidate_model = tf.keras.Sequential([
ItemModel(),
tf.keras.layers.Dense(32)
])
# 任务
self.task = tfrs.tasks.Retrieval(
metrics = tfrs.metrics.FactorizedTopK(
# candidates = items.batch(128).map(self.candidate_model),
candidates = items.batch(128).map(self.candidate_model),
)
)
# 计算损失函数
def compute_loss(self, features, training = False):
query_embeddings = self.query_model({
'user_id': features['user_id'],
'bhv_time': features['bhv_time'],
'bhv_value': features['bhv_value'],
})
item_embeddings = self.candidate_model({
'item_id': features['item_id'],
'title': features['title'],
})
return self.task(query_embeddings, item_embeddings)`
There is no problem with the user model part, and the following error occurs in the item model part:

Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the shown ItemModel and ItemlensModel path with the feature dictionaries used by compute_loss, and capture the full error behind the linked image. Inspect ItemModel.call(), the title_text_embedding pipeline, and the commented title_vectorizer.adapt(titles) step; done means the added item features run through candidate_model without the reported error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, tensorflow
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100