import torch
# 随机生成一个2×3的张量,取出最大值和最大值对应的索引(一般做分类标签的时候预测用得上)
a = torch.randn(2, 3)
print(a)
b, c = torch.max(a, 1)
print(b)
print(c)
# 将list转为字符串
keywords_list = ['好的', '交集', '是哦就是', '上帝把', '恩德萨部分']
keywords_string = " ".join(keywords_list)
print(keywords_string)
# 错开配对
ngram_range = 2
input_list = [1, 4, 9, 5, 7, 3]
a = zip(*[input_list[i:] for i in range(ngram_range)]) # *用来解包的作用
print(list(a))
# 1×1×32取后面两维用Q[0],得到1×32,以及torch.cat的用法
import torch
Q = torch.randn(1, 1, 32)
K = torch.randn(1, 1, 32)
print(Q)
print(Q.shape)
print(Q[0].shape)
print('catshape===', torch.cat((Q[0], K[0]), 1).shape)
# F.softmax的用法
import torch
import torch.nn.functional as F
input = torch.randn(3, 4)
print("input=", input)
b = F.softmax(input, dim=0) # 按列SoftMax,列和为1(即0维度进行归一化)
print("b=", b)
c = F.softmax(input, dim=1) # 按行SoftMax,行和为1(即1维度进行归一化)
print("c=", c)
# unsqueeze和squeeze的用法(只对维度=1的生效)
import torch
Q = torch.randn(1, 1, 32)
print(Q.shape)
print(Q.unsqueeze(0).shape) # 在第一维插入一个维度
print(Q.squeeze(0).shape) # 在第一维减少一个维度
import torch
x = torch.tensor([1, 2, 3, 4])
y = torch.unsqueeze(x, 0)
print(y)
# tensor([[1, 2, 3, 4]])
z = torch.unsqueeze(x, 1)
print(z)
# tensor([[1],
# [2],
# [3],
# [4]])
深度学习笔记(自用)
于 2022-10-12 10:33:29 首次发布