张量简介与创建

更多见深度学习.pytorch

目录

习题练习

张量

1.定义

2.张量创建:直接创建

2.1张量创建:torch.tensor

2.2张量创建:from_numpy

3.张量创建:依据数值创建

3.1torch.zeros()

 3.2torch.zeros_like()

3.3torch.ones()

3.4torch.ones_like()

3.5torch.full()

3.6torch.full_like()

3.7torch.arange()

3.8torch.linespace()

 3.9torch.logspace()

3.10torch.eye()

4.张量创建:依据概率分布创建

4.1torch.normal()

4.2torch.randn()

4.3torch.randn_like()

4.4torch.rand()

4.5torch.rand_like()

4.6torch.randint()

4.7torch.randint_like()

4.8torch.randperm()

4.9torch.bernoulli()


习题练习

  1. 张量与矩阵、向量、标量的关系是怎么样的?
  2. Variable“赋予”张量什么功能?
    1. 自动求导
  3. 采用torch.from_numpy创建张量,并打印查看ndarray和张量数据的地址;
  4. 实现torch.normal()创建张量的四种模式。

张量

1.定义

张量是多维数组,它是标量、向量、矩阵的高维拓展。

 

2.张量创建:直接创建

2.1张量创建:torch.tensor

torch.tensor(

data,

dtype=None,

device=None,

requires_grad=False,

pin_memory=False):

从data创建tensor。

data:数据,可以是list,numpy.

dtype:数据类型默认与data一致。

device:所在设备,cuda/cpu

requires_grad:是否需要梯度

pin_memory:是否存在锁页内存。

1.

import numpy as np
import torch

#通过torch.tensor创建
arr = np.ones((3,3))
print("arr的数据类型:{}".format(arr.dtype))
t = torch.tensor(arr)
print(t)

输出:

arr的数据类型:float64
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)

张量t的数据类型与arr的数据类型一致,都是float64。

2.

import numpy as np
import torch

#通过torch.tensor创建
arr = np.ones((3,3))
print("arr的数据类型:{}".format(arr.dtype))
t = torch.tensor(arr, device="cuda")
print(t)

输出:

arr的数据类型:float64
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], device='cuda:0', dtype=torch.float64)

2.2张量创建:from_numpy

功能:从numpy创建tensor。

从torch。from_numpy创建的tensor与原来的ndarray共享内存,当修改其中一个数据,另一个也将会被改变。

# ===============================  example 2 ===============================
# 通过torch.from_numpy创建张量
arr = np.array([[1, 2, 3], [4, 5, 6]])
t = torch.from_numpy(arr)
print("numpy array: ", arr)
print("tensor : ", t)

print("\n修改arr")
arr[0, 0] = 0
print("numpy array: ", arr)
print("tensor : ", t)

print("\n修改tensor")
t[0, 0] = -1
print("numpy array: ", arr)
print("tensor : ", t)

输出:

numpy array:  [[1 2 3]
 [4 5 6]]
tensor :  tensor([[1, 2, 3],
        [4, 5, 6]], dtype=torch.int32)

修改arr
numpy array:  [[0 2 3]
 [4 5 6]]
tensor :  tensor([[0, 2, 3],
        [4, 5, 6]], dtype=torch.int32)

修改tensor
numpy array:  [[-1  2  3]
 [ 4  5  6]]
tensor :  tensor([[-1,  2,  3],
        [ 4,  5,  6]], dtype=torch.int32)

3.张量创建:依据数值创建

3.1torch.zeros()

功能:依size创建全0张量

size:张量的形状,如(3,3),(3,224,224)

out:输出的张量

layout:内存中布局形式,有strided,sparse_coo等,默认strided,稀疏矩阵使用sparse_coo

device:所在设备,cuda/cup

requires_grad:是否需要梯度。

# 通过torch.zeros创建张量
out_t = torch.tensor([1])

t = torch.zeros((3, 3), out=out_t)

print(t, '\n', out_t)
print(id(t), id(out_t), id(t) == id(out_t))

输出:

tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]) 
 tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])
2100226699000 2100226699000 True

 3.2torch.zeros_like()

功能:从input形状创建全0张量;input:创建与input同形状的全0张量;dtype:数据类型;layout:内存中布局形式。

3.3torch.ones()

3.4torch.ones_like()

3.5torch.full()

3.6torch.full_like()

依据input形状创建指定数据的张量:size:张量的形状,如(3,3);fill_value:张量的值。

# ===============================  example 4 ===============================
# 通过torch.full创建全1张量
t = torch.full((3, 3), 1)
print("t:", t)
a = torch.full((3, 3), 3)
print("a:", a)

输出:

t: tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
a: tensor([[3., 3., 3.],
        [3., 3., 3.],
        [3., 3., 3.]])

3.7torch.arange()

功能:创建等差的1维张量;注意事项:数值区间[start,end);

start:数列起始值;end:数列“结束值”;step:数列公差,默认为1。

# ===============================  example 5 ===============================
# 通过torch.arange创建等差数列张量
t = torch.arange(2, 10, 2)
print(t)

输出:

tensor([2, 4, 6, 8])

3.8torch.linespace()

功能:创建均分的1维张量。

注意事项:数值区间[start,end].

start:数列起始值;end:数列结束值;steps:数列长度。

# ===============================  example 6 ===============================
# 通过torch.linspace创建均分数列张量
t = torch.linspace(2, 10, 5)
print("t:", t)
a = torch.linspace(2, 10, 6)
print("a:", a)

输出:

t: tensor([ 2.,  4.,  6.,  8., 10.])
a: tensor([ 2.0000,  3.6000,  5.2000,  6.8000,  8.4000, 10.0000])

 3.9torch.logspace()

功能:创建对数均分的1维张量。

注意事项:长度steps,底为base。

3.10torch.eye()

功能:创建单位对角矩阵(2维张量)

注意事项:默认为方阵。

n:方阵行数;m:矩阵列数。

4.张量创建:依据概率分布创建

4.1torch.normal()

功能:生成正态分布,也就是高斯分布。mean:均值;std:方差。

可以分为四种模式:mean为标量,std为标量;mean为标量,std为张量;mean为张量,std为标量;mean为张量,std为张量。

# mean:张量 std: 张量
mean = torch.arange(1, 5, dtype=torch.float)
std = torch.arange(1, 5, dtype=torch.float)
t_normal = torch.normal(mean, std)
print("mean:{}\nstd:{}".format(mean, std))
print(t_normal)

输出:

mean:tensor([1., 2., 3., 4.])
std:tensor([1., 2., 3., 4.])
tensor([3.2227, 5.1389, 2.7871, 2.5042])

 3.2227, 5.1389, 2.7871, 2.5042来自四个不同高斯分布,3.2227均值为1,方差为1;2.7871均值为2,方差为2。

# mean:标量 std: 标量
t_normal = torch.normal(0., 1., size=(4,))
print(t_normal)

mean为标量,std为标量时,需要指定size

输出:

tensor([ 0.3160, -0.7045,  1.1657,  2.6423])

输出的tensor来自同一个分布,均值为0,方差为1。

# mean:张量 std: 标量
mean = torch.arange(1, 5, dtype=torch.float)
std = 1
t_normal = torch.normal(mean, std)
print("mean:{}\nstd:{}".format(mean, std))
print(t_normal)

输出:

mean:tensor([1., 2., 3., 4.])
std:1
tensor([-0.2408,  3.0934,  4.7962,  5.1816])

tensor大小与mean_tensor相同,tensor每个元素均值与mean_tensor一一对应。 

4.2torch.randn()

4.3torch.randn_like()

生成标准正态分布。

4.4torch.rand()

4.5torch.rand_like()

在[0,1)上,生成均匀分布

4.6torch.randint()

4.7torch.randint_like()

在区间[low,high)生成整数均匀分布。

4.8torch.randperm()

生成从0到n-1的随机排列,n为张量长度。

4.9torch.bernoulli()

以input为概率,生成伯努利分布(0-1分布,两点分布)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haimianjie2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值