TensorFlow Recommenders: Quickstart
In this tutorial, we build a simple matrix factorization model using the MovieLens 100K dataset with TFRS. We can use this model to recommend movies for a given user.
Import TFRS
from typing import Dict, Text import numpy as np import tensorflow as tf import tensorflow_datasets as tfds import tensorflow_recommenders as tfrs
Read the data
# Ratings data.
ratings = tfds.load('movielens/100k-ratings', split="train")
# Features of all the available movies.
movies = tfds.load('movielens/100k-movies', split="train")
# Select the basic features.
ratings = ratings.map(lambda x: {
"movie_title": x["movie_title"],
"user_id": x["user_id"]
})
movies = movies.map(lambda x: x["movie_title"])
Build vocabularies to convert user ids and movie titles into integer indices for embedding layers:
user_ids_vocabulary = tf.keras.layers.experimental.preprocessing.StringLookup(mask_token=None) user_ids_vocabulary.adapt(ratings.map(lambda x: x["user_id"])) movie_titles_vocabulary = tf.keras.layers.experimental.preprocessing.StringLookup(mask_token=None) movie_titles_vocabulary.adapt(movies)
Define a model
We can define a TFRS model by inheriting from tfrs.Model and implementing the compute_loss method:
class MovieLensModel(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, movie_model: tf.keras.Model, task: tfrs.tasks.Retrieval): super().__init__() # Set up user and movie representations. self.user_model = user_model self.movie_model = movie_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["user_id"]) movie_embeddings = self.movie_model(features["movie_title"]) return self.task(user_embeddings, movie_embeddings)
Define the two models and the retrieval task.
# Define user and movie models. user_model = tf.keras.Sequential([ user_ids_vocabulary, tf.keras.layers.Embedding(user_ids_vocabulary.vocab_size(), 64) ]) movie_model = tf.keras.Sequential([ movie_titles_vocabulary, tf.keras.layers.Embedding(movie_titles_vocabulary.vocab_size(), 64) ]) # Define your objectives. task = tfrs.tasks.Retrieval(metrics=tfrs.metrics.FactorizedTopK( movies.batch(128).map(movie_model) ) )
Fit and evaluate it.
Create the model, train it, and generate predictions:
# Create a retrieval model.
model = MovieLensModel(user_model, movie_model, task)
model.compile(optimizer=tf.keras.optimizers.Adagrad(0.5))
# Train for 3 epochs.
model.fit(ratings.batch(4096), 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(movies.batch(100).map(model.movie_model), movies)
# Get some recommendations.
_, titles = index(np.array(["42"]))
print(f"Top 3 recommendations for user 42: {titles[0, :3]}")
相关推荐
基于线性车辆三自由度动力学+无迹卡尔曼滤波UKF估计模型(横摆角速度+质心侧偏角+车速)联合仿真
内容概要:通过构建基于线性车辆三自由度动力学模型与无迹卡尔曼滤波(UKF)的联合仿真系统,实现对车辆关键状态参数的高精度估计,重点包括横摆角速度、质心侧偏角和车速。该方法利用车辆动力学模型描述其横向、纵向及横摆运动行为,结合UKF这一适用于非线性系统的先进状态估计算法,有效处理传感器噪声和模型不确定性,从而在缺乏直接测量手段的情况下,实现对难以获取状态量的可靠估计。整个仿真框架通常在MATLAB/Simulink环境中搭建,能够模拟真实驾驶工况,验证算法的有效性和鲁棒性,为车辆稳定性控制、自动驾驶感知与决策等高级功能提供重要的状态信息支撑。; 适合人群:具备车辆动力学、现代控制理论和MATLAB/Simulink仿真基础,从事汽车工程、自动驾驶或状态估计相关研究的研发人员与研究生。; 使用场景及目标:① 深入理解并掌握非线性系统状态估计的核心方法,特别是UKF的原理与实现;② 学习如何将理论化的车辆动力学模型转化为可用于仿真的模块;③ 实践并验证车辆关键状态(如质心侧偏角)的间接估计技术,服务于车辆动态控制系统的设计与测试。; 阅读建议:此资源以开发仿真模型为核心,不仅关注代码和模型的实现,更强调对车辆动力学机理和滤波算法设计思路的理解。学习者应结合理论知识,动手搭建并调试仿真模型,通过改变工况和参数来观察估计效果,从而深刻领会系统辨识与状态估计在实际工程中的应用价值。
基于PID控制的ABS汽车防抱死制动系统、车轮滑移率+最优滑移率Carsim和Simulink联合仿真(带参考文献)
内容概要:本文档主要围绕基于PID控制的汽车防抱死制动系统(ABS)开展研究,重点聚焦于车轮滑移率与最优滑移率的精确控制。通过Carsim与Simulink联合仿真平台,构建整车动力学模型与ABS控制器,利用PID算法实时调节制动压力,使实际滑移率逼近理论最优值,从而最大化轮胎与路面间的纵向附着力并保障横向稳定性,有效提升紧急制动工况下的制动效能与操控安全性。研究涵盖了从车辆建模、滑移率特性分析、PID控制器设计到联合仿真验证的全过程,并提供了完整的仿真模型、MATLAB/Simulink代码及参考文献,为相关领域的科研与工程实践提供了详实的技术参考。; 适合人群:具备车辆工程、自动化或控制理论基础知识,从事汽车电控系统开发、自动驾驶或车辆动力学仿真的研究生、工程师及科研人员。; 使用场景及目标:① 实现ABS系统的核心控制逻辑,掌握滑移率控制的关键技术;② 学习Carsim与Simulink联合仿真的搭建方法,进行车辆动力学与控制算法的协同仿真;③ 基于PID控制策略优化制动性能,完成相关学术研究或工程项目。; 阅读建议:在学习过程中,应结合所提供的完整仿真模型和代码,深入理解车辆动力学与ABS控制的交互机制,重点分析不同路面附着系数下最优滑移率的设定及PID参数的整定过程,以达到最佳的仿真效果和理论认知。
R语言SCINature绘图模板SCI科研绘图-雷达图
R语言SCINature绘图模板SCI科研绘图--雷达图
JESD82-543 DDR5MRCD03 Multiplexed Rank Registering Clock Driver.rar
JESD82-543 DDR5MRCD03 Multiplexed Rank Registering Clock Driver.rar
【多机器人路径规划】基于动态边权膨胀与节点预留历史机制的多智能体无冲突轨迹生成方法(Matlab代码实现)
内容概要:本文提出了一种基于动态边权膨胀与节点预留历史机制的多智能体无冲突轨迹生成方法,旨在高效解决多机器人路径规划中的碰撞与死锁问题。该方法通过动态调整路径图中边的权重以反映局部拥堵状况,实现路径的实时优化;同时引入节点预留机制,记录各机器人对路径节点的占用历史,确保时空冲突的有效规避。算法融合了路径搜索、冲突检测、权重自适应更新与调度协调等核心模块,形成了完整的无冲突轨迹规划框架,并配套提供了Matlab代码实现,便于算法验证与性能分析。该方法在复杂动态环境中表现出良好的鲁棒性与可扩展性,适用于多机器人系统的协同运动控制。; 适合人群:具备一定编程基础和路径规划基础知识,从事自动化、机器人、智能交通、智能制造等相关领域的科研人员、工程技术人员及研究生。; 使用场景及目标:①应用于仓储物流、智能工厂、无人驾驶车队等多智能体协同作业场景中的路径规划与动态避障;②为解决多机器人系统中的路径冲突、通信协调与死锁预防等关键技术难题提供理论支持与算法工具;③通过Matlab仿真平台复现算法逻辑,进行不同规模与场景下的性能测试、对比分析与算法优化。; 阅读建议:建议读者结合提供的Matlab代码逐模块实现与调试,重点理解动态边权调整策略的设计原理与节点预留机制的实现细节,通过设置不同初始条件与障碍环境开展仿真实验,以全面掌握算法的运行机制与优化潜力。


