01Pytorch基础 tensor的创建和数据类型

Pytorch基础

01Pytorch基础 tensor的创建和数据类型

1. tensor(张量)是什么

各种类型的数值数据

  • 0阶张量:标量、常数 scaler
  • 1阶张量:向量vector
  • 2阶张量:矩阵matrix

2.tensor的创建方法

.tensor()
.empty()
.ones()
.zeros()
.rand()
.randint()
.randn()
.ones_like()

# 张量的创建
import torch

t0 = torch.tensor(3)
print(t0, t0.shape, t0.size())

# tensor(3) torch.Size([]) torch.Size([])

t1 = torch.tensor([1, 2, 3, 4])
print(t1, t1.shape, t1.size())
# tensor([1, 2, 3, 4]) torch.Size([4]) torch.Size([4])


t2 = torch.tensor([[1, 2], [2, 3]], dtype=torch.double)
print(t2, t2.shape, t2.size())

"""
tensor([[1, 2],
        [2, 3]]) torch.Size([2, 2]) torch.Size([2, 2])

"""
t3 = torch.empty([3, 4])  # 生产随机的3行4列的张量
print(t3)

t4 = torch.ones(3, 4)  # 创建一个3行4列值全为1的tensor
print(t4)

t5 = torch.ones_like(t3)  # tensor转换成与t3类型相同值全为1的tensor
print(t5)

print(torch.rand(3, 4))  # 随机区间 [0, 1)
print(torch.randint(low=10, high=20, size=[3, 4]))  # 随机[10,20),【3,4】 整数
print(torch.randn(3, 4))  # 符合正态分布的

3.tensor的数据类型

取tensor的类型 t1.dtype

  • torch.float == torch.float32
  • torch. double == torch.float64
  • torch.long == torch.int64
  • torch.int =- torch.int32
  • torch.uint8
# 数据类型
print("""-------------------数据类型---------------------""")
# t1.dtype  # 获取tensor的类型
print(t1.dtype)

# dtype  创建的时候指定类型
# torch.float   torch.float32
# torch.double    torch.float64
# torch.long  torch.int64
# torch.int   torch.int32
# torch.uint8

# 使用封装好的函数来创建
# torch.Tensor()
t5 = torch.FloatTensor([3, 4])  # 利用float对象来创建, 直接转换tensor([3., 4.]) torch.float32
print(t5, t5.dtype)
t6 = torch.DoubleTensor(3, 4)  # 转成3,行4列的
print(t6)
# torch.DoubleTensor()
# torch.IntTensor()

# 类型的转换
print("类型的转换")
print(t6.long(), t6.long().dtype)  # t6.long() 数值转换, t6.long().dtype类型转换

4.张量的方法和属性

import torch

# 张量的方法和属性
print("----------------张量的方法和属性--------------")
# t1.shape t1.size() t1.size(0)
t1 = torch.ones(3, 4)
print(t1.shape, t1.size())  # 取出tensor的形状 torch.Size([3, 4]) torch.Size([3, 4])
print(t1.shape[0], t1.size(0))  # t6.size(0)==t6.size(0)

print(t1.dtype)  # torch.float32
print(t1.int())
print(t1.int().dtype)

# 取值
t2 = torch.tensor([[[[[[[[[[[[[[[10121]]]]]]]]]]]]]]])
print(t2)
print(t2.item())  # 取值  item()里面只能是一个值
t3 = torch.tensor([[[[100, 200]]]])
# print(t3.item()) 报错
print(t3[0][0][0][0].numpy())
print(t3[0][0][0].numpy(), type(t3[0][0][0].numpy()))  # 利用numpy去转换

# t1.view()  改变形状
t4 = torch.rand(3, 4, 2)  # 随机区间 [0, 1)  3 * 4 * 2
print(t4)
print(t4.view(-1))  # 拍平成一维
print(t4.view(-1, 2))  # 表示转化成n* 2的矩阵
print(t4.view(6, -1))  # 表示转化成n* 2的矩阵

# 获取阶数
print(t4.dim())
print(t4.size())
t5 = torch.tensor([
    [[1, 2], [3, 4]],
    [[6, 7], [8, 9]],
    [[10, 11], [12, 13]]
])
print(t5.size())  # torch.Size([3, 2, 2])  3行2列每一行2个值
"""
dim	shape
0	3
1	2
2	2

"""
print(t5.max(dim=0))
"""
values=tensor([[10, 11],
        [12, 13]]),
indices=tensor([[2, 2],
        [2, 2]]))
"""
print("=================")
print(t5.max(dim=1))
"""
values=tensor([[ 3,  4],
        [ 8,  9],
        [12, 13]]),
indices=tensor([[1, 1],
        [1, 1],
        [1, 1]]))
"""
print(t5.max(dim=2))
"""
values=tensor([[ 2,  4],
        [ 7,  9],
        [11, 13]]),
indices=tensor([[1, 1],
        [1, 1],
        [1, 1]]))
"""
"""
torch.max()主要找出某个维度的最大值,当dim=0时,表示分别找出第0维对应位置的最大值,
即比较
x[0][0][0]、x[1][0][0]、x[2][0][0];
x[0][0][1]、x[1][0][1]、x[2][0][1];
x[0][1][0]、x[1][1][0]、x[2][1][0];
x[0][1][1]、x[1][1][1]、x[2][1][1]。
然后得到4个值,a表示每个位置上返回的最大值为多少,
b表示该最大值在第0维上的位置。
"""
value, index = t5.max(dim=-1)
# value, index = t5.min(dim=-1)
print(value, index)

t6 = torch.tensor([1, 2, 3, 4, 5])
mean = t6.float().mean()  # 对所以的数字求平均值
std = t6.float().std()  # 对所以的数字求标准差
print(mean, std)

# 下划线的方法 t1.sub_()
t7 = torch.tensor([1, 2, 3])
print(t7.add_(1))  # 不用赋值 t1=t1+1

# 轴交换
t8 = torch.randn(3, 4, 2)
print(t8.size())
t8.permute(4, 3, 2)
print(t8.size())  # 轴交换,换了轴但是维度没有改变。而view()是将数据拍平在改形状。
# t8.view()

我的博客:http://www.lls603.cn/
微信公众号:
请添加图片描述
B站:https://space.bilibili.com/430651363
请添加图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值