torch部分用法(一)

本文介绍了PyTorch中的关键函数用法,包括torch.manual_seed用于设定随机数种子确保可复现实验,torch.Tensor.item用于获取单元素张量的值,以及torch.nn.Embedding用于创建词嵌入矩阵,它根据输入的索引选取对应的嵌入向量。此外,还提及了requires_grad属性在梯度计算中的作用。
摘要由CSDN通过智能技术生成

torch.manual_seed(int)

设置随机数种子,每次参数初始化结果都一样

torch.Tensor.item()

tensor只有一个元素才能调用item方法

torch.nn.Embedding(num_embedding, embedding_dim)

官方解释:
A simple lookup table that stores embeddings of a fixed dictionary and size.

This module is often used to store word embeddings and retrieve them using indices.

The input to the module is a list of indices, and the output is the corresponding word embeddings.

实际上: 调用后产生了size为(num_embedding, embedding_dim)的权重矩阵,把被作用的Tensor的内容当做索引,从权重矩阵里选出对应的行,如图:
图片示例
其中,requires_grad: 用于说明当前量是否需要在计算中保留对应的梯度信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值