Pytorch第一课:package-torch(1)之张量初识

微博:https://weibo.com/wangxiaocaoai/profile?rightmod=1&wvr=6&mod=personinfo
微信公众号:搜索"AI躁动街"


本节要点:

1 张量

2 创建张量的方式

3 张量的索引,切片,连接,换位等操作

4 随机抽样的操作

5 序列化(保存与加载)

6 并行化

1 张量Tensor

1.1 判断是否为张量

torch.is_tensor(obj) 如果obj 是一个pytorch张量,则返回True

import torch

# 创建一个普通数值
a = 1
print(torch.is_tensor(a))

# 创建一个float类型的tensor
b = torch.FloatTensor(a)
print(torch.is_tensor(b))

False
True

1.2 判断是否为一维数组

torch.Storage是单个数据类型的连续的一维数组,每个torch.Tensor都具有相同数据类型的相应存储。他是torch.tensor底层数据结构,他除了像Tensor一样定义数值,还可以直接把文件映射到内存中进行操作

torch.is_storage(obj),如何obj 是一个pytorch storage对象,则返回True

# 创建torch.storage,共有以下8中类型
torch.FloatStorage([1,2,3,4,5])
torch.ByteStorage([1,2,3,4,5])
torch.ShortStorage([1,2,3,4,5])
torch.IntStorage([1,2,3,4,5])
torch.LongStorage([1,2,3,4,5])
torch.FloatStorage([1,2,3,4,5])
torch.DoubleStorage([1,2,3,4,5])
torch.ShortStorage([1,2,3,4,5])

# 判断是否为torch.storage
# 随机创建长度为5的一维数组
a = torch.FloatStorage(5)
print(a)
# 判断
print(torch.is_storage(a))
 0.0
 -0.0
 821.166015625
 4.657746965897047e-10
 5.000000953674316
[torch.FloatStorage of size 5]
True

1.3 设置默认的tensor类型

torch.set_default_tensor_type(torch.FloatTensor)
torch.get_default_dtype()
torch.float32

1.4 获取张量中元素的个数

# 随机创建1*2*3维的tensor
a = torch.randn(1,2,3)
print(a)

# 输出元素个数
print(torch.numel(a))
tensor([[[-1.9520, -1.2041, -0.8372],
         [-0.8017, -0.5982,  0.5224]]])
6

1.5 设置打印选项

完全参考numpy

参数:

precision – 浮点数输出的精度位数 (默认为8 )

threshold – 阈值,触发汇总显示而不是完全显示(repr)的数组元素的总数 (默认为1000)

edgeitems – 汇总显示中,每维(轴)两端显示的项数(默认值为3)

linewidth – 用于插入行间隔的每行字符数(默认为80)。Thresholded matricies will ignore this parameter.

profile – pretty打印的完全默认值。 可以覆盖上述所有选项 (默认为short, full)

torch.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None)

2 创建操作 Creation Ops

2.1 创建对角位置为1的2维张量

torch.eye(n, m=None, out=None)

参数:

n (int ) – 行数

m (int, optional) – 列数.如果为None,则默认为n

out (Tensor, optinal) - Output tensor

a = torch.eye(4, 4)
print(a)
tensor([[ 1.,  0.,  0.,  0.],
        [ 0.,  1.,  0.,  0.],
        [ 0.,  0.,  1.,  0.],
        [ 0.,  0.,  0.,  1.]])

2.2 将numpy.ndarray 转换为pytorch的 Tensor

返回的张量tensor和numpy的ndarray共享同一内存空间。修改一个会导致另外一个也被修改。返回的张量不能改变大小。

import numpy as np

# 互换
a = np.array([1,2,3,4])
print('a:',a)
t = torch.from_numpy(a)
print('t:', t)

# 共享内存,修改一个,另一个改变
t[0] = -10
print('a:',a)

# tensor转numpy
 t_a = t.numpy()
a: [1 2 3 4]
t: tensor([ 1,  2,  3,  4])
a: [-10   2   3   4]

2.3 linspace创建1维张量:等差数列

包含在区间start 和 end 上均匀间隔的steps个点。 输出1维张量的长度为steps。

参数:

start (float) – 序列的起始点

end (float) – 序列的最终值

steps (int) – 在start 和 end间生成的样本数

out (Tensor, optional) – 结果张量

a = torch.linspace(start=
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值