from __future__ import print_function
import torch
x = torch.empty(5, 3)
print(x)
tensor([[-3.9455e+14, 4.5659e-41, -3.9455e+14],
[ 4.5659e-41, nan, 0.0000e+00],
[ 7.6194e+31, 1.5564e+28, 1.8484e+31],
[ 1.8370e+25, 1.4603e-19, 2.7517e+12],
[ 7.5338e+28, 3.0313e+32, 6.3828e+28]])
x = torch.rand(5, 3)
print(x)
tensor([[0.6871, 0.4370, 0.3990],
[0.0113, 0.2071, 0.4084],
[0.4247, 0.1985, 0.3557],
[0.9084, 0.2724, 0.9430],
[0.9945, 0.9366, 0.2340]])
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)
print(x)
x = torch.randn_like(x, dtype = torch.float)
print(x)
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[ 0.4788, -0.3258, 0.8718],
[-0.1653, 0.6451, -0.0676],
[-0.4569, 0.0050, -0.2013],
[-0.2161, -1.4332, 0.4315],
[ 1.2185, 1.3106, 1.2528]])
print(x.size())
torch.Size([5, 3])
x.size()[0]
5
y = torch.rand(5, 3)
print(x + y)
tensor([[ 0.6953, 0.4946, 1.3985],
[ 0.0877, 0.6795, 0.4029],
[ 0.1927, 0.3577, 0.7364],
[ 0.4793, -0.4866, 1.0796],
[ 1.8104, 1.4822, 1.7686]])
print(torch.add(x, y))
tensor([[ 0.6953, 0.4946, 1.3985],
[ 0.0877, 0.6795, 0.4029],
[ 0.1927, 0.3577, 0.7364],
[ 0.4793, -0.4866, 1.0796],
[ 1.8104, 1.4822, 1.7686]])
result = torch.empty(5, 3)
torch.add(x, y, out = result)
print(result)
tensor([[ 0.6953, 0.4946, 1.3985],
[ 0.0877, 0.6795, 0.4029],
[ 0.1927, 0.3577, 0.7364],
[ 0.4793, -0.4866, 1.0796],
[ 1.8104, 1.4822, 1.7686]])
y.add_(x)
print(y)
tensor([[ 0.6953, 0.4946, 1.3985],
[ 0.0877, 0.6795, 0.4029],
[ 0.1927, 0.3577, 0.7364],
[ 0.4793, -0.4866, 1.0796],
[ 1.8104, 1.4822, 1.7686]])
print(x[:, 1])
tensor([-0.3258, 0.6451, 0.0050, -1.4332, 1.3106])
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)
print(x.size(), y.size(), z.size())
print(x)
print(y)
print(z)
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
tensor([[ 0.5526, -2.0670, -1.2156, -0.3779],
[-1.8972, 0.6599, -0.8264, 1.0419],
[-0.1237, 0.5389, -0.0888, 0.9985],
[-0.0256, 0.4312, -1.0118, -0.2873]])
tensor([ 0.5526, -2.0670, -1.2156, -0.3779, -1.8972, 0.6599, -0.8264, 1.0419,
-0.1237, 0.5389, -0.0888, 0.9985, -0.0256, 0.4312, -1.0118, -0.2873])
tensor([[ 0.5526, -2.0670, -1.2156, -0.3779, -1.8972, 0.6599, -0.8264, 1.0419],
[-0.1237, 0.5389, -0.0888, 0.9985, -0.0256, 0.4312, -1.0118, -0.2873]])
x = torch.randn(1)
print(x)
print(x.item())
tensor([1.5204])
1.5204463005065918
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.]
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)
if torch.cuda.is_available():
device = torch.device('cuda')
y = torch.ones_like(x, device=device)
x = x.to(device)
z = x+y
print(z.to('cpu',dtype=torch.double))
tensor([2.5204], dtype=torch.float64)
该内容模仿自pytorch官网 :original url











网友评论