Pytorch学习:torch.nn
记录torch.nn模块中一些函数操作
torch.nn.Linear()
torch.nn.Linear(in_features, out_features, bias=True, device=None, dtype=None)
全连接层: \(y = xA^{T} + b\)
参数
in_features:输入大小out_features: 输出大小bias: 是否增加偏置
- 输入:[*, in_features]
- 输出: [*, out_features]
torch.nn.Embedding()
torch.nn.Embedding(num_embeddings, embedding_dim, padding_idx=None, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, sparse=False, _weight=None, _freeze=False, device=None, dtype=None)
编码,将字符、数字甚至向量编码为向量
参数
num_embeddings: 编码的字典的大小(字典中数据的个数)embedding_dim: 编码后向量的维度padding_idx: 将字典中某个字编码为0(个人理解)
输入: [* ]
输出:[*,embedding_dim ]
举个🌰:
某个字典中有num_embeddings个字(A、B、C...),对这些字进行编码,即每个字对应一个向量,且向量的维度为embedding_dim
例子
embedding = nn.Embedding(10, 3)
input = torch.LongTensor([[1, 2, 4, 5], [4, 3, 2, 9]])
embedding(input)
输出:
tensor([[[-0.0251, -1.6902, 0.7172],
[-0.6431, 0.0748, 0.6969],
[ 1.4970, 1.3448, -0.9685],
[-0.3677, -2.7265, -0.1685]],
[[ 1.4970, 1.3448, -0.9685],
[ 0.4362, -0.4004, 0.9400],
[-0.6431, 0.0748, 0.6969],
[ 0.9124, -2.3616, 1.1151]]])

浙公网安备 33010602011771号