脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - pytorch教程之Tensor的值及操作使用学习

pytorch教程之Tensor的值及操作使用学习

2022-01-01 00:23xz1308579340 Python

这篇文章主要为大家介绍了pytorch教程中关于Tensor的操作使用,有需要的朋友可以借鉴参考下,希望可以有所帮助,祝大家升职加薪,共同进步

参考网址

1、Tensors

Tensors are similar to NumPy's ndaeeays,不同的是可以在GPU上使用和加速计算。
导入包

?
1
2
from __future__ import print_function
import torch

建立5*3的矩阵,未初始化

?
1
2
x = torch.empty(5,3)
print(x)

out

?
1
2
3
4
5
tensor([[ 1.4395e-364.5848e-411.4395e-36],
        [ 4.5848e-411.4395e-364.5848e-41],
        [ 1.4395e-364.5848e-412.8026e-45],
        [-1.9501e+008.5165e+230.0000e+00],
        [ 2.5223e-430.0000e+000.0000e+00]])

建立随机初始化矩阵

?
1
2
x = torch.rand(5,3)
print(x)

out

?
1
2
3
4
5
tensor([[ 0.80740.91750.8109],
        [ 0.33130.59020.9179],
        [ 0.65620.32830.9798],
        [ 0.82180.08170.4454],
        [ 0.59340.00400.3411]])

建立零初始化矩阵,数据类型是Long

?
1
2
3
4
...
x = torch.zeros(5,3,dtype = torch.long)
print(x)
...

out

?
1
tensor([[ 000],<br>        [ 000],<br>        [ 000],<br>        [ 000],<br>        [ 000]])

建立一个tensor数据来源于data

?
1
2
x = torch.tensor([5.5,3])
print(x)

out

?
1
tensor([ 5.50003.0000])

在原有tnesor的基础上形成新的tensor,会继承原有tensor的shapee和dtype等属性,当然我么也可以修改这些属性

?
1
2
3
4
x = x.new_ones(5,3,dtype = torch.double)
print(x)
x = torch.randn_like(x,dype = torch.float)
print(x)

out

?
1
2
3
4
5
6
7
8
9
10
tensor([[ 1.1.1.],
        [ 1.1.1.],
        [ 1.1.1.],
        [ 1.1.1.],
        [ 1.1.1.]], dtype=torch.float64)
tensor([[-0.0730, -0.0716, -0.8259],
        [-1.70040.8790, -0.0659],
        [-0.89690.8736, -0.6035],
        [-0.1539, -2.9178, -0.7456],
        [-0.02450.40751.4904]])

获取tensor的size

?
1
print(x.size())

out

?
1
torch.Size([5, 3])

torch.size是一个元组,支持所有元组(tuple)的操作

2、对Tensor的操作

实现加法的四种方式

方法一L

?
1
print(x+y)

方法二

?
1
print(torch.add(x,y))

方法三:输出给额外的tensor

?
1
2
3
result = torch.empty(5,3)
torch.add(x,y ,out= result)
print (result)

方法四:原地替换-结果存放在y中

?
1
print(y)

所有原地替换

所有原地替换tensor的操作都有后缀,比如x.copy(y),会改变x

使用标准的numpy操作

?
1
print(x[:1]

out

?
1
tensor([-0.07160.87900.8736, -2.91780.4075])

使用torch.view 改变tensor的形状

?
1
2
3
4
x = torch.randn(4,4)
y = x.view(16)
z = x.view(-1,8)   # the size -1 is inferred from other dimensions
print (x.size(),y.xize(),z.size())

out

?
1
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])

tensor转化为numpy的数字,使用item

?
1
2
3
x = torch.rnadn(1)
print(x)
print(x.item())

Torch Tensor 和numpy的相互转换

?
1
2
a = torch.ones(5)
print (a)

out

?
1
tensor([ 1.1.1.1.1.])

并且改变tensor的值会同时改变numpy的值

?
1
2
3
a.add_(1)
print(a)
print(b)

out

?
1
2
tensor([ 2.2.2.2.2.])
[ 2.  2.  2.  2.  2.]

将numpy array转化为pytorch Tensor

?
1
2
3
4
5
6
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a,1,out = a )
print(a)
print(b)

out

?
1
2
[ 22222.]
tensor([ 2.,  2.,  2.,  2.,  2.], dtype=torch.float64)

所有在cpu上的tensor都支持numpy转化,除了char形的tensor

CUDA Tensors

Tensors 可以被移动到其他设备使用.to的方法

?
1
2
3
4
5
6
7
8
9
...
if torch.cuda.is_avaulable():
device = torch.device(“cuda”)
y = torch.ones_like(x,device = devcie)
x= x.to(device)
z = x+y
print(z)
print(z.to(“cpu”,torch.double))
...

out

?
1
2
tensor([-1.0620], device='cuda:0')
tensor([-1.0620], dtype=torch.float64)

以上就是pytorch教程之Tensor学习笔记的详细内容,更多关于pytorch教程的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/xz1308579340/article/details/80078225

延伸 · 阅读

精彩推荐