绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
Pytorch学习之torch基本用法
2020-05-22 10:07:51


pytorch的一个基本概念是张量,张量也可以称为多维矩阵。

例如,标量:为0维张量         向量:为1维张量           矩阵:为2维张量 .......

张量除了有维度、大小和元素个数之外,还有元素的类型

张量有大量的初始化方法,可以和list以及numpy数组进行转换

此外张量还有大量的计算函数

如下:

from __future__ import print_function
import torch
import numpy as np
# 常用矩阵创建函数
# torch.tensor(data, dtype) # data 可以是Numpy中的数组
# torch.as_tensor(data) #为data生成tensor。
# torch.from_numpy(ndarray)
# torch.empty(size)
# torch.empty_like(input)
l=[[1,2,3],[4,5,6]] #列表
nparray=np.array(l) #numpy数组
print('list=',l)
print('np.array=',nparray)
x=torch.tensor(nparray) #numpy数组转张量
print('torch.tensor=',x)
x=torch.tensor([[1,2,3,4],[5,6,7,8]])   #列表转张量
print('torch.tensor=',x)
x=torch.as_tensor(l)    #列表转张量
print('torch.as_tensor=',x)
x=torch.as_tensor(nparray)  #numpy数组转张量
print('torch.as_tensor=',x)
x=torch.from_numpy(nparray) #numpy数组转张量
print('torch.as_tensor=',x)
x=torch.empty(5,3)  #创建空张量,实际值为
print('torch.empty=',x)
y=torch.empty_like(x)   #创建和input张量同阶的空张量,实际值为
print('torch.empty_like=',y)
# 全零/全一/单位矩阵
# torch.zeros(size)
# torch.zeros_like(input, dtype)
# torch.ones(size)
# torch.ones_like(input, dtype)
# torch.eye(size)
x=torch.zeros(5,3)
print('torch.zeros=',x)
y=torch.tensor([[1,2,3,4],[5,6,7,8]])
x= torch.zeros_like(y)
print('torch.zeros_like=',x)
x=torch.ones(5,3)
print('torch.ones=',x)
y=torch.tensor([[1,2,3,4],[5,6,7,8]])
x= torch.ones_like(y)
print('torch.ones_like=',x)
x= torch.eye(6)
print('torch.eye=',x)
# 序列生成
# torch.arange(start, end, step)  # 不包括end, step是两个点间距
# torch.range(start, end, step) # 包括end,step是两个点间距
# torch.linspace(start, end, steps) # 包括end, steps 是点的个数,包括端点, (等距离)
# torch.logspace(start, end, steps) # 
x=torch.arange(1,10,2)  #step为间距
print('torch.arange=',x)
x=torch.range(1,10,2)   #step为间距
print('torch.range=',x)
x=torch.linspace(1,10,5)    #step为间距个数,线性
print('torch.linspace=',x)
x=torch.logspace(1,10,5)    #step为间距个数,对数
print('torch.logspace=',x)
# 稀疏矩阵
# torch.sparse_coo_tensor(indices, values, size) # indices 值的x-y坐标,size 稀疏矩阵的大小
indices = torch.tensor([[0, 1, 1], [2, 0, 2]])
values = torch.tensor([345], dtype=torch.float32)
x = torch.sparse_coo_tensor(indices, values, [24])
print('torch.sparse_coo_tensor=',x)
# 相同值填充矩阵
# torch.full(size, fill_value)
# torch.full_like(input, fill_value)
x = torch.full((2,3),0.5)
print('torch.full=',x)
y=torch.tensor([[1,2,3,4],[5,6,7,8]])
x=torch.full_like(y,5)
print('torch.full_like=',x)
# 随机矩阵生成
# torch.rand(size) # 数值范围[1), size = [2,3or 2,3
# torch.rand_like(input, dtype) # 形状和input相同
# torch.randn(size)  # 标准正态分布 N(,1)
# torch.randn_like(input, dtype)
# torch.randint(low = , high, size) # 整数范围[low, high),  e.g. torch.randint(38, [2,3])
# torch.randint_like(input, low = , high, dtype)
x=torch.rand(5,3)   #包含了从区间[1)的均匀分布中抽取的一组随机数
print('torch.rand=',x)
x=torch.randn(5,3)  #包含了从标准正态分布(均值为,方差为1,即高斯白噪声)中抽取的一组随机数
print('torch.randn=',x)
x=torch.randint(38, [5,3])    #low和high之间的随机数
print('torch.randint=',x)
# 随机排列生成
# torch.randperm(n) # 生成一个到n-1的n-1个整数的随机排列
x=torch.randperm(10)
print('torch.randperm=',x)  
# 计算函数
# torch.abs(input,out) # tensor 输出张量元素值
# torch.acos(input,out) # 求反余弦
# torch.add(input,value,out)  # 对每个张量元素逐个加上value
# torch.addcdiv(tensor,value=1,tensor1,tensor2) # 张量(tensor1/tensor2)*value+tensor 用tensor2对tensor1逐元素相除,然后乘以标量值value 并加到tensor。
# torch.addcmul(tensor, value=1, tensor1, tensor2, out=None) # 用tensor2对tensor1逐元素相乘,并对结果乘以标量值value然后加到tensor。张量的形状需要匹配的
# torch.ceil(input,out)  # 向上取整
# torch.clamp(input,min,max,out=None) # 将元素调整至[min,max]区间
# torch.div(input,value)  # 除
# torch.exp(tensor,out)  # 指数
# torch.floor(input,out)  # 向下去整
# torch.fmod(input,divisor,out)  # 取余数
# torch.frac  # 取分数部分
# torch.lerp(start, end, weight, out=None) # 线性插值:out = start+weight*(end-start)
# torch.log(input, out=None)  # 取自然对数
# torch.mul(input, value, out=None)
# torch.mul(input, other, out=None)  # 哈达玛积
# torch.pow(input, exponent, out=None)  # 求幂
# torch.reciprocal(input, out=None) # Tensor 去倒数
# torch.remainder(input, divisor, out=None) # Tensor 取余数
# torch.rsqrt(input, out=None) # Tensor 平方根倒数
# torch.sigmoid(input, out=None) # Tensor sigmoid值
# torch.sigmoid(input, out=None) # Tensor 符号函数
# torch.cumprod(input, dim, out=None) # Tensor 按指定维度累积
# torch.cumsum(input, dim, out=None) # Tensor 指定维度累加
# torch.dist(input, other, p=2, out=None) # Tensor 求P范数
# torch.mean(input) # float 均值
# torch.mean(input, dim, out=None) # Tensor 指定维度均值
# torch.median(input, dim=-1, values=None, indices=None) -> (Tensor, LongTensor) 指定维度中位数
# torch.mode(input, dim=-1, values=None, indices=None) -> (Tensor, LongTensor) 众数
# torch.norm(input, p, dim, out=None) # Tensor 指定维度p范数
# torch.prod(input) # float 所有积
# torch.prod(input, dim, out=None) # Tensor 指定维度积
# torch.std(input, dim, out=None) # Tensor 标准差
# torch.sum(input, dim, out=None) # Tensor 按维度求和
# torch.sum(input) # float 所有元素和var 按行方差,所有元素方差
# torch.eq(input, other, out=None) # Tensor 相等比较操作 返回01
# torch.equal(tensor1, tensor2) # bool 张量比较shape and value返回bool
# torch.ge(input, other, out=None) # Tensor 大于
# torch.gt(input, other, out=None) # Tensor 与equal类似返回不同
# torch.kthvalue(input, k, dim=None, out=None) -> (Tensor, LongTensor) 取指定维度小值
# torch.le(input, other, out=None) # Tensor 小于等于
# torch.lt(input, other, out=None) # Tensor 小于
# torch.max(input, dim, max=None, max_indices=None) -> (Tensor, LongTensor) 返回指定维度大值和索引
# torch.log2(),torch.log10() # 其他210为低的指数
# torch.cos()、torch.sin()、torch.tan() # 三角函数
# torch.acos()、torch.asin()、torch.atan()  # 反三角函数


分享好友

分享这个小栈给你的朋友们,一起进步吧。

追梦IT人
创建时间:2020-02-12 11:47:47
20年IT工作经验,曾在华为、HP、移动、电网等国内外知名IT企业任职;关注领域包括证券、航空、制造、电信、电网等。在数据库开发和优化、数据仓库、系统架构、大中型项目管理、部门管理、数据挖掘和分析、数据治理、大数据方面有一定研究。
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

栈主、嘉宾

查看更多
  • bq_wang
    栈主

小栈成员

查看更多
  • 栈栈
  • 小雨滴
  • 我没
  • 飘絮絮絮丶
戳我,来吐槽~