3.1(PyTorch入门)Tensors(张量)

准备把PyTorch官方教程学习一下。

下面是一个学习记录

#导入PyTorch库
from __future__ import print_function
import torch

1.构造一个5*3矩阵,不初始化。

x = torch.empty(5, 3)
print(x)



#-------------------------
"""tensor([[4.6939e+21, 3.0946e-41, 3.3631e-44],
        [0.0000e+00,        nan, 3.0946e-41],
        [1.1578e+27, 1.1362e+30, 7.1547e+22],
        [4.5828e+30, 1.2121e+04, 7.1846e+22],
        [9.2198e-39, 7.0374e+22, 9.9122e+20]])

"""

2.构造一个随机初始化的矩阵。

x = torch.rand(5, 3)
print(x)




#------------------------------------------------------------
"""
tensor([[0.7570, 0.3381, 0.2046],
        [0.9844, 0.5851, 0.5871],
        [0.6098, 0.6779, 0.9945],
        [0.6985, 0.0859, 0.9987],
        [0.9530, 0.8253, 0.8798]])
"""

3.构造一个矩阵全为 0,而且数据类型是 long.

x = torch.zeros(5, 3, dtype = torch.long)
print(x)




#---------------------------------------------------
"""tensor([[ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0]])
"""

4.构造一个使用数据的张量。

x = torch.tensor([5.5, 3])
print(x)




#---------------------------------------------------------------
"""tensor([ 5.5000,  3.0000])

"""

5.创建一个 tensor 基于已经存在的 tensor。

x = x.new_ones(5, 3, dtype = torch.double)
print(x)
x = x.torch.randn_like(x, dtype = torch.float)
print(x)





#----------------------------------------------------------
"""tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]], dtype=torch.float64)
tensor([[-0.2183,  0.4477, -0.4053],
        [ 1.7353, -0.0048,  1.2177],
        [-1.1111,  1.0878,  0.9722],
        [-0.7771, -0.2174,  0.0412],
        [-2.1750,  1.3609, -0.3322]])
"""

6.获取它的维度信息:

print(x.size())






#---------------------------------------------------------
"""torch.Size([5, 3])
"""

7.加法

#方式1
y = torch.rand(5, 3)
print(x)


#方式2
print(torch.add(x, y))



#方式3
result = torch.empty(5, 3)
torch.add(x, y, out = result)
print(result)


#方式4
y.add_(x)
print(y)



#----------------------------------------------------------------
"""tensor([[-0.1859,  1.3970,  0.5236],
        [ 2.3854,  0.0707,  2.1970],
        [-0.3587,  1.2359,  1.8951],
        [-0.1189, -0.1376,  0.4647],
        [-1.8968,  2.0164,  0.1092]])
"""

8.改变一个tensor的大小或者形状

x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)  
print(x.size(), y.size(), z.size())






#-------------------------------------------------------
"""torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
"""

9.获得value

x = torch.randn(1)
print(x)
print(x.item())








#----------------------------------------------------------
"""tensor([ 0.9422])
0.9422121644020081
"""

在学习过程中我发现一个点:

randn和rand的使用区别,大家时候也注意一下。

 

        randn是从标准正态分布中返回一个或多个样本值。正态分布,也即这些随机数的期望为0,方差为1

        rand则会产生[0, 1)之间的随机数。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值