[PyTroch系列

您所在的位置:网站首页 python如何获取小数部分 [PyTroch系列

[PyTroch系列

2023-10-24 10:26| 来源: 网络整理| 查看: 265

作者主页(文火冰糖的硅基工坊):https://blog.csdn.net/HiWangWenBing

本文网址:https://blog.csdn.net/HiWangWenBing/article/details/119429253

目录

 第1 章 Tensor运算概述

1.1 概述

1.3  “in place“运算 

1.4 Tensor的广播机制: 不同维度的tensor实例运算

1.5 环境准备

1.6 取整运算概述

第2章 向下取整:floor()

第3章 向上取整:ceil()

第4章 四舍五入的函数:around() 

第5章 裁剪取整数部分:trunc()

第6章 取小数部分:frac()

第7章 取余数运算:remainder()

 第1 章 Tensor运算概述 1.1 概述

PyTorch提供了大量的张量运算,基本上可以对标Numpy多维数组的运算,以支持对张量的各种复杂的运算。

这些操作运算中大多是对数组中每个元素执行相同的函数运算,并获得每个元素函数运算的结果序列,这些序列生成一个新的同维度的数组。

https://pytorch.org/docs/master/torch.html

1.2 运算分类

(1)算术运算:加、减、系数乘、系数除

(2)函数运算:sin,cos

(3)取整运算:上取整、下取整

(4)统计运算:最大值、最小值、均值

(5)线性代数运算:矩阵、点乘、叉乘

1.3  “in place“运算 

“in place“运算不是某个特定的函数运算,而是每个函数都有自己的“in place“运算的版本。

xxx_():执行完该操作,直接修改tensor自身的值。

基本上,每个函数都有自己的in place版本。

torch.cos() =》torch.cos_()

torch.floor() =》torch.floor_()

1.4 Tensor的广播机制: 不同维度的tensor实例运算

1.5 环境准备 import numpy as np import torch print("Hello World") print(torch.__version__) print(torch.cuda.is_available())

1.6 取整运算概述

第2章 向下取整:floor()

floor() 返回小于或者等于指定表达式的最大整数,即向下取整,即小于该数的、与该数最接近的整数。

#实例 a = torch.Tensor([0.24, 1.0, 2.1, 3, 4.55]) print ('原数组:') print (a) print ('\n') print ('运算后:') print (torch.floor(a)) #输出结果为: 原数组: tensor([0.2400, 1.0000, 2.1000, 3.0000, 4.5500]) 运算后: tensor([0., 1., 2., 3., 4.])

第3章 向上取整:ceil()

ceil() 返回大于或者等于指定表达式的最小整数,即向上取整。

#实例 a = torch.Tensor([0.24, 1.0, 2.1, 3, 4.55]) print ('原数组:') print (a) print ('\n') print ('运算后:') print (torch.ceil(a)) #输出结果为: 原数组: tensor([0.2400, 1.0000, 2.1000, 3.0000, 4.5500]) 运算后: tensor([1., 1., 3., 3., 5.])

第4章 四舍五入的函数:around() 

around() 函数返回指定数字的四舍五入值。

#实例 a = torch.Tensor([0.24, 1.0, 2.1, 3, 4.55]) print ('原数组:') print (a) print ('\n') print ('运算后:') print (torch.round(a)) #输出结果为: 原数组: tensor([0.2400, 1.0000, 2.1000, 3.0000, 4.5500]) 运算后: tensor([0., 1., 2., 3., 5.]) 第5章 裁剪取整数部分:trunc()

trunc() 函数返回指定数字的整数部分

a = torch.Tensor([0.24, 1.0, 2.1, 3, 4.55]) print ('原数组:') print (a) print ('\n') print ('运算后:') print (torch.trunc(a)) #输出结果: 原数组: tensor([0.2400, 1.0000, 2.1000, 3.0000, 4.5500]) 运算后: tensor([0., 1., 2., 3., 4.]) 第6章 取小数部分:frac()

frac() 函数返回指定数字的分数

a = torch.Tensor([0.24, 1.0, 2.1, 3, 4.55]) print ('原数组:') print (a) print ('\n') print ('运算后:') print (torch.frac(a)) 输出结果: 原数组: tensor([0.2400, 1.0000, 2.1000, 3.0000, 4.5500]) 运算后: tensor([0.2400, 0.0000, 0.1000, 0.0000, 0.5500])

第7章 取余数运算:remainder()

numpy.mod() 计算输入数组中相应元素的相除后的余数。

函数 numpy.remainder() 也产生相同的结果。

实例 a = torch.Tensor([10,10,10,10,10]) b = torch.Tensor([1,2,3,4,5]) print("原数据a") print(a) print("原数据b") print(b) #导数运算 print("运算后数据") print(a%b) print(torch.remainder(a,b)) print(a.remainder(b)) print("原数据a") print (a) 输出结果为: 原数据a tensor([10., 10., 10., 10., 10.]) 原数据b tensor([1., 2., 3., 4., 5.]) 运算后数据 tensor([0., 0., 1., 2., 0.]) tensor([0., 0., 1., 2., 0.]) tensor([0., 0., 1., 2., 0.]) 原数据a tensor([10., 10., 10., 10., 10.])

作者主页(文火冰糖的硅基工坊):https://blog.csdn.net/HiWangWenBing

本文网址:https://blog.csdn.net/HiWangWenBing/article/details/119429253



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3