#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/3/12 14:03
# @Author  : zhoujianwen
# @Email   : zhou_jianwen@qq.com
# @File    : CreateTensor.py
# @Describe: 创建Tensor
import numpy as np
import torch

a = np.array([2,3.3])
# 从numpy导入的float其实是double类型
print("torch from numpy:",torch.from_numpy(a))
torch from numpy: tensor([2.0000, 3.3000], dtype=torch.float64)
# 创建一个二行三列的S元素全是的数组
b = np.ones([2,3])
print("torch from numpy",torch.from_numpy(b))
torch from numpy tensor([[1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
# 小写的tensor只能接受现成的数据
c = torch.tensor([2.,3.2]) 
print("c:",c)
print("c:",c.type())
c: tensor([2.0000, 3.2000])
c: torch.FloatTensor
# 大写的tensor接受数据的维度,也可以接受现成的数据
d = torch.Tensor(2,3)  
print("d:",d)
print("d:",d.type())
d: tensor([[0.0000e+00, 4.1530e-05, 1.8754e+28],
        [6.4978e-07, 4.1727e-08, 1.0129e-11]])
d: torch.FloatTensor
# 四维的tensor比较适合处理图片数据类型,cnn:[b,c,h,w]
# 创建1片数据,每片数据2个通道,3行4列
e = torch.FloatTensor(2,2,3,4)  
print("e:",e)
print("e:",e.type())
e: tensor([[[[ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00],
          [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00],
          [ 1.0000e+00,  0.0000e+00, -6.3164e-34,  8.7441e-43]],

         [[ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00],
          [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00],
          [ 9.8091e-45,  0.0000e+00,  1.1210e-44,  0.0000e+00]]],


        [[[ 1.0000e+00,  0.0000e+00, -6.3167e-34,  8.7441e-43],
          [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00],
          [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00]],

         [[ 9.8091e-45,  0.0000e+00,  1.1210e-44,  0.0000e+00],
          [ 0.0000e+00,  0.0000e+00,  0.0000e+00,  0.0000e+00],
          [ 0.0000e+00,  0.0000e+00,  1.4013e-45,  0.0000e+00]]]])
e: torch.FloatTensor
# 大写的tensor接受数据的维度,也可以接受现成的数据
f = torch.FloatTensor([2.,3.2])  
print("f:",f)
print("f:",f.type())
# 不推荐 torch.FloatTensor([2.,3.2]) = torch.tensor([2.,3.2])
f: tensor([2.0000, 3.2000])
f: torch.FloatTensor
# 创建2*3的空数组,空数据中的值并不为0,而是未初始化的垃圾值,这些值非常随机。
g = torch.empty((2,3))  
print("g:",g)
print("g:",g.type())
g: tensor([[0., 0., 0.],
        [0., 0., 0.]])
g: torch.FloatTensor

# 未初始化的tensor一定要跟写入数据的后续步骤
# 增强学习一般使用double,其他一般使用float
torch.set_default_tensor_type(torch.DoubleTensor)
print("set_default_tensor_type:",torch.tensor([1.2,3]).type())
print(torch.tensor(1.))
print("type",torch.tensor(1.).type())
set_default_tensor_type: torch.DoubleTensor
tensor(1.)
type torch.DoubleTensor
# 均匀采样0-10的TENSOR,要用x = 10*torch.rand(d1,d2),randint只能采样整数额
# rand/rand_like,randint
h = 10 * torch.rand(3,3)  # 正态分布,N(0,1),mean为0,std为1
print("torch.rand:",h)
print("torch.rand_like:",torch.rand_like(h))
print("torch.randint:",torch.randint(1,10,[3,3]))
torch.rand: tensor([[4.5408, 1.3240, 9.3553],
        [9.6625, 2.3309, 2.1998],
        [6.9066, 4.8293, 3.6077]])
torch.rand_like: tensor([[0.1560, 0.0928, 0.0979],
        [0.9117, 0.4084, 0.8880],
        [0.5676, 0.9840, 0.7720]])
torch.randint: tensor([[4, 8, 3],
        [3, 3, 5],
        [8, 3, 9]])
# 自定义均值和方差
j = torch.normal(mean=torch.full([10],0),std=torch.arange(1,0,-0.1))
print("normal:",j)
print("生成一个值全为10的tensor:",torch.full([10],0))
print("生成一个等差数列:",torch.arange(1,0,-0.1))  # arange不包含右边界,左闭右开[1,0)
print("生成一个标量:",torch.full([], 2),",dim:",torch.full([], 2).dim(),",size:",torch.full([], 2).size())
print("生成一个向量:",torch.full([1], 2),",dim:",torch.full([1], 2).dim(),",size:",torch.full([1], 2).size())
normal: tensor([ 0.1456,  0.9674, -1.1302,  0.7635, -1.1897, -0.4885, -0.2525,  0.1764,
        -0.1095, -0.2154])
生成一个值全为10的tensor: tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
生成一个等差数列: tensor([1.0000, 0.9000, 0.8000, 0.7000, 0.6000, 0.5000, 0.4000, 0.3000, 0.2000,
        0.1000])
生成一个标量: tensor(2.) ,dim: 0 ,size: torch.Size([])
生成一个向量: tensor([2.]) ,dim: 1 ,size: torch.Size([1])
# 等差数列
print(torch.arange(0,10))
print(torch.arange(0,10,2))
print(torch.range(0,10))  # 可以使用arange代替
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
tensor([0, 2, 4, 6, 8])
tensor([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])


D:\ProgramData\Anaconda3\envs\LearningProjects\lib\site-packages\ipykernel_launcher.py:4: UserWarning: torch.range is deprecated in favor of torch.arange and will be removed in 0.5. Note that arange generates values in [start; end), not [start; end].
  after removing the cwd from sys.path.
# linspace/logspace
print(torch.linspace(0,10,steps=4))  # [0,10]
print(torch.linspace(0,10,steps=10))
print(torch.linspace(0,10,steps=11))
tensor([ 0.0000,  3.3333,  6.6667, 10.0000])
tensor([ 0.0000,  1.1111,  2.2222,  3.3333,  4.4444,  5.5556,  6.6667,  7.7778,
         8.8889, 10.0000])
tensor([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
# logspace的base参数可以设置为2,10,e等底数
print(torch.logspace(0,-1,steps=10))  # 10^0,10^x,......10^-1
tensor([1.0000, 0.7743, 0.5995, 0.4642, 0.3594, 0.2783, 0.2154, 0.1668, 0.1292,
        0.1000])
# ones/zeros/eye
print(torch.ones(3,3))  # 3x3矩阵元素全是1
print(torch.zeros(3,3))  # 3x3矩阵元素全是0
print(torch.eye(4,4))  # 4x4对角矩阵元素是1
print(torch.eye(4))
l = torch.zeros(3,3)
print("l:",torch.ones_like(l))
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.],
        [0., 0., 1., 0.],
        [0., 0., 0., 1.]])
tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.],
        [0., 0., 1., 0.],
        [0., 0., 0., 1.]])
l: tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
# randperm , random.shuffle
print("randperm:",torch.randperm(10)) # [0,9)
n = torch.rand(2,3)
m = torch.rand(2,2)
idx = torch.randperm(2)
idx
print("idx:",idx)
idx
print("idx:",idx)

print("n[idx]:",n[idx])
print("m[idx]:",m[idx])
randperm: tensor([9, 0, 2, 7, 4, 1, 8, 5, 6, 3])
idx: tensor([0, 1])
idx: tensor([0, 1])
n[idx]: tensor([[0.8147, 0.7831, 0.0456],
        [0.5734, 0.4782, 0.6875]])
m[idx]: tensor([[0.4919, 0.5207],
        [0.0426, 0.8168]])
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐