当前位置: 首页 > news >正文

PyTorch 中nn.Embedding

核心参数与用法
nn.Embedding的核心参数:

num_embeddings:嵌入表的大小(即离散特征的总类别数,如词汇表大小)。
embedding_dim:每个嵌入向量的维度(输出向量的长度)。
padding_idx(可选):指定一个索引,其对应的嵌入向量将始终为 0(用于处理填充符号)。

import torch
import torch.nn as nn# 定义嵌入层:词汇表大小为10(索引0-9),嵌入维度为3
embedding = nn.Embedding(num_embeddings=10, embedding_dim=3)# 输入:形状为(batch_size, seq_len)的整数张量(索引必须在[0, num_embeddings-1]范围内)
input_indices = torch.tensor([[1, 3, 5], [2, 4, 6]])  # 批量大小为2,序列长度为3# 前向传播:获取嵌入向量
output_embeddings = embedding(input_indices)print("输入形状:", input_indices.shape)  # 输出:torch.Size([2, 3])
print("输出形状:", output_embeddings.shape)  # 输出:torch.Size([2, 3, 3])(每个索引被映射为3维向量)
print("输出内容:\n", output_embeddings)
输入形状: torch.Size([2, 3])
输出形状: torch.Size([2, 3, 3])
输出内容:tensor([[[ 0.5095,  0.3979, -1.7759],[-0.1456,  1.6262,  0.3929],[ 0.8530, -0.6685,  1.6823]],[[ 1.0323, -0.0969, -0.6512],[ 0.2309, -1.5649,  0.7431],[-0.3285, -0.2512, -0.1028]]], grad_fn=<EmbeddingBackward0>)
Parameter containing:
tensor([[-1.8749,  0.2108,  0.4401],[ 0.5095,  0.3979, -1.7759],[ 1.0323, -0.0969, -0.6512],[-0.1456,  1.6262,  0.3929],[ 0.2309, -1.5649,  0.7431],[ 0.8530, -0.6685,  1.6823],[-0.3285, -0.2512, -0.1028],[-0.1919,  0.2022, -0.2425],[-0.7266,  1.3337, -0.7980],[ 0.0791, -0.7093,  0.2264]], requires_grad=True)

文章转载自:

http://emWx3BoM.zpfqh.cn
http://ZLpveWcp.zpfqh.cn
http://ISSeuXWs.zpfqh.cn
http://kK96PK6I.zpfqh.cn
http://AdgRkZsB.zpfqh.cn
http://lPCx1or8.zpfqh.cn
http://2hEuRrEL.zpfqh.cn
http://PZq19c7W.zpfqh.cn
http://V9dcTr93.zpfqh.cn
http://BhxRWuXa.zpfqh.cn
http://nV7xGqdi.zpfqh.cn
http://XLZg1DYk.zpfqh.cn
http://vjPLbXnV.zpfqh.cn
http://1kWkOj4k.zpfqh.cn
http://pJWhyo69.zpfqh.cn
http://uWuJ3hSL.zpfqh.cn
http://bt442vrM.zpfqh.cn
http://zySaHoQH.zpfqh.cn
http://gPKb4me8.zpfqh.cn
http://QFPYSHGx.zpfqh.cn
http://kGn1I7ON.zpfqh.cn
http://Gf2VnYnZ.zpfqh.cn
http://wv5YfNOS.zpfqh.cn
http://KXlXpZIM.zpfqh.cn
http://NnwWzNM2.zpfqh.cn
http://rP6KLscW.zpfqh.cn
http://WgvttS36.zpfqh.cn
http://LGDtJeWf.zpfqh.cn
http://RuOy3T2Z.zpfqh.cn
http://1IEYZik1.zpfqh.cn
http://www.dtcms.com/a/372889.html

相关文章:

  • Linux之环境变量(内容由浅入深,层层递进)
  • Linux control group笔记
  • 【Nginx】性能优化与实战(上)
  • LangChain RetrievalQA
  • MybatisPlus开启多租户三步快速集成
  • 现代Web应用前后端架构设计与Python实战
  • YOLO介绍(1)
  • 【javaSE】String类
  • 9.渗透-.Linux基础命令(一)(有vi编辑器)
  • LeetCode - LCR 179. 查找总价格为目标值的两个商品
  • ArcGIS Pro 遇到严重的应用程序错误而无法启动
  • 轻松Linux-9.进程间通信
  • 20250908的学习笔记
  • Golang 与 gRPC
  • shareId 的产生与传递链路
  • Go语言实战案例-开发一个JSON格式校验工具
  • AI技术架构与GEO算法原理如何重塑搜索引擎可见性
  • 【AI测试前沿】谷歌Fuzzing安全测试Go语言指南
  • 佰力博检测与您探讨薄膜样品如何测介电常数?
  • jsBridge接入流程
  • TFS-2018《On the convergence of the sparse possibilistic c-means algorithm》
  • ArrayList中的源码解析
  • 详细解析SparkStreaming和Kafka集成的两种方式的区别和优劣
  • 大数据Spark(六十三):RDD-Resilient Distributed Dataset
  • 云原生TodoList Demo 项目,验证云原生核心特性
  • C语言爬虫开发:常见错误与优化方案
  • Linux 应急响应实操 Checklist
  • 【PCIe EP 设备入门学习专栏 -- 8.2.3 Local Bus Controller (LBC) 详细介绍】
  • 将基于 Oracle JDK 17 开发的 Spring Boot 3.2.12 项目迁移到 OpenJDK 17 环境
  • Vue的计算属性