尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

脑脑之家AI智能体与大模型落地应用-知识点002-one hot编码

脑脑之家AI智能体与大模型落地应用-知识点002-one hot编码 one-hot手把手代码实操importtorch# 假设我们有三个类别: 0, 1, 2num_classes3# 样本标签labelstorch.tensor([0,2,1,0])# 将标签转换为 One-Hot 编码one_hot_labelstorch.nn.functional.one_hot(labels,num_classes)print(Labels:)print(labels)print(\nOne-Hot Encoded Labels:)print(one_hot_labels)代码输出Labels:tensor([0, 2, 1, 0])One-Hot Encoded Labels:tensor([[1, 0, 0],[0, 0, 1],[0, 1, 0],[1, 0, 0]])importtorch# 类别映射: A - 0, B - 1, C - 2category_map{A:0,B:1,C:2}# 样本类别标签labels[A,C,B,A]# 将类别标签转换为整数标签integer_labelstorch.tensor([category_map[label]forlabelinlabels])# 将整数标签转换为 One-Hot 编码num_classeslen(category_map)one_hot_labelstorch.nn.functional.one_hot(integer_labels,num_classes)print(Labels:)print(labels)print(\nInteger Labels:)print(integer_labels)print(\nOne-Hot Encoded Labels:)print(one_hot_labels)
返回列表