Pytorch基础(机器学习)

网友投稿 1127 2022-11-17

Pytorch基础(机器学习)

Pytorch基础(机器学习)

导入​​Pytoch​​模块

import torchprint(torch.__version__)"""输出结果:1.9.0+cpu"""

注意这里导入时使用的关键字是​​torch​​​而不是​​Pytorch​​​. 其次我们可以看到这里的输出结果表示我们安装的是​​cpu​​​版本,有些电脑支持​​GPU​​​也可以安装对应的​​GPU​​版本。

创建一个​​tensor​​对象

import torcht1 = torch.Tensor([1, 2, 3])print(t1)"""输出结果:tensor([1., 2., 3.])"""

我们可以看到输出的数据类型为​​tensor​​类型。

将​​Numpy​​​中使用的类型转化为​​Pytorch​​中可以使用的类型

import torchimport numpy as nparray1 = np.arange(12).reshape(3, 4)t1 = torch.Tensor(array1)print(t1)"""输出结果:tensor([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.]])"""

可以看到我们成功将​​ndarray​​​类型数据转换为了​​tensor​​类型的数据。

创建三行四列空的​​tensor​​对象

import torcht1 = torch.empty([3, 4])print(t1)"""输出结果:tensor([[9.3674e-39, 1.0929e-38, 1.0469e-38, 1.0561e-38], [1.0286e-38, 1.0653e-38, 1.0194e-38, 4.6838e-39], [5.1429e-39, 4.9592e-39, 9.9184e-39, 9.0000e-39]])"""

这里需要注意空并不代表没有数据,此时,在我们未指定数据值的情况下,系统会自动生成一些非常小的数据。类似的还有​​torch.ones()​​​和​​torch.zeros()​​函数。

创建值位于​​[0, 1)​​区间内三行四列的随机数

import torcht1 = torch.rand([3, 4])print(t1)"""输出结果:tensor([[0.5059, 0.3887, 0.8215, 0.5488], [0.8683, 0.1002, 0.8799, 0.0522], [0.6802, 0.7736, 0.6704, 0.1614]])"""

创建值位于​​[0, 3)​​之间三行四列的随机整数

import torcht1 = torch.randint(low=0, high=3, size=[3, 4])print(t1)"""输出结果:tensor([[2, 1, 0, 2], [2, 2, 0, 0], [1, 2, 1, 0]])"""

7.获取​​tensor​​​中的数据,当​​tensor​​​中只有一个元素可用时​​tensor.item()​​

import torcht1 = torch.tensor(1)print(t1)print(t1.item())"""输出结果:tensor(1)1"""

将​​tensor​​​类型的数据转换为​​ndarray​​类型

import torcht1 = torch.tensor([1, 2])print(t1.numpy())"""输出结果:[1 2]"""

获取​​tensor​​对象的尺寸(形状)

import torcht1 = torch.tensor([[[1, 2, 3]]])print(t1.size())print(t1.size(0))print(t1.size(1))print(t1.size(2))"""输出结果:torch.Size([1, 1, 3])113"""

我们可以看到,当给定参数​​0​​​时代表输出第一个维度中的尺寸,​​1​​​代表第二个维度中的尺寸,​​2​​代表第三个维度中的尺寸。

改变​​tensor​​的尺寸(形状)

import torcht1 = torch.tensor([[[1, 2], [2, 3], [3, 4]]])print(t1.view(-1))print(t1.view(2, 3))print(t1.view(2, -1))print(t1.view(3, 2))"""输出结果:tensor([1., 2., 2., 3., 3., 4.])tensor([[1., 2., 2.], [3., 3., 4.]])tensor([[1., 2., 2.], [3., 3., 4.]])tensor([[1., 2.], [2., 3.], [3., 4.]])"""

​​t1.view(-1)​​​表示将​​tensor​​​变为一维的,​​t1.view(2, 3)​​​表示将​​tensor​​​变为两行三列的,​​t1.view(2, -1)​​​表示将​​tensor​​​变为两行三列的,其中​​-1​​表示由编译器自动计算列数。

获取​​tensor​​的维度,最大,最小值和标准差。

import torcht1 = torch.tensor([[[1, 2], [2, 3], [3, 4]]])# dim methodprint(t1.dim())# max methodprint(t1.max())# min methodprint(t1.min())# standard deviationprint(t1.std())"""输出结果:3tensor(4.)tensor(1.)tensor(1.0488)"""

低维度(一维,二维)​​tensor​​的转置操作

# 一维tensor,转置后仍旧为原来的tensort1 = torch.tensor(1)print(f"The original tensor is: {t1}")print(f"The transpose tensor of t1 is {t1.t()}")print(f"The transpose tensor of t1 is {torch.transpose(input=t1, dim0=0, dim1=-1)}")# 二维tensor,类似矩阵转置t1 = torch.tensor([[1, 2], [3, 4]])print(f"The original tensor is: {t1}")print(f"The transpose tensor of t1 is {t1.t()}")print(f"The transpose tensor of t1 is {torch.transpose(input=t1, dim0=0, dim1=1)}")"""输出结果:The original tensor is: 1The transpose tensor of t1 is 1The transpose tensor of t1 is 1The original tensor is: tensor([[1, 2], [3, 4]])The transpose tensor of t1 is tensor([[1, 3], [2, 4]])The transpose tensor of t1 is tensor([[1, 3], [2, 4]])"""

​​tensor​​切片操作

import torchimport numpy as npt1 = torch.tensor(np.arange(36).reshape((3, 3, 4)))# 获取单个值print(t1[1, 2, 1])# 获取第二个区域内的值print(t1[1, :, :])"""输出结果:tensor(21, dtype=torch.int32)tensor([[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]], dtype=torch.int32)"""

14.输出​​tensor​​中元素的数据类型

import torchimport numpy as npt1 = torch.tensor(np.arange(24).reshape((2, 3, 4)))print(t1.dtype)"""输出结果:torch.int32"""

15.定义​​tensor​​时设定内部元素数据类型

import torchimport numpy as np# 定义tensor时指定输入数据类型t1 = torch.tensor(1, dtype=torch.double)print(t1.dtype)# numpy生成数组时定义元素数据类型,之后转换为tensort1 = torch.tensor(np.array(12, dtype=np.int32))print(t1.dtype)t1 = torch.LongTensor(1, 2)print(t1.dtype)t1 = torch.DoubleTensor(1, 2)print(t1.dtype)"""输出结果:torch.float64torch.int32torch.int64torch.float64"""

16.​​tensor​​的加法运算

import torcht1 = torch.ones(3, 5)print(t1)t2 = torch.rand((3, 5))print(t2)# add methodprint(t1 + t2)print(torch.add(t1, t2))"""输出结果:tensor([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]])tensor([[0.5156, 0.1977, 0.9491, 0.4509, 0.7190], [0.9628, 0.4868, 0.9708, 0.9781, 0.7506], [0.3628, 0.0885, 0.7869, 0.9531, 0.4079]])tensor([[1.5156, 1.1977, 1.9491, 1.4509, 1.7190], [1.9628, 1.4868, 1.9708, 1.9781, 1.7506], [1.3628, 1.0885, 1.7869, 1.9531, 1.4079]])tensor([[1.5156, 1.1977, 1.9491, 1.4509, 1.7190], [1.9628, 1.4868, 1.9708, 1.9781, 1.7506], [1.3628, 1.0885, 1.7869, 1.9531, 1.4079]])"""

相应的减法,乘法和除法运算可以类比于加法运算。关于四则运算的进阶写法,可以参考这篇​​Pytorch中的四则运算高级写法​​。

码字不易,如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:关于BindingResult的使用总结及注意事项
下一篇:docker容器入门最佳教程
相关文章

 发表评论

暂时没有评论,来抢沙发吧~