#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.]])

pytorch之Tensor的更多相关文章

  1. 对pytorch中Tensor的剖析

    不是python层面Tensor的剖析,是C层面的剖析. 看pytorch下lib库中的TH好一阵子了,TH也是torch7下面的一个重要的库. 可以在torch的github上看到相关文档.看了半天 ...

  2. pytorch中tensor数据和numpy数据转换中注意的一个问题

    转载自:(pytorch中tensor数据和numpy数据转换中注意的一个问题)[https://blog.csdn.net/nihate/article/details/82791277] 在pyt ...

  3. [Pytorch]Pytorch中tensor常用语法

    原文地址:https://zhuanlan.zhihu.com/p/31494491 上次我总结了在PyTorch中建立随机数Tensor的多种方法的区别. 这次我把常用的Tensor的数学运算总结到 ...

  4. [Pytorch]Pytorch的tensor变量类型转换

    原文:https://blog.csdn.net/hustchenze/article/details/79154139 Pytorch的数据类型为各式各样的Tensor,Tensor可以理解为高维矩 ...

  5. Pytorch的tensor数据类型

    基本类型 torch.Tensor是一种包含单一数据类型元素的多维矩阵. Torch定义了七种CPU tensor类型和八种GPU tensor类型: Data tyoe CPU tensor GPU ...

  6. pytorch中tensor张量数据基础入门

    pytorch张量数据类型入门1.对于pytorch的深度学习框架,其基本的数据类型属于张量数据类型,即Tensor数据类型,对于python里面的int,float,int array,flaot ...

  7. pytorch之Tensor与Variable的区别

    首先在变量的操作上:Tensor对象支持在原对象内存区域上修改数据,通过“+=”或者torch.add()方法而Variable不支持在原对象内存区域上修改数据Variable对象可求梯度,并且对Va ...

  8. pytorch 创建tensor的几种方法

    tensor默认是不求梯度的,对应的requires_grad是False. 1.指定数值初始化 import torch #创建一个tensor,其中shape为[2] tensor=torch.T ...

  9. pytorch中tensor的属性 类型转换 形状变换 转置 最大值

    import torch import numpy as np a = torch.tensor([[[1]]]) #只有一个数据的时候,获取其数值 print(a.item()) #tensor转化 ...

随机推荐

  1. 性能调优的本质、Spark资源使用原理和调优要点分析

    本课主题 大数据性能调优的本质 Spark 性能调优要点分析 Spark 资源使用原理流程 Spark 资源调优最佳实战 Spark 更高性能的算子 引言 我们谈大数据性能调优,到底在谈什么,它的本质 ...

  2. LAMP配置NFS页面共享,autofs实现挂载,DNS实现名称解析,纯手动操作

    0.实验架构: 共6台服务器 分工如下: 服务器 职责 IP地址 Centos版本 描述 A DNS 172.18.7.70 7 B Apache 172.18.7.71 7 httpd+php-fp ...

  3. 深入浅出SharePoint2013——使用沙箱解决方案

    启用“Microsoft SharePoint Foundation Sandboxed Code Service”后,我们就可以使用Sandboxed solution了.

  4. win7装postgresql10.4

    第一步: 第二步: 第三步: 第四步: 第五步: 下载地址:https://get.enterprisedb.com/postgresql/postgresql-10.4-1-windows-x64. ...

  5. UVa 1640 - The Counting Problem(数论)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. 2018-2019-2 网络对抗技术 20165322 Exp6 信息搜集与漏洞扫描

    2018-2019-2 网络对抗技术 20165322 Exp6 信息搜集与漏洞扫描 目录 实验原理 实验内容与步骤 各种搜索技巧的应用 DNS IP注册信息的查询 基本的扫描技术 漏洞扫描 基础问题 ...

  7. $_cookie的使用

    设置并发送 cookie: <?php $value = "my cookie value"; // 发送一个简单的 cookie setcookie("TestC ...

  8. [Python]json对象转换出错expected string or buffer python

    [问题] 今天在使用python中的json转换碰到一个问题: 错误显示: expected string or buffer json内容如下: {u'err_no': 0, u'corpus_no ...

  9. sudo 启动tomcat报错没有java环境

    报错: Cannot find ./catalina.shThe file is absent or does not have execute permissionThis file is need ...

  10. Maven profile动态选择配置条件

    背景 在开发过程中,我们的软件会面对不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置.以及一些软件运行过程中的基本配置, ...