question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add more features to the item_model

See original GitHub issue

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: image

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
markhardingcommented, Aug 26, 2021

I also ran into this issue, and managed to fix it by making items a dict like:

items = items.map(lambda x: {
    "item_id": x['item_id'],
    "item_title": x['item_title'],
}).cache()
0reactions
hugoferrerocommented, Jun 29, 2022

I also ran into this issue, and managed to fix it by making items a dict like:

items = items.map(lambda x: {
    "item_id": x['item_id'],
    "item_title": x['item_title'],
}).cache()

yes, i solved it that way too. items needs to have same variables that item tower.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add new to-do items · Little ASP.NET Core Book
The user will add new to-do items with a simple form below the list: Final form. Adding this feature requires a few steps:...
Read more >
Create New Features From Existing Features - OpenClassrooms
Create New Features From Existing Features. Log in or subscribe for free to enjoy all this course has to offer! Feature Engineering.
Read more >
How to create RecyclerView with multiple view types
The functions needed to define in this class are pretty much same as the adapter class when creating the single view type. For...
Read more >
Model - Minecraft Wiki - Fandom
This feature is exclusive to Java Edition. ... Please expand the section to include this information. Further details may exist on the talk...
Read more >
QStandardItemModel — Qt for Python
Constructs a new item model that initially has rows rows and columns columns, ... this function provides a convenient way to append a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found