1.基本概念

标量:就是一个数,是0维的,只有大小,没有方向

向量:是1*n的一列数,是1维的,有大小,也有方向

张量:是n*n的一堆数,是2维的,n个向量合并而成

2.a.size(),a.shape(),a.numel(),a.dim()的区别

a.size():输出a的某一维度中元素的个数,若未指定维度,则计算所有元素的个数

a.shape():输出a数组各维度的长度信息,返回是元组类型。

a.numel():输出a占用内存的数量

a.dim():输出a的维数

3.tensor的基本函数:

import torch
import numpy as np if __name__ == '__main__':
# 随机正太分布
a=torch.randn(2,3)
print("a:",a)
print("a.size():",a.size())
print("a.size(0):",a.size(0))
print("a.size(1):",a.size(1))
print("a.shape[0]:",a.shape[0])
print("a.shape[1]:",a.shape[1])
print("a.shape:",a.shape)
# 将a.shape转换成list
print("list(a.shape):",list(a.shape))
# 输出a占用内存的数量=2*3
print("a.numel():",a.numel())
# 输出a的维数
print("a.dim():",a.dim())
print() # 0~1随机均匀分布
b=torch.rand(2,3,4)
print("b:",b)
print("b.size():",b.size())
print("b.size(0):",b.size(0))
print("b.size(1):",b.size(1))
print("b.shape[0]:",b.shape[0])
print("b.shape[1]:",b.shape[1])
print("b.shape:",b.shape)
print("list(b.shape):",list(b.shape))
# 输出b占用内存的数量=2*3*4
print("b.numel():",b.numel())
print("b.dim():",b.dim())
print()

运行结果:

a: tensor([[-0.2106, -2.1292, -0.8221],
[-1.5805, 0.2592, -1.1203]])
a.size(): torch.Size([2, 3])
a.size(0): 2
a.size(1): 3
a.shape[0]: 2
a.shape[1]: 3
a.shape: torch.Size([2, 3])
list(a.shape): [2, 3]
a.numel(): 6
a.dim(): 2 b: tensor([[[0.8126, 0.8908, 0.3507, 0.1554],
[0.8679, 0.5295, 0.5461, 0.5021],
[0.2570, 0.2250, 0.6310, 0.0662]], [[0.1139, 0.9552, 0.5847, 0.5421],
[0.3589, 0.0090, 0.0324, 0.6984],
[0.9562, 0.4533, 0.4296, 0.4052]]])
b.size(): torch.Size([2, 3, 4])
b.size(0): 2
b.size(1): 3
b.shape[0]: 2
b.shape[1]: 3
b.shape: torch.Size([2, 3, 4])
list(b.shape): [2, 3, 4]
b.numel(): 24
b.dim(): 3

tensor的创建:

# 将一个numpy的变量转变成torch类型的
# c是一个一行两列的,[2.2 , 3.3] 矩阵
c=np.array([2.2,3.3])
print(torch.from_numpy(c)) # d是一个三行四列的值为1的矩阵
d=np.ones([3,4])
# 导入后类型转变成了torch.float64()
print(torch.from_numpy(d))
print() # 小写的tensor()接受的是现有的数据,一行两列
print("torch.tensor([2,3]):\n",torch.tensor([2,3]))
print()
# 大写的FloatTensor()接受的是数据的维度,两行三列,(也可以接受现有的数据)
print("torch.FloatTensor(2,3):\n",torch.FloatTensor(2,3))
print() # 不推荐使用上边的方法,因为上边初始化,会生成非常大或者非常小的值
# 而是推荐使用随机生成指定数值区间的初始化
# rand(2,3):随机生成值在0~1区间的,两行三列的的张量
print("torch.rand(2,3):\n",torch.rand(2,3))
print() # rand_like() 参数是一个张量(tensor),相当于把e的shape读出来,之后再送给rand函数
e=torch.rand(3,4)
print("torch.rand_like(e): //e是一个三行四列的张量\n",torch.rand_like(e))
print() # randint() 参数依次是,randint(最小值,最大值,shape),取不到最大值
print("torch.randint(1,10,[3,4]):\n",torch.randint(1,10,[3,4]))

运行结果:

tensor([2.2000, 3.3000], dtype=torch.float64)
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]], dtype=torch.float64) torch.tensor([2,3]):
tensor([2, 3]) torch.FloatTensor(2,3):
tensor([[0., 0., 0.],
[0., 0., 0.]]) torch.rand(2,3):
tensor([[0.4876, 0.7776, 0.3553],
[0.3311, 0.9068, 0.0672]]) torch.rand_like(e): //e是一个三行四列的张量
tensor([[0.0792, 0.8138, 0.6931, 0.8604],
[0.2047, 0.9061, 0.8075, 0.1821],
[0.0216, 0.2109, 0.4703, 0.7405]]) torch.randint(1,10,[3,4]):
tensor([[8, 3, 2, 7],
[6, 3, 8, 6],
[1, 4, 1, 5]])

4.tensor的创建与切片

import torch

if __name__ == '__main__':

    # 生成一个两行三列的矩阵,并把所有值赋值为3.92
c=torch.full((2, 3), 3.92)
print('torch.full((2, 3), 3.92):\n',c,'\n') # 步长为2,按序生成0~10之间的数字
d=torch.arange(0,10,step=2)
print('d=torch.arange(0,10,step=2):\n',d,'\n') # 均匀生成某段数据
e=torch.linspace(0,10,steps=10)
print('torch.linspace(0,10,steps=10):\n',e,'\n')
e1=torch.linspace(0,10,steps=11)
print('e1=torch.linspace(0,10,steps=11):\n',e1,'\n') # 值全为1矩阵
f=torch.ones(3,3)
print('torch.ones(3,3):\n',f,'\n')
# 值全为零矩阵
f1=torch.zeros(3,3)
print('torch.zeros(3,3):\n',f1,'\n')
# 单位矩阵
f2=torch.eye(3,3)
print('torch.eye(3,3):\n',f2,'\n') g=torch.rand(4,3,28,28)
print('shape的基本使用:')
print(g[0].shape)
print(g[0,0].shape)
print(g[0,0,2,4]) print('\ntensor的切片使用:')
# 取前两张图片
print(g[:2].shape)
# 取第二张图片向后及第一个通道向后
print(g[2:,1:].shape)
# 行:隔七个采一个样,列:隔14个采一个样,(start:stop:step)
print(g[0,0,0:28:7,::14]) h=torch.randn(3,4)
print('\n',h)
# 矩阵中值大于0.5的 赋值为ture
mask=h.__ge__(0.5)
print(mask)
print(torch.masked_select(h,mask))

运行结果:

torch.full((2, 3), 3.92):
tensor([[3.9200, 3.9200, 3.9200],
[3.9200, 3.9200, 3.9200]]) d=torch.arange(0,10,step=2):
tensor([0, 2, 4, 6, 8]) torch.linspace(0,10,steps=10):
tensor([ 0.0000, 1.1111, 2.2222, 3.3333, 4.4444, 5.5556, 6.6667, 7.7778,
8.8889, 10.0000]) e1=torch.linspace(0,10,steps=11):
tensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]) torch.ones(3,3):
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]) torch.zeros(3,3):
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]) torch.eye(3,3):
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]) shape的基本使用:
torch.Size([3, 28, 28])
torch.Size([28, 28])
tensor(0.7568) tensor的切片使用:
torch.Size([2, 3, 28, 28])
torch.Size([2, 2, 28, 28])
tensor([[0.4571, 0.3198],
[0.6540, 0.3359],
[0.2601, 0.8069],
[0.9713, 0.6876]])
tensor([[-2.4096, 1.1243, -1.0314, -1.4685],
[-2.5054, 0.7131, -0.0376, -0.2110],
[ 1.8922, 1.8989, 0.0459, -1.6457]])
tensor([[False, True, False, False],
[False, True, False, False],
[ True, True, False, False]])
tensor([1.1243, 0.7131, 1.8922, 1.8989])

Pytorch-tensor的创建,索引,切片的更多相关文章

  1. pytorch张量数据索引切片与维度变换操作大全(非常全)

    (1-1)pytorch张量数据的索引与切片操作1.对于张量数据的索引操作主要有以下几种方式:a=torch.rand(4,3,28,28):DIM=4的张量数据a(1)a[:2]:取第一个维度的前2 ...

  2. Pytorch Tensor 常用操作

    https://pytorch.org/docs/stable/tensors.html dtype: tessor的数据类型,总共有8种数据类型,其中默认的类型是torch.FloatTensor, ...

  3. Python array,list,dataframe索引切片操作 2016年07月19日——智浪文档

    array,list,dataframe索引切片操作 2016年07月19日——智浪文档 list,一维,二维array,datafrme,loc.iloc.ix的简单探讨 Numpy数组的索引和切片 ...

  4. 列表的初识,列表的索引切片,列表的增删改查,列表的嵌套,元组的初识,range

    1 内容总览 列表的初识 列表的索引切片 列表的增删改查 列表的嵌套 元组的初识(了解) 元组的简单应用(了解) range 2 具体内容 列表的初识 why: str: 存储少量的数据.切片出来全都 ...

  5. SQL语句-创建索引

    语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名)WITH FILLFACTOR = 填充因子值0~100 GO USE 库名GO IF EXISTS (SELECT * FRO ...

  6. *使用while循环遍历数组创建索引和自增索引值

    package com.chongrui.test;/* *使用while循环遍历数组 *  *  * */public class test {    public static void main ...

  7. 程序员眼中的 SQL Server-执行计划教会我如何创建索引?

    先说点废话 以前有 DBA 在身边的时候,从来不曾考虑过数据库性能的问题,但是,当一个应用程序从头到脚都由自己完成,而且数据库面对的是接近百万的数据,看着一个页面加载速度像乌龟一样,自己心里真是有种挫 ...

  8. SQL Server创建索引(转)

    什么是索引 拿汉语字典的目录页(索引)打比方:正如汉语字典中的汉字按页存放一样,SQL Server中的数据记录也是按页存放的,每页容量一般为4K .为了加快查找的速度,汉语字(词)典一般都有按拼音. ...

  9. hive创建索引

    索引是hive0.7之后才有的功能,创建索引需要评估其合理性,因为创建索引也是要磁盘空间,维护起来也是需要代价的 创建索引 hive> create index [index_studentid ...

  10. MongoDB性能篇之创建索引,组合索引,唯一索引,删除索引和explain执行计划

    这篇文章主要介绍了MongoDB性能篇之创建索引,组合索引,唯一索引,删除索引和explain执行计划的相关资料,需要的朋友可以参考下 一.索引 MongoDB 提供了多样性的索引支持,索引信息被保存 ...

随机推荐

  1. mysql中innodb创建表的一些限制

    1. 背景 在新创建mysql数据表的时候.不太确定表能创建多少个字段,多少个索引.索引多少有限制么?mysql的数据是怎么存储的存在在哪里. 2.基本个数限制 在MySQL5.6.9以后的版本,一个 ...

  2. 20个Python random模块的代码示例

    本文分享自华为云社区<Python随机数探秘:深入解析random模块的神奇之处>,作者:柠檬味拥抱. 标准库random函数大全:探索Python中的随机数生成 随机数在计算机科学和数据 ...

  3. TimeLine 时间轴 网站 分享 time.graphics - nodejs 10年图

    话说20年 弹指一挥间 https://time.graphics/line/682014 nodejs 10年图 https://time.graphics/line/598790 资料 2022, ...

  4. c语言中静态链接库的创建和使用

    静态链接库的创建 静态链接库其实就相当于压缩包,其内部可以包含多个源文件.但需要注意的是,并非任何一个源文件都可以被加工成静态链接库,其至少需要满足以下 2 个条件: 源文件中只提供可以重复使用的代码 ...

  5. IDEA或Android Studio用书签功能标识代码

    原文地址:IDEA或Android Studio用书签功能标识代码 | Stars-One的杂货小窝 在之前的维护工作中,有时候翻源码的时候,找到了一个文件的某行关键,之后一层层进去之后,又突然不记得 ...

  6. day03-分析SpringBoot底层机制

    分析SpringBoot底层机制 Tomcat启动分析,Spring容器初始化,Tomcat如何关联Spring容器? 1.创建SpringBoot环境 (1)创建Maven程序,创建SpringBo ...

  7. cpprest示例微服务链路嵌套调用层数1000以及跟踪

    本demo使用本人两个github项目cpprestsdk4mingw,zhepler-wxWdigets编写,一个简单的rest服务器cpprestbox,只提供GET方法方便测试,可以添加修改ap ...

  8. ubuntu重启网卡

    1.关闭接口:sudo ifconfig eth0 down 2.然后打开:sudo ifconfig eth0 up

  9. Java递归实现全排列改进(二)---利用ArrayList实现去重

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Test{  priv ...

  10. 【2302. 统计得分小于 K 的子数组数目】前缀和+二分

    class Solution { public static void main(String[] args) { Solution solution = new Solution(); soluti ...