Pytorch框架的学习与使用_day01

1.序列化语义

保存模型的推荐方法

这里主要有两种方法序列化和恢复模型

第一种(recommend)只保存和加载模型参数

torch.save(the_model.state_dict(), PATH)

the_model = TheModelClass(*args, **kwargs)
the_model.load_state_dict(torch.load(PATH))

第二种保存和加载整个模型

torch.save(the_model, PATH)

the_model = torch.load(PATH)

然而在这种情况下,序列化的数据被绑定到特定的类和固定的目录机构,所以当在其他项目中使用时,或者在一些严重的重构器之后它可能会以各种方式break。

 

2.张量Tensors

torch.is_tensor(obj)

如果obj是一个pytorch张量,则返回True

torch.is_storage(obj)

如果obj是一个pytorch storage对象,则返回True

torch.set_default_tensor_type(t)

用于改变tensor中数据的默认类型 如

>>> torch.tensor([1.2, 3]).dtype           # initial default for floating point is torch.float32
torch.float32
>>> torch.set_default_dtype(torch.float64)
>>> torch.tensor([1.2, 3]).dtype           # a new floating point tensor
torch.float64

 

torch.numel(input) -->int

返回input张量中的元素个数 如

>>> a  = torch.randn(1, 2, 3, 4, 5)
>>>torch.numel(a)
120
>>>b = torch.zeros(4, 4)
>>>torch.numel(b)
16

 

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

设置打印选项。完全参考自Numpy

其中参数为:

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

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

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

linewidth - 用于插入行间隔的每行字符数(默认为80) Threshold matricies will ignore thi parameter

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

 

3 创建操作 Creation Ops

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

返回一个2维张量,对角线位置全是1,其他位置都是0.

参数:

n(int) - 行数

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

out(tensor,optional) - Output tensor

返回值类型:Tensor

例子:

>>> import torch
>>> torch.eye(3)
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

 

torch.from_numpy(ndarray) -> Tensor

numpy桥,将numpy.ndarray转换为pytorch的Tensor 返回的张量Tensor 和 numpy的ndarray共享同一内存空间,修改一个会导致另外一个也被修改。返回的张量不能改变大小。

>>> a = numpy.array([1, 2, 3])
>>> t = torch.from_numpy(a)
>>> t
tensor([1, 2, 3], dtype=torch.int32)
>>> t[0] = -1   # 共享同一内存空间
>>> a
array([-1,  2,  3])

 

torch.linspace(start, end, steps = 100, out = None) --> Tensor

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

参数:

start(float) - 序列的起始点

end(float) - 序列的最终值

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

out(Tensor,optional) - 结果张量

例子:

>>> torch.linspace(3, 10, steps = 5)
tensor([ 3.0000,  4.7500,  6.5000,  8.2500, 10.0000])
>>> torch.linspace(-10, 10, steps = 5)
tensor([-10.,  -5.,   0.,   5.,  10.])

 

torch.logspace(start, end, steps = 100, out = None) -->Tensor

返回一个1维张量,包含在区间10^start和10^end上以对数刻度均匀间隔的steps个点。输出1维张量的长度为steps

参数:

start(float) - 序列的起始点

end(float) - 序列的最终值

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

out(Tensor,optional) - 结果张量

例子:

>>> torch.logspace(start = -10, end = 10, steps = 5)
tensor([1.0000e-10, 1.0000e-05, 1.0000e+00, 1.0000e+05, 1.0000e+10])
>>> torch.logspace(start = 0.1, end = 1.0, steps = 5)
tensor([ 1.2589,  2.1135,  3.5481,  5.9566, 10.0000])

 

torch.ones(*sizes, out = None) -->Tensor

返回一个全为1的张量,形状由可变参数sizes定义

参数:

sizes(int...) - 整数序列,定义了输出形状

out(Tensor, optional) - 结果张量

例子:

>>> torch.ones(2, 3)
tensor([[1., 1., 1.],
        [1., 1., 1.]])
>>> torch.ones(5)
tensor([1., 1., 1., 1., 1.])

 

torch.rand(*sizes, out = None) -->Tensor

返回一个张量,包含了从区间[0,1)的均匀分布中抽取的一组随机数,形状由可变参数sizes定义

参数:

sizes(int...) - 整数序列,定义了输出形状

out(Tensor, optional) - 结果张量

例子:

tensor([1., 1., 1., 1., 1.])
>>> torch.rand(4)
tensor([0.0604, 0.8474, 0.3680, 0.2867])
>>> torch.rand(2, 3)
tensor([[0.2124, 0.1746, 0.1262],
        [0.0501, 0.6094, 0.1288]])

 

torch.randn(*sizes, out = None) -->Tensor

返回一个张量,包含了从标准正态分布中抽取一字随机数,形状由可变参数sizes定义

参数:

sizes(int...) - 整数序列,定义了输出形状

out(Tensor, optional) - 结果张量

例子:

>>> torch.randn(4)
tensor([-1.5939, -0.5135,  0.7069, -0.4224])
>>> torch.randn(2,3)
tensor([[-0.3005,  0.3611,  1.7078],
        [ 0.5721,  1.2918,  0.4820]])

 

torch.randperm(n, out = None) -->LongTensor

给定参数n,返回一个从0到n-1的随机整数排列

参数:

n(int) - 上边界(不包含)

例子:

>>> torch.randperm(4)
tensor([2, 3, 1, 0])
>>> torch.randperm(8)
tensor([2, 4, 5, 1, 3, 0, 7, 6])

 

torch.arange(start, end, step = 1, out = None) -->Tensor

返回一个1维张量,长度为floor((end - start)/step).包含从start到end,以step为补偿的一组序列值,默认步长是1

start(float) - 序列的起始点

end(float) - 序列的最终值

steps(float) - 相邻点的间隔大小

out(Tensor,optional) - 结果张量

例子:

>>> torch.arange(4)
tensor([0, 1, 2, 3])
>>> torch.arange(1,4)
tensor([1, 2, 3])
>>> torch.arange(1, 2.5, 0.5)
tensor([1.0000, 1.5000, 2.0000])

 

torch.zeros(*sizes, out = None) --> Tensor

返回一个全为标量0的张量,形状由可变参数sizes定义

参数:

sizes(int...) - 整数序列,定义了输出形状

out(Tensor, optional) - 结果张量

例子:

>>> torch.zeros(2, 3)
tensor([[0., 0., 0.],
        [0., 0., 0.]])
>>> torch.zeros(5)
tensor([0., 0., 0., 0., 0.])

 

posted on 2019-06-04 14:05  孙大仙儿的编程之路  阅读(310)  评论(0编辑  收藏  举报