Pytorch-get ready with me
Posted btschang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pytorch-get ready with me相关的知识,希望对你有一定的参考价值。
from __future__ import print_function import torch
x = torch.empty(5, 3)
print(x)
tensor([[ 3.7740e+04, 4.5877e-41, -5.4795e-33], [ 3.0792e-41, 0.0000e+00, 0.0000e+00], [ 0.0000e+00, 0.0000e+00, 3.4438e-41], [ 0.0000e+00, 4.8901e-36, 2.8026e-45], [ 3.1212e+29, 0.0000e+00, 4.6243e-44]])
x = torch.rand(5, 3) print(x)
tensor([[0.1607, 0.0298, 0.7555], [0.8887, 0.1625, 0.6643], [0.7328, 0.5419, 0.6686], [0.0793, 0.1133, 0.5956], [0.3149, 0.9995, 0.6372]])
x = torch.zeros(5, 3, dtype=torch.long) print(x)
tensor([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])
x = torch.tensor([5.5, 3]) print(x)
tensor([5.5000, 3.0000])
x = x.new_ones(5, 3, dtype=torch.double) # new_* methods take in sizes print(x) x = torch.randn_like(x, dtype=torch.float) # override dtype! print(x) # result has the same size
tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], dtype=torch.float64)
tensor([[-0.2217, -0.9135, -0.6010], [-0.3193, -0.3675, 0.1951], [ 0.0646, -0.4947, 1.0374], [-0.4154, -1.0247, -1.2872], [ 0.5228, 0.3420, 0.0219]])
print(x.size())
torch.Size([5, 3])
【注意】torch.Size
是一个 tuple , 支持所有的tuple操作
相加
y = torch.rand(5, 3) print(x + y)
tensor([[ 0.2349, -0.0427, -0.5053], [ 0.6455, 0.1199, 0.4239], [ 0.1279, 0.1105, 1.4637], [ 0.4259, -0.0763, -0.9671], [ 0.6856, 0.5047, 0.4250]])
print(torch.add(x, y))
tensor([[ 0.2349, -0.0427, -0.5053], [ 0.6455, 0.1199, 0.4239], [ 0.1279, 0.1105, 1.4637], [ 0.4259, -0.0763, -0.9671], [ 0.6856, 0.5047, 0.4250]])
也可以输出给一个tensor
result = torch.empty(5, 3) torch.add(x, y, out=result) print(result)
tensor([[ 0.2349, -0.0427, -0.5053], [ 0.6455, 0.1199, 0.4239], [ 0.1279, 0.1105, 1.4637], [ 0.4259, -0.0763, -0.9671], [ 0.6856, 0.5047, 0.4250]])
# adds x to y y.add_(x) print(y)
tensor([[ 0.2349, -0.0427, -0.5053], [ 0.6455, 0.1199, 0.4239], [ 0.1279, 0.1105, 1.4637], [ 0.4259, -0.0763, -0.9671], [ 0.6856, 0.5047, 0.4250]])
【注意】Any operation that mutates a tensor in-place is post-fixed with an _
. For example: x.copy_(y)
, x.t_()
, will change x
.
输出 x 第二列的所有值
print(x[:, 1])
tensor([-0.9135, -0.3675, -0.4947, -1.0247, 0.3420])
用 torch.view resize或reshape tensor
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.size(), z.size())
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
用 .item() 提取 tensor存储的数值
x = torch.randn(1) print(x) print(x.item())
tensor([1.9218])
1.9218417406082153
Tensor 转换成 NumPy Array
a = torch.ones(5) print(a)
tensor([1., 1., 1., 1., 1.])
b = a.numpy() print(b)
[1. 1. 1. 1. 1.]
a.add_(1) print(a) print(b)
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
NumPy Array 转换成 Tensor
import numpy as np a = np.ones(5) b = torch.from_numpy(a) np.add(a, 1, out=a) print(a) print(b)
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
CUDA TENSORS
用 .to 方法
# let us run this cell only if CUDA is available # We will use ``torch.device`` objects to move tensors in and out of GPU if torch.cuda.is_available(): device = torch.device("cuda") # a CUDA device object y = torch.ones_like(x, device=device) # directly create a tensor on GPU x = x.to(device) # or just use strings ``.to("cuda")`` z = x + y print(z) print(z.to("cpu", torch.double)) # ``.to`` can also change dtype together!
tensor([2.9218], device=‘cuda:0‘)
tensor([2.9218], dtype=torch.float64)
以上是关于Pytorch-get ready with me的主要内容,如果未能解决你的问题,请参考以下文章
poj-2996 Help Me with the Game
Calico 报错 calico/node is not ready: BIRD is not ready: BGP not established with(导致 K8S 集群网络无法使用)
经典英文情歌Taylor Swift《You Belong with Me》致最爱的你!