Pytorch 创建Tensor

Pytorch 创建Tensor

tensor创建
import numpy as np
import torch
a = np.array([2, 3.3])

b = torch.from_numpy(a)

print("b: \t", b)

c = np.ones([2,3])

d = torch.from_numpy(c)

print("d: \t", d)
b: 	 tensor([2.0000, 3.3000], dtype=torch.float64)
d: 	 tensor([[1., 1., 1.], [1., 1., 1.]], dtype=torch.float64)
import numpy as np
import torch
a = torch.tensor([2., 3.2])     # tensor 接收的现成的数据 要么numpy 要么list
print("a: \t",a)
# torch.FloatTensor(d1, d2, d3)
b = torch.FloatTensor([2., 3.2])   # Tensor 接收数据的维度(shape)例: (2,3)  也可以接收现有的数据 用list表示
print("b: \t",b)
c = torch.tensor([[2., 3.2], [1., 22.3]])
print("c: \t",c)
a: 	 tensor([2.0000, 3.2000])
b: 	 tensor([2.0000, 3.2000])
c: 	 tensor([[ 2.0000,  3.2000], [ 1.0000, 22.3000]])
未初始化数据(unintialized):
Torch。empty()                   
Torch.FloatTensor(d1, d2, d3)     # shape Not toch.FloatTensor([1,2]) = torch.tensor([1,2])
Torch.IntTensr(d1, d2, d3)
import torch
a = torch.empty(1)
print("a:\t", a)

b = torch.Tensor(2,3)
print("b:\t", b)

c = torch.FloatTensor(2,3)
print("c:\t", c)
a:	 tensor([-1.7936e-18])
b:	 tensor([[0., 0., 0.], [0., 0., 0.]])
c:	 tensor([[0.0000e+00, 0.0000e+00, 8.4078e-45], [0.0000e+00, 1.4013e-45, 0.0000e+00]])
Set default type:
import torch
a = torch.tensor([1.2, 3]).type()
print("a:\t", a)

torch.set_default_tensor_type(torch.DoubleTensor) # 设定tensor的类型为:DoubleTensor
b = torch.tensor([1.2, 3]).type()
print("b: \t", b)
a:	 torch.FloatTensor
b: 	 torch.DoubleTensor
rand/rand_like, randint:
import torch
# rand() 0~1之间
a = torch.rand(3,3)
print('a:\t', a)

b = torch.rand_like(a)  # 接收的是一个torch
print("b:\t", b)
# 均匀采样0~10的Tensor randint()只能采样整数
c = torch.randint(1, 10, [3,3]) 
print("c \t", c)
a:	 tensor([[0.5405, 0.0994, 0.7603],
        [0.9873, 0.6534, 0.0490],
        [0.7819, 0.9018, 0.1276]])
b:	 tensor([[0.4911, 0.0674, 0.2491],
        [0.7798, 0.0695, 0.4160],
        [0.9018, 0.7866, 0.1199]])
c 	 tensor([[7, 8, 4], [5, 5, 1], [9, 8, 4]])
randn 正态分布 N(0, 1)
import torch
a = torch.randn(3,3)
print("a:\t", a)
# 自定义均值 和 方差
b = torch.normal(mean=torch.full([10], 0), std=torch.arange(1, 0, -0.1))
print("b:\t", b)
a:	 tensor([[-0.6150, -0.5848, -0.1453],
        [-0.3552, -0.0263,  0.0746],
        [ 1.3996,  0.2154, -0.7002]])
b:	 tensor([-2.3748, -0.3933, -0.3524,  0.5886, -0.5104,  0.1725, -0.4686,  0.1332,
        -0.1855,  0.0342])
full 全部赋值为同一个元素:
import torch
# [2,3]相当于一个 shape , 7:表示赋值
a = torch.full([2,3], 7)
print('a:\t', a)

b = torch.full([], 7)   # dim =0  
print("b:\t", b)

c = torch.full([1], 7)  # dim =1
print("c:\t", c)
a:	 tensor([[7., 7., 7.],
        [7., 7., 7.]])
b:	 tensor(7.)
c:	 tensor([7.])
arange / range:
import torch
a = torch.arange(0, 10)
print('a:\t', a)

b = torch.arange(0, 10, 2)
print("b:\t", b)
a:	 tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b:	 tensor([0, 2, 4, 6, 8])
linspace / logspace:
import torch
# linspace(): 0-10都包含进来了 step数量值 例:steps=4 等分切出4个值
a = torch.linspace(0, 10, steps=4)
print("a:\t", a)
b = torch.linspace(0, 10, steps=10)
print("b:\t", b)
c = torch.linspace(0, 10, steps=11)
print("c:\t", c )
# logspace(): logspace的base参数可以设置为2, 10, e等底数
d = torch.logspace(0, -1, steps=10) #以为底数 10的0次方~10的-1次方
print("d:\t", d)
a:	 tensor([ 0.0000,  3.3333,  6.6667, 10.0000])
b:	 tensor([ 0.0000,  1.1111,  2.2222,  3.3333,  4.4444,  5.5556,  6.6667,  7.77788.8889, 10.0000])
c:	 tensor([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
d:	 tensor([1.0000, 0.7743, 0.5995, 0.4642, 0.3594, 0.2783, 0.2154, 0.1668, 0.1292, 0.1000])
Ones / zeros/ eye:
import torch
# 全部是one的矩阵
a = torch.ones(3, 3)
print("a:\t", a)
# 全部是zero矩阵
b = torch.zeros(3, 3)
print("b:\t", b)
# eye: 单位矩阵
c = torch.eye(3, 4)
print("c:\t", c)

d = torch.eye(3)
print("d:\t", d)

e = torch.ones_like(a)
print("e:\t", e)
a:	 tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
b:	 tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
c:	 tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.],
        [0., 0., 1., 0.]])
d:	 tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])
e:	 tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
randperm
a = torch.randperm(10) # 0~9 的1随机索引
print("a:\t", a)
a:	 tensor([9, 1, 4, 5, 2, 8, 6, 0, 3, 7])
import torch
a = torch.rand(2, 3)
b = torch.rand(2 , 2)
print("a, b : \t", a, b)
idx = torch.randperm(2)
print("idx:\t", idx)

print("a[idx]:\t", a[idx])   # 相当于索引一行 
print("b[idx]:\t", b[idx])	 # 相当于索引一行
a, b : 	 tensor([[0.1115, 0.4137, 0.6847],
        [0.0791, 0.4034, 0.0941]]) tensor([[0.5270, 0.5309],
        [0.2291, 0.2432]])
idx:	 tensor([1, 0])
a[idx]:	 tensor([[0.0791, 0.4034, 0.0941],
        [0.1115, 0.4137, 0.6847]])
b[idx]:	 tensor([[0.2291, 0.2432],
        [0.5270, 0.5309]])
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值