Pytorch的学习笔记(一:属性统计&&范数讲解)

Pytorch的学习笔记(一:属性统计&&范数讲解)

参考博客链接:https://blog.csdn.net/wohu1104/article/details/107283396

                         https://zhuanlan.zhihu.com/p/68833474

tensor的基本运算函数

(一)mean均值    sum和    median中位数    mode众数

(二)norm范数    dist距离

(三)std标准差(待更)    var方差(待更)

(四)cumsum累加    cumprod累积

(五)张量torch和数组numpy的转换

以上大多数函数都有一个参数dim,用来指定这些操作是在哪个维度上执行的

(一)

import torch
a = torch.Tensor([[2, 4, 3], [4, 4, 4], [2, 2, 1]])
print(a)
print(a.size())

print("=" * 40) 

print(torch.mean(a))
print(a.mean())
print(torch.mean(a, dim = 0))
print(a.mean(dim = 0))
print(torch.mean(a, dim = 1))

print("=" * 40)

print(a.sum())
print(a.sum(dim = 0))
print(a.sum(dim = 1))

print("=" * 40)
#中位数
print(a.median())
print(a.median(dim = 0))
print(a.median(dim = 1))

print("=" * 40)
#众数
print(a.mode())
print(a.mode(dim = 0))
print(a.mode(dim = 1))
tensor([[2., 4., 3.],
        [4., 4., 4.],
        [2., 2., 1.]])
torch.Size([3, 3])
========================================
tensor(2.8889)
tensor(2.8889)
tensor([2.6667, 3.3333, 2.6667])
tensor([2.6667, 3.3333, 2.6667])
tensor([3.0000, 4.0000, 1.6667])
========================================
tensor(26.)
tensor([ 8., 10.,  8.])
tensor([ 9., 12.,  5.])
========================================
tensor(3.)
torch.return_types.median(
values=tensor([2., 4., 3.]),
indices=tensor([0, 0, 0]))
torch.return_types.median(
values=tensor([3., 4., 2.]),
indices=tensor([2, 0, 0]))
========================================
torch.return_types.mode(
values=tensor([2., 4., 2.]),
indices=tensor([0, 2, 1]))
torch.return_types.mode(
values=tensor([2., 4., 1.]),
indices=tensor([2, 1, 2]))
torch.return_types.mode(
values=tensor([2., 4., 2.]),
indices=tensor([0, 2, 1]))

(二)

向量范数

0-范数:向量中非零元素的个数

1-范数:即向量元素绝对值之和

2-范数:(欧几里得范数、常用计算向量长度),即向量元素绝对值的平方和再开方、优化正则化项、避免过拟合

\infty-范数:即所有向量元素绝对值中最大值

p-范数:向量元素绝对值的p次方和的1/p次幂

矩阵范数

1-范数:所有矩阵列向量绝对值之和的最大值(列和范数)

2-范数:即A^{T}A矩阵最大特征值的开平方、\lambdaA^{T}A的最大特征值、\left \| A \right \|_{2} = \sqrt{\lambda }

\infty-范数:所有矩阵行向量绝对值之和的最大值(行和范数)

F-范数:Frobenius范数,即矩阵元素绝对值的平方和再开平方

核范数:矩阵奇异值之和【矩阵奇异值(待更)】

dist距离(默认即为欧式距离)

p = 1 欧式距离(相减的平方和 的 开方)

p = 2 哈密尔顿距离(相减绝对值累加)

import torch 
import torch.tensor as tensor
import numpy 
a = torch.ones(2, 3)
print(a)

#默认即为二范数
print(a.norm())
a2 = torch.norm(a)
print(a2)
a1 = torch.norm(a, p = 1)
print(a1)
a0 = torch.norm(a, p = 0)
print(a0)
ainf = torch.norm(a, p = float('inf'))
print(ainf)

a = torch.Tensor([[2, 4],[1, 3]])
print(a.norm())
print(a.norm(dim = 0))
print(a.norm(dim = 1))

b = torch.Tensor([4, 0])
print(a.dist(b))
print(torch.dist(a, b, p = 2))
print(torch.dist(a, b, p = 1))
tensor([[1., 1., 1.],
        [1., 1., 1.]])
tensor(2.4495)
tensor(2.4495)
tensor(6.)
tensor(6.)
tensor(1.)
tensor(5.4772)
tensor([2.2361, 5.0000])
tensor([4.4721, 3.1623])
tensor(6.1644)
tensor(6.1644)
tensor(12.)

(三)

import torch 
a = torch.Tensor([[2, 4], [1, 3]])

print(a.std())
print(a.std(dim = 0))
print(a.std(dim = 1))

print(a.var())
print(a.var(dim = 0))
print(a.var(dim = 1))
tensor(1.2910)
tensor([0.7071, 0.7071])
tensor([1.4142, 1.4142])
tensor(1.6667)
tensor([0.5000, 0.5000])
tensor([2., 2.])

(四)

cumsum、cumprod 累加累乘

import torch
a = torch.Tensor([[2, 4], [1, 3]])

print(a.cumsum(dim = 0))
print(a.cumsum(dim = 1))

print(a.cumprod(dim = 0))
print(a.cumprod(dim = 1))
tensor([[2., 4.],
        [3., 7.]])
tensor([[2., 6.],
        [1., 4.]])
tensor([[ 2.,  4.],
        [ 2., 12.]])
tensor([[2., 8.],
        [1., 3.]])

(五)

张量Tensors类似于numpy的ndarrays, 另外还可以在GPU上使用Tensors来加速计算

(张量的操作、移调,索引,切片,数学运算,线性代数,随机数等等描述)

import torch
x = torch.ones(5, 3)
print(x)
print(x.size())
y = torch.rand(5, 3)
print(x + y)
print(torch.add(x, y))

result = torch.Tensor(5, 3)
torch.add(x, y, out = result)
print(result)

y.add_(x)
print(y)

#输出第一列的信息
print(y[:, 0])
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
torch.Size([5, 3])
tensor([[1.1555, 1.2997, 1.1562],
        [1.1778, 1.8985, 1.9427],
        [1.4613, 1.5878, 1.8851],
        [1.8738, 1.6801, 1.8564],
        [1.6713, 1.5740, 1.1392]])
tensor([[1.1555, 1.2997, 1.1562],
        [1.1778, 1.8985, 1.9427],
        [1.4613, 1.5878, 1.8851],
        [1.8738, 1.6801, 1.8564],
        [1.6713, 1.5740, 1.1392]])
tensor([[1.1555, 1.2997, 1.1562],
        [1.1778, 1.8985, 1.9427],
        [1.4613, 1.5878, 1.8851],
        [1.8738, 1.6801, 1.8564],
        [1.6713, 1.5740, 1.1392]])
tensor([[1.1555, 1.2997, 1.1562],
        [1.1778, 1.8985, 1.9427],
        [1.4613, 1.5878, 1.8851],
        [1.8738, 1.6801, 1.8564],
        [1.6713, 1.5740, 1.1392]])
tensor([1.1555, 1.1778, 1.4613, 1.8738, 1.6713])

Numpy转换

numpy数组和tensor张量可以相互转换(可以使用.cuda功能将传感器移动到GPU上 torch.cuda.is_available())

import torch
import numpy as np

a = torch.ones(5)
print(a)
#将tensor张量转换为numpy数组
b = a.numpy()
print(b)

a.add_(1)
print(a)
print(b)

#numpy数组转换成tensor张量
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out = a)
print(a)
print(b)
a[0] += 1
print(a)
print(b)
b[0] += 1
print(a)
print(b)
tensor([1., 1., 1., 1., 1.])
[1. 1. 1. 1. 1.]
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
[3. 2. 2. 2. 2.]
tensor([3., 2., 2., 2., 2.], dtype=torch.float64)
[4. 2. 2. 2. 2.]
tensor([4., 2., 2., 2., 2.], dtype=torch.float64)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Pytorch是机器学习中的一个重要框架,它与TensorFlow一起被认为是机器学习的两大框架。Pytorch学习可以从以下几个方面入手: 1. Pytorch基本语法:了解Pytorch的基本语法和操作,包括张量(Tensors)的创建、导入torch库、基本运算等\[2\]。 2. Pytorch中的autograd:了解autograd的概念和使用方法,它是Pytorch中用于自动计算梯度的工具,可以方便地进行反向传播\[2\]。 3. 使用Pytorch构建一个神经网络:学习使用torch.nn库构建神经网络的典型流程,包括定义网络结构、损失函数、反向传播和更新网络参数等\[2\]。 4. 使用Pytorch构建一个分类器:了解如何使用Pytorch构建一个分类器,包括任务和数据介绍、训练分类器的步骤以及在GPU上进行训练等\[2\]。 5. Pytorch的安装:可以通过pip命令安装Pytorch,具体命令为"pip install torch torchvision torchaudio",这样就可以在Python环境中使用Pytorch了\[3\]。 以上是一些关于Pytorch学习笔记,希望对你有帮助。如果你需要更详细的学习资料,可以参考引用\[1\]中提到的网上帖子,或者查阅Pytorch官方文档。 #### 引用[.reference_title] - *1* [pytorch自学笔记](https://blog.csdn.net/qq_41597915/article/details/123415393)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Pytorch学习笔记](https://blog.csdn.net/pizm123/article/details/126748381)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值