[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>张量维度的查看
前言:我们都知道向量是一维的,矩阵是二维的,其实张量就是在此基础上继续增加维度,在对一个深度学习预测股价的项目案例实现过程中遇到了如下的报错,
Using a target size (torch.Size([20])) that is different to the input size
(torch.Size([20, 1])). This will likely lead to incorrect results due to
broadcasting. Please ensure they have the same size. return F.mse_loss(input,
target, reduction=sel
这个报错是由维度不一致的原因造成的,之前对于维度改变这一块的知识也不是很了解,所以对这一块的知识进行了一些学习
<>张量维度处理的常见方法
<>一、维度的查看
* a.shape
* a.size() import torch a = torch.Tensor([[[1]],[[2]],[[2]]]) print(a.size())
print(a.shape) # 输出: # torch.Size([3, 1, 1]) # torch.Size([3, 1, 1])
<>二、维度转换
tensor.view(n1,n2,n3)
** tensor.reshape(n1,n2,…,ni)**
进行张量维度的转换时候,会保持张量内元素的个数不变,根据设置的张量,改变其形状。
attention:如果出现view(-1,2)的情况,这里的-1表示根据设置的值调整张量的维度,即列为2,行数根据列数和元素个数进行调整。
print(a) print(a.view(-1,3)) # 输出: # tensor([[[1.]], # [[2.]], # [[2.]]]) #
tensor([[1., 2., 2.]])
<>三、张量降维
**tensor.squeeze(input, dim=None, out=None) **
功能:将输入张量形状中维度为1的除去
给定dim时去掉对应dim上边的维度,注意去掉的只能是维度为1的dim
# 去掉指定为1的维度 print(torch.squeeze(a, 1).size()) # 去掉所有为1的维度 print(a.squeeze().
size()) # 输出: # torch.Size([3, 1]) # torch.Size([3])
a.squeeze(-1)
在一些代码中经常能够看到括号内为-1的形式,那么括号内为-1是什么意思?
这里的-1表示的是倒数第一个维度,例子如下
a = torch.randn(3,2,1) print(a.size()) print(a.squeeze(-1).size()) # 输出: #
torch.Size([3, 2, 1]) # torch.Size([3, 2])