我在做一个推荐系统。对于类似的查询,我想给出类似的建议。
我从这里学习了一个例子:https://www.tensorflow.org/recommenders/examples/featurization?hl=en
用户创建一个查询。他可以在地图上选择想要的位置,这就是为什么query的功能中有经度和纬度。我想在推荐算法的查询模型中添加经度和纬度。
以下是查询模型。它已经采用了标记化的文本特性:
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)我将查询模型传递到这个排序模型中,该模型的任务是对每个查询-候选人对进行评分:
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该算法预测查询中最相关的候选人的评分。
我的问题是:我如何考虑经度和纬度,一般的方法是什么?
发布于 2022-07-20 07:30:40
在这个谷歌博客给出了一个解释如何建模地理坐标。您可以使用特征交叉组合经度和纬度特征。
https://stackoverflow.com/questions/72891147
复制相似问题