ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

pytorch-tensor张量

2021-07-19 23:01:35  阅读:196  来源: 互联网

标签:tensor numpy torch 张量 pytorch print type


torch

目录

张量

张量(Tensor)是线性代数中的一种数据结构,是向量和矩阵的推广,我们可以在张量上进行算术运算。Tensors 类似于 NumPy 的 ndarrays ,同时 Tensors 可以使用 GPU 进行计算

判断是否是张量

import torch

var_1 = 1
print(torch.is_tensor(var_1)) 
# False

tensor的几种属性

属性 描述
tensor.data 存储的数据
tensor.type() 有括号,其他的没有
tensor.shape
tensor.device
tensor.requires_grad
tenor.grad

内置类型

type CPU GPU
torch.float32 torch.FloatTensor torch.cuda.FloatTensor
torch.float64 torch.DoubleTensor torch.cuda.DoubleTensor
torch.float16 torch.HalfTensor torch.HalfTensor
torch.uint8 torch.ByteTensor torch.cuda.ByteTensor
torch.int8 torch.CharTensor torch.cuda.CharTensor
torch.int16 torch.ShortTensor torch.cuda.ShortTensor
torch.int32 torch.IntTensor torch.cuda.IntTensor
torch.int64 torch.LongTensor torch.cuda.LongTensor
torch.bool torch.BoolTensor torch.cuda.BoolTensor

torch.type()

t1 = torch.LongTensor(3, 5)
print(t1.type())
# 转换为其他类型
t2=t1.type(torch.FloatTensor)
print(t2.type())
 
 
torch.LongTensor
torch.FloatTensor

type_as()

t1=torch.Tensor(2,3)
t2=torch.IntTensor(3,5)
t3=t1.type_as(t2)
print(t3.type())
 
torch.IntTensor

设置默认类型

torch.set_default_tensor_type(torch.FloatTensor)

torch.get_default_dtype()
#  torch.float32

类型转换

import torch
 
tensor = torch.randn(2, 2)
print(tensor.type())
 
# torch.long() 将tensor转换为long类型
long_tensor = tensor.long()
print(long_tensor.type())
 
# torch.half()将tensor转换为半精度浮点类型
half_tensor = tensor.half()
print(half_tensor.type())
 
# torch.int()将该tensor转换为int类型
int_tensor = tensor.int()
print(int_tensor.type())
 
# torch.double()将该tensor转换为double类型
double_tensor = tensor.double()
print(double_tensor.type())
 
# torch.float()将该tensor转换为float类型
float_tensor = tensor.float()
print(float_tensor.type())
 
# torch.char()将该tensor转换为char类型
char_tensor = tensor.char()
print(char_tensor.type())
 
# torch.byte()将该tensor转换为byte类型
byte_tensor = tensor.byte()
print(byte_tensor.type())
 
# torch.short()将该tensor转换为short类型
short_tensor = tensor.short()
print(short_tensor.type())
 
    
torch.FloatTensor
torch.LongTensor
torch.HalfTensor
torch.IntTensor
torch.DoubleTensor
torch.FloatTensor
torch.CharTensor
torch.ByteTensor
torch.ShortTensor

创建tensor

创建Tensor时指明数据类型

import torch

double_points = torch.ones((10, 2), dtype=torch.double)
short_points = torch.tensor([[1,2],[3,4]], dtype=torch.short)

获取数据类型

tensor.type()

tenor=torch.tensor([1,2,3]).type()
torch.set_default_tensor_type(torch.FloatTensor)
tenor2=torch.tensor([1.2, 3]).type() 
print(tenor)
print(tenor2)

torch.LongTensor
torch.FloatTensor

转换数据类型

3种方式

import torch
# (1)直接在tensor后面接.dtype()进行转换

double_points = torch.zeros(10,2).double()

# (2)使用to进行转换
double_points = torch.zeros(10,2).to(torch.double)

# (3)使用type()进行转换
double_points = torch.zeros(10,2).type(torch.short)

torch.tensor(data)

data 可以是list, numpy

torch.tensor( data, dtype=None, device=None, requires_grad=False, pin_memory=False)
"""
data就是张量中存储的数据,可以是list,numpy;
dtype数据类型默认和data一致;
device所在设备(cuda or cpu);
requires_grad是否需要计算梯度
"""

import torch
import numpy as np
 
# 创建numpy数据
arry = np.ones((3,3))
print("array的数据类型:",arry.dtype)
 
# 创建tensor
t = torch.tensor(arry)
print(t)
# --------------------------------------------------------------
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)

torch.from_numpy(ndarray)

tensorndarray是共享内存的,当修改一个数据时另一个数据也会随之改变

import torch
import numpy as np
 
# 创建numpy数据
arry = np.array([[1,2,3],[4,5,6]])
print("numpy:",arry)
 
# 创建tensor
t = torch.from_numpy(arry)
print("tensor:",t)
 
# 测试共享内存
 
arry[0][0] = 10
print("numpy:",arry)
print("tensor:",t)
# --------------------------------------------------------
numpy: [[1 2 3]
 [4 5 6]]       
tensor: tensor([[1, 2, 3],
        [4, 5, 6]], dtype=torch.int32)
numpy: [[10  2  3]
 [ 4  5  6]]
tensor: tensor([[10,  2,  3],
        [ 4,  5,  6]], dtype=torch.int32)

torch.as_tensor(data)

torch.as_tensor()torch.from_numpy() 原生的numpy数组共享内存

import torch

a = np.array([1, 2, 3])
t_1 = torch.as_tensor(a)
print(t1)
a[[0,1,2]] = 0
print(t1)
# ----------------------------------------------------
tensor([1, 2, 3], dtype=torch.int32)
array([0, 0, 0])

tensor.tolist()

tenor_rand = torch.randn(2,3)
print(tenor_rand)
print(tenor_rand.tolist())
print(type(tenor_rand.tolist()))

# tensor([[-0.3383,  0.4823,  0.3448],
#         [ 0.2380,  0.1300, -0.1907]])
# [[-0.3383448123931885, 0.482287734746933, 0.34484198689460754], 
#  [0.23801031708717346, 0.12998750805854797, -0.19072400033473969]]
# <class 'list'>

tensor.numpy()


tenor_rand = torch.randn(2,3)
print(tenor_rand.numpy())
print(type(tenor_rand.numpy()))

# [[ 1.6192089  -0.7044066   0.8276266 ]
#  [-0.10407448  0.11350407 -0.9923446 ]]
# <class 'numpy.ndarray'>

创建指定张量

函数 描述
torch.zeros(size) 创建全为0的张量
torch.zeros_like(input) 创建和input一样大小的全0张量
torch.ones(size) 创建全1张量
torch.ones_like(input) 创建和input一样大小的全1张量
torch.full(size, fill_value) 创建全”full_value“张量
torch.full_like(input,fill_value)
torch.eye(n,m) 创建单位对角矩阵(二维张量),默认为方阵 n:矩阵行数 m: 矩阵列数。
torch.arange (start=0, end, step=1) 创建等差的一维张量,数值区间为[ start, end )
torch.linspace(start, end, steps) 创建均分的一维张量,数值区间为[start, end] steps:生成的样本数
torch.logspace(start, end, steps)
使用方式基本和numpy类似

随机操作

torch.manual_seed() 随机种子
torch.rand(size) 区间[0, 1)的均匀分布 float
torch.randn(size) 均值为0,方差为1,标准正态分布 float
torch.normal(means, std, out=None) 指定均值means和标准差std的离散正态分布
torch.randint(low,high,size) 在区间[low,high)上,生成整数均匀分布
torch.randperm(n) 生成从0n-1的随机排列
torch.bernoulli(input) input为概率,生成伯努利分布(0-1分布,两点分布)
import torch
rand = torch.rand(2, 3)  # 返回一个张量,包含了从区间[0,1)的均匀分布中抽取的一组随机数
print(rand)  # 随机初始化的值在 [0,1) 之间
# tensor([[0.5047, 0.8442, 0.1771],
#         [0.3896, 0.5468, 0.9686]])
# torch.rand_like()

#---------------------------------------------------------------------------
randn = torch.randn(2, 3)  
# 返回一个张量,包含了从标准正态分布(均值为0,方差为 1,即高斯白噪声)中抽取一组随机数
print(randn)
# tensor([[ 0.0539,  0.8289, -1.6746],
#         [-1.7428,  0.6285,  0.1432]])
# torch.randn_like()

	
#--------------------------------------------------------------------------
randint_01 = torch.randint(3, 5, (3,))
print(randint_01)
# tensor([3, 4, 3])

标签:tensor,numpy,torch,张量,pytorch,print,type
来源: https://www.cnblogs.com/tian777/p/15032698.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有