PyTorch基础-Tensor的属性,数据,运算-01
Tensor的属性
import torch
a = torch.tensor([1,2,3],dtype=int) # 创建一个整数tensor
print(a)
print(a.dtype)
b = torch.tensor([1,2,3],dtype=float)# 创建一个浮点数tensor
print(b)
print(b.dtype)
c = torch.tensor([[1,2,3],[4,5,6]])# # 创建一个二维tensor
print(c)
print(c.ndim)# 数据维度
print(c.shape) # 数据形状
print(c.dtype) # 数据类型
Tensor的数据创建
import torch
torch.ones(2,3) # 创建一个2行3列全为1的数据
torch.zeros(3,3) # 创建一个3行3列全为0的数据
torch.rand(3,4)# 生成一个3行4列的随机数(0-1之间)
torch.randint(0,10,(2,3))# 生成一个0-10之间的2行3列的整数
torch.randn(3,4) # 生成一个3行4列正态分布的随机数
a = torch.tensor([[1,2],[3,4],[5,6]])
a
b = torch.randn_like(a,dtype=float)# 生成一个和a形状一样的随机数数据类型float
b
# 查看数据形状
print(b.shape)
print(b.size())
# 修改数据形状 view相当于reshape
c = b.view(6) # 把b修改成1维的
c
d = b.view(2,3) # 修改成2行3列
d
d1 = b.reshape(6)
d1
d1[1]# 切片
d1[1].item() # 标准数值 item只能修改一个值为标准数值
import numpy as np
np.array(d1) # 把tensor变成array类型
array = np.array([1,2,3])
tensor = torch.tensor(array) # 把array变成tensor类型
tensor
基本运算操作
import torch
a = torch.randint(1,5,(2,3)) # 随机生成2行3列1-5的数值
b = torch.randint(1,5,(2,3))
print(a)
print(b)
a+b # 矩阵相加
torch.add(a,b) # 矩阵相加
result = torch.zeros(2,3) # 生成一个2行3列全为0的矩阵
result
torch.add(a,b,out=result) # a+b 的结果(out=result)输出到 result中
result
# a = a+b
# 注意任何使用张量tensor会发生变化的操作都有一个前缀"_",列入a.add_()加法,b.sub_()减法
a.add_(b)
a-b
a.sub_(b)
a,b
a*b
a/b
# 取余除
a%b
# 取整除
a//b
矩阵相乘
tensor = torch.ones(3,5,dtype=int)
tensor
a1 = torch.randint(1,5,(2,3))
a1
torch.matmul(a1,tensor)#矩阵相乘 2行3列 * 3行5列 = 2行5列
a1.T # 矩阵转置 2行3列变为3行2列
sample = torch.rand(3,2)
sample
torch.sum(sample) # 求和
torch.min(sample) #最小值
torch.max(sample) #最大值
torch.argmin(sample) # 求最小值所在的位置(索引)
torch.argmax(sample) # 求最大值所在的位置(索引)
torch.mean(sample)# 求平均值
torch.median(sample)# 求中位数
torch.sqrt(sample)# 每个值计算开方
sample ** 2 # 求每个值的平方
数据的索引
import torch
tensor = torch.arange(2,14) # 生成2-14的数据arange取左不取右
print(tensor)
# 列表索引从左开始就是0 ,1,2,3 从右开始就是-1,-2,-3
print(tensor[2]) # 切片第2个数值
print(tensor[-2])# 切片倒数第2个数值
print(tensor[1:4]) # 切片 第一个到第四个
print(tensor[2:-1])
print(tensor[:5])
print(tensor[:-3])
index = [1,3,4,5,5]
tensor[index]
for t in tensor:print(t)
自动求导
import torch
x = torch.ones((2,2),requires_grad=True) # requires_grad=True 自动梯度机制开始记录追踪这个张量tensor
x
y = x + 2
y
z = y*y*3
z
out = z.mean()# 求z的平均值
out
out.backward() # backward 计算梯度的值
print(x.grad)
PyTorch基础-Tensor的属性,数据,运算-01相关推荐
- PyTorch基础(四)-----数据加载和预处理
前言 之前已经简单讲述了PyTorch的Tensor.Autograd.torch.nn和torch.optim包,通过这些我们已经可以简单的搭建一个网络模型,但这是不够的,我们还需要大量的数据,众所 ...
- python3基础3--数据类型--数据运算--表达式if -else-while-for
一.python3 数据类型 1.1 数字 例如:1,2,3,4等 1.2 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2 ...
- pytorch学习笔记 1. pytorch基础 tensor运算
pytorch与tensorflow是两个近些年来使用最为广泛的机器学习模块.开个新坑记录博主学习pytorch模块的过程,不定期更新学习进程. 文章较为适合初学者,欢迎对代码和理解指点讨论,下面进入 ...
- python中不同类型的数据不能相互运算_PyTorch中Tensor的数据类型和运算的使用
在使用Tensor时,我们首先要掌握如何使用Tensor来定义不同数据类型的变量.Tensor时张量的英文,表示多维矩阵,和numpy对应,PyTorch中的Tensor可以和numpy的ndarra ...
- 深入浅出Pytorch:02 PyTorch基础知识
深入浅出Pytorch 02 PyTorch基础知识 内容属性:深度学习(实践)专题 航路开辟者:李嘉骐.牛志康.刘洋.陈安东 领航员:叶志雄 航海士:李嘉骐.牛志康.刘洋.陈安东 开源内容:http ...
- [PyTroch系列-3]:PyTorch基础 - Hello World程序与张量(Tensor)概述
作者主页(文火冰糖的硅基工坊):https://blog.csdn.net/HiWangWenBing 本文网址:https://blog.csdn.net/HiWangWenBing/article ...
- 【深度之眼PyTorch框架班第五期】作业打卡01:PyTorch简介及环境配置;PyTorch基础数据结构——张量
文章目录 任务名称 任务简介 详细说明 作业 1. 安装anaconda,pycharm, CUDA+CuDNN(可选),虚拟环境,pytorch,并实现hello pytorch查看pytorch的 ...
- PyTorch基础(一)-----张量(Tensor)
前言 很多人都会拿PyTorch和Google的Tensorflow进行比较,这个肯定是没有问题的,因为他们是最火的两个深度学习框架了.但是说到PyTorch,其实应该先说Torch. 什么是Torc ...
- 01 Python学习--第一周--开课介绍、数据类型、数据运算、表达式、循环、三元运算、对象基本概念...
模块一主要内容 开课介绍 python发展介绍 第一个python程序 变量 字符编码与二进制 字符编码的区别与介绍 用户交互程序 if else流程判断 while 循环 while 循环优化版本 ...
最新文章
- 调用其他脚本上方法的方法
- 403错误代码导致网站样式全部乱了
- 4.3. postForObject
- 计算机无法识别华为m3,华为8寸M3(非青春版)电脑连接问题报告
- php如何让字母加1,如何使用PHP以任何顺序(从12个字母组成6个单词组成一个字母)进行字符搜索?...
- *args, **kwargs的用法
- java中equals的理解(1)
- Linux交叉编译+粤嵌LCD实现三色图
- 仿Linux中的cp操作
- Android两种 旋转Bitmap方法
- 连载31:软件体系设计新方向:数学抽象、设计模式、系统架构与方案设计(简化版)(袁晓河著)...
- 黑马程序员python入门学习笔记
- 学计算机的思维导图,电脑上最好用的6款,思维导图软件测评
- 【OpenBMC 系列】2.OpenBMC镜像编译流程
- TOGAF10标准读书会第2场活动精彩继续,高光时刻回顾!
- [转载]用JBuilder 2005实现重构之认识重构
- linkkitapp log for debug
- 免费远程协助软件,可以手机控制电脑,电脑控制电脑!
- Camera | 4.瑞芯微平台MIPI摄像头应用程序编写
- 神经网络学习(3)————BP神经网络以及python实现
热门文章
- PHP的pathinfo()
- Yii的beforeAction
- Browser控制台分析
- 序数是什么意思_序数与基数
- 开卡教程_流量卡开卡教程(必看)
- sql limit不接具体数字_SQL别再秀操作了,这么写吧还是。
- springboot整合jdbc
- eureka hostname作用_SpringCloud基础教程(三)-Eureka进阶
- android 8 esp8266,微信硬件平台(八) 1 esp8266从自己的服务器获取token
- 欧瑞变频器800参数设置_【收藏】变频器密码大全和维修常用方法!