首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在tensorflow推荐模型中添加经度和纬度作为特征

在tensorflow推荐模型中添加经度和纬度作为特征
EN

Stack Overflow用户
提问于 2022-07-07 00:36:43
回答 1查看 122关注 0票数 0

我在做一个推荐系统。对于类似的查询,我想给出类似的建议。

我从这里学习了一个例子:https://www.tensorflow.org/recommenders/examples/featurization?hl=en

用户创建一个查询。他可以在地图上选择想要的位置,这就是为什么query的功能中有经度和纬度。我想在推荐算法的查询模型中添加经度和纬度。

以下是查询模型。它已经采用了标记化的文本特性:

代码语言:javascript
复制
class QueryModel(tf.keras.Model):
  
  def __init__(self):
    super().__init__()

    max_tokens = 10_000

    self.query_features_vectorizer = tf.keras.layers.TextVectorization(
        max_tokens=max_tokens)

    self.query_features_embedding = tf.keras.Sequential([
      self.query_features_vectorizer,
      tf.keras.layers.Embedding(max_tokens, 64, mask_zero=True),
      tf.keras.layers.GlobalAveragePooling1D(),
    ])

    self.query_features_vectorizer.adapt(query_features)

  def call(self, inputs):
    # Take the input dictionary, pass it through each input layer,
    # and concatenate the result.
    return tf.concat([
        self.query_features_embedding(inputs["query_features"]),
    ], axis=1)

我将查询模型传递到这个排序模型中,该模型的任务是对每个查询-候选人对进行评分:

代码语言:javascript
复制
class RatingsModel(tfrs.models.Model):

  def __init__(self):
    super().__init__()

    # query and warehouse models
    self.query_model = tf.keras.Sequential([
      QueryModel(),
      tf.keras.layers.Dense(16)
    ])
    self.candidate_model = tf.keras.Sequential([
      WarehouseModel(),
      tf.keras.layers.Dense(16)
    ])

    # A small model to take in query and warehouse embeddings and predict ratings.
    # We can make this as complicated as we want as long as we output a scalar
    # as our prediction.
    self.rating_model = tf.keras.Sequential([
        tf.keras.layers.Dense(16, activation="linear"),
        tf.keras.layers.Dense(8, activation="tanh"),
        tf.keras.layers.Dense(1, activation="linear"),
    ])

    self.task = tfrs.tasks.Ranking(
      loss=tf.keras.losses.MeanSquaredError(),
      metrics=[tf.keras.metrics.RootMeanSquaredError("RMSE")]
    )

  def call(self, features):
    query_embeddings = self.query_model({
        "query_features": features["query_features"],
    })

    warehouse_embeddings = self.candidate_model({
        "warehouse_id": features["warehouse_id"],
    })

    return (
        self.rating_model(
            tf.concat([query_embeddings, warehouse_embeddings], axis=1)
        ),
    )

  def compute_loss(self, features, training=False):
    labels = features.pop("similarity")
    
    rating_predictions = self(features)

    # We compute the loss for each task.
    rating_loss = self.task(
        labels=labels,
        predictions=rating_predictions,
    )
    return rating_loss

该算法预测查询中最相关的候选人的评分。

我的问题是:我如何考虑经度和纬度,一般的方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-20 07:30:40

在这个谷歌博客给出了一个解释如何建模地理坐标。您可以使用特征交叉组合经度和纬度特征。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72891147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档