pytorch之Tensor

#tensor和numpy

import torch

import numpy as np

numpy_tensor = np.random.randn(3,4)

print(numpy_tensor)

#将numpy的ndarray转换到tendor上

pytorch_tensor1 = torch.Tensor(numpy_tensor)

pytorch_tensor2 = torch.from_numpy(numpy_tensor)

print(pytorch_tensor1)

print(pytorch_tensor2)

#将pytorch的tensor转换到numpy的ndarray

numpy_array = pytorch_tensor1.numpy()   #如果pytorch在cpu上

print(numpy_array)

#tensor的一些属性,得到tensor的大小

print(pytorch_tensor1.shape)

print(pytorch_tensor1.size())

print(pytorch_tensor1.type()) #得到tensor的数据类型

print(pytorch_tensor1.dim()) #得到tensor的维度

print(pytorch_tensor1.numel()) #得到tensor所有元素的个数

x = torch.rand(3,2)

x.type(torch.DoubleTensor)

print(x)

np_array = x.numpy()

print(np_array.dtype)

[[ 1.05174423  1.09272735  0.46027768 -0.03255727]
 [ 0.57027229  1.22165706 -0.77909099 -0.17678552]
 [ 0.02112402 -1.08971068  0.72317744 -1.45482622]]
tensor([[ 1.0517,  1.0927,  0.4603, -0.0326],
        [ 0.5703,  1.2217, -0.7791, -0.1768],
        [ 0.0211, -1.0897,  0.7232, -1.4548]])
tensor([[ 1.0517,  1.0927,  0.4603, -0.0326],
        [ 0.5703,  1.2217, -0.7791, -0.1768],
        [ 0.0211, -1.0897,  0.7232, -1.4548]], dtype=torch.float64)
[[ 1.0517442   1.0927273   0.46027768 -0.03255726]
 [ 0.57027227  1.221657   -0.779091   -0.17678553]
 [ 0.02112402 -1.0897107   0.72317743 -1.4548262 ]]
torch.Size([3, 4])
torch.Size([3, 4])
torch.FloatTensor
2
12
tensor([[0.1810, 0.5168],
        [0.9859, 0.1294],
        [0.9262, 0.6952]])
float32

#Tensor的操作1

import torch

x = torch.ones(2,3)

print(x)

print(x.type())

x = x.long()

print(x.type())

x = x.float()

print(x.type())

y = torch.rand(3,4)

print(y)

#沿着行取最大值

maxval,maxindex = torch.max(y,dim=1)

print(maxval,‘\n‘,maxindex)

#沿着行对y求和

sum = torch.sum(y,dim=1)

print(sum)

tensor([[1., 1., 1.],
        [1., 1., 1.]])
torch.FloatTensor
torch.LongTensor
torch.FloatTensor
tensor([[0.8910, 0.0130, 0.9600, 0.6760],
        [0.5184, 0.6240, 0.9589, 0.2151],
        [0.6904, 0.3474, 0.7502, 0.2055]])
tensor([0.9600, 0.9589, 0.7502])
 tensor([2, 2, 2])
tensor([2.5400, 2.3164, 1.9936])

#Tensor操作2

import torch

x = torch.rand(3,2)

print(x)

print(x.size())

#增加一个维度

x = x.unsqueeze(0)

print(x.size())

#减少一个维度

x = x.squeeze(0)

print(x.size())

#增加回来

x = x.unsqueeze(1)

print(x.size())

#使用permute和transpose来对矩阵维度进行变换

#permute 可以重新排列tensor的维度

#transpose 可以交换两个维度

x = x.permute(1,0,2)

print(x.size())

x = x.transpose(0,2)

print(x.size())

tensor([[0.9131, 0.2160],
        [0.0987, 0.5013],
        [0.1715, 0.8862]])
torch.Size([3, 2])
torch.Size([1, 3, 2])
torch.Size([3, 2])
torch.Size([3, 1, 2])
torch.Size([1, 3, 2])
torch.Size([2, 3, 1])

#使用view对tensor进行reshape

import torch

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

print(x.shape)

x = x.view(-1,5)

print(x.size())

x = x.view(60)

print(x.shape)

#两个Tensor求和

a = torch.rand(3,4)

b = torch.rand(3,4)

c = a + b

print(c)

z = torch.add(a,b)

print(z)

torch.Size([3, 4, 5])
torch.Size([12, 5])
torch.Size([60])
tensor([[0.8822, 1.3766, 1.3586, 0.8951],
        [1.0096, 0.5511, 0.2035, 0.9684],
        [1.2502, 0.0963, 1.3955, 0.9479]])
tensor([[0.8822, 1.3766, 1.3586, 0.8951],
        [1.0096, 0.5511, 0.2035, 0.9684],
        [1.2502, 0.0963, 1.3955, 0.9479]])

import torch

x = torch.ones(4,4)

print(x)

x[1:3,1:3] = 2

print(x)

tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])
tensor([[1., 1., 1., 1.],
        [1., 2., 2., 1.],
        [1., 2., 2., 1.],
        [1., 1., 1., 1.]])

原文地址:https://www.cnblogs.com/ryluo/p/10170687.html

时间: 2024-08-30 17:17:50

pytorch之Tensor的相关文章

[Pytorch]Pytorch中tensor常用语法

原文地址:https://zhuanlan.zhihu.com/p/31494491 上次我总结了在PyTorch中建立随机数Tensor的多种方法的区别. 这次我把常用的Tensor的数学运算总结到这里,以防自己在使用PyTorch做实验时,忘记这些方法应该传什么参数. 总结的方法包括: Tensor求和以及按索引求和:torch.sum() torch.Tensor.indexadd() Tensor元素乘积:torch.prod(input) 对Tensor求均值.方差.极值: torch

PyTorch中Tensor的维度变换实现

对于 PyTorch 的基本数据对象 Tensor (张量),在处理问题时,需要经常改变数据的维度,以便于后期的计算和进一步处理,本文旨在列举一些维度变换的方法并举例,方便大家查看. 维度查看:torch.Tensor.size() 查看当前 tensor 的维度 举个例子: >>> import torch >>> a = torch.Tensor([[[1, 2], [3, 4], [5, 6]]]) >>> a.size() torch.Size

pytorch中tensor张量的创建

import torch import numpy as np print(torch.tensor([1,2,3])) print(torch.tensor(np.arange(15).reshape(3,5))) print(torch.empty([3,4])) print(torch.ones([3,4])) print(torch.zeros([3,4])) #0-1之间的随机数 print(torch.rand([2,3])) #3-10之间的随机整数 print(torch.ran

Pytorch个人心得(一)-----Tensor基本使用

最近在学习Pytorch,在这里分享一些心得,其实如果也是入门的朋友可以直接参考我的这一个系列,因为比较接地气.Pytorch的安装我们这里忽略,毕竟也不是什么难事.这一讲我们大概说一下Pytorch的Tensor,就像numpy的ndarray(如果你有接触过numpy),Tensor是Pytorch里面的主要操作的数据形式,中文叫做张量,就是维度大于2的量.比如矩阵是二维,向量是一维,而张量就是三维以上了.下面话不多说直接就着代码和注释来理解吧同学们: import torch import

pytorch 学习笔记之编写 C 扩展,又涨姿势了

pytorch利用CFFI 进行 C 语言扩展.包括两个基本的步骤(docs): 编写 C 代码: python 调用 C 代码,实现相应的 Function 或 Module. 在之前的文章中,我们已经了解了如何自定义 Module.至于 [py]torch 的 C 代码库的结构,我们留待之后讨论: 这里,重点关注,如何在 pytorch C 代码库高层接口的基础上,编写 C 代码,以及如何调用自己编写的 C 代码. 官方示例了如何定义一个加法运算(见 repo).这里我们定义ReLU函数(见

PyTorch官方中文文档:torch

torch 包 torch 包含了多维张量的数据结构以及基于其上的多种数学操作.另外,它也提供了多种工具,其中一些可以更有效地对张量和任意类型进行序列化. 它有CUDA 的对应实现,可以在NVIDIA GPU上进行张量运算(计算能力>=2.0). http://www.aibbt.com/a/pytorch/ 张量 Tensors torch.is_tensor[source] torch.is_tensor(obj) 如果obj 是一个pytorch张量,则返回True 参数: obj (Ob

生产与学术之Pytorch模型导出为安卓Apk尝试记录

生产与学术 写于 2019-01-08 的旧文, 当时是针对一个比赛的探索. 觉得可能对其他人有用, 就放出来分享一下 生产与学术, 真实的对立... 这是我这两天对pytorch深度学习->android实际使用的这个流程的一个切身感受. 说句实在的, 对于模型转换的探索, 算是我这两天最大的收获了... 全部浓缩在了这里: https://github.com/lartpang/DHSNet-PyTorch/blob/master/converter.ipynb 鉴于github加载ipyn

Pytorch学习之梯度计算backward函数

Pytorch在梯度方面提供的功能,大多是为神经网络而设计的.而官方文档给出的定义和解释比较抽象.以下将结合实例,总结一下自己对Pytorch中梯度计算backward函数的理解. 1. 简单的神经网络构建 首先我们看一个非常简单的神经网络. 假设x1,x2是神经网络的中间层,y是我们的输出层,Y是真实值,L是loss.w1和w2是对应于x1和x2的weight. 上图用数学公式表示为: \(x2= w1 * x1\) \(y = w2 * x2\) \(L = Y - y\) 通常我们会把x1

PyTorch张量类型转换

1 numpy与CUDA之间的转换 1.tensor张量与numpy相互转换 tensor ----->numpy import torcha=torch.ones([2,5]) tensor([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]])# ********************************** b=a.numpy() array([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]], dtype=f