深度学习笔记(自用)

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]])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值