from __future__ import print_function
import torch
x = torch.empty(5,3)
print(x)
tensor([[9.1834e-41, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 1.0762e-42, 0.0000e+00],
[0.0000e+00, 5.6718e-11, 0.0000e+00]])
x = torch.rand(5,3)
print(x)
tensor([[0.0186, 0.8807, 0.7894],
[0.3810, 0.7405, 0.5440],
[0.9247, 0.7272, 0.1362],
[0.4587, 0.0767, 0.0761],
[0.8206, 0.0725, 0.3316]])
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([[ 1.1492, -0.3027, -0.3889],
[-0.0061, -0.8942, 2.5887],
[-1.8800, -1.1619, -0.6853],
[-0.2841, 0.1407, 0.0970],
[ 0.0358, -0.4802, 0.7781]])
print(x.size())
torch.Size([5, 3])
y = torch.rand(5,3)
print(x)
print(y)
print(x+y)
tensor([[ 1.1492, -0.3027, -0.3889],
[-0.0061, -0.8942, 2.5887],
[-1.8800, -1.1619, -0.6853],
[-0.2841, 0.1407, 0.0970],
[ 0.0358, -0.4802, 0.7781]])
tensor([[0.2793, 0.7743, 0.4154],
[0.5497, 0.4504, 0.7533],
[0.1075, 0.5314, 0.1604],
[0.6892, 0.6163, 0.0620],
[0.7559, 0.3567, 0.2655]])
tensor([[ 1.4285, 0.4716, 0.0266],
[ 0.5437, -0.4439, 3.3420],
[-1.7725, -0.6305, -0.5249],
[ 0.4050, 0.7570, 0.1590],
[ 0.7917, -0.1235, 1.0436]])
print(torch.add(x,y))
tensor([[ 1.4285, 0.4716, 0.0266],
[ 0.5437, -0.4439, 3.3420],
[-1.7725, -0.6305, -0.5249],
[ 0.4050, 0.7570, 0.1590],
[ 0.7917, -0.1235, 1.0436]])
result = torch.empty(5,3)
torch.add(x,y,out = result)
print(result)
tensor([[ 1.4285, 0.4716, 0.0266],
[ 0.5437, -0.4439, 3.3420],
[-1.7725, -0.6305, -0.5249],
[ 0.4050, 0.7570, 0.1590],
[ 0.7917, -0.1235, 1.0436]])
y.add_(x)
print(y)
tensor([[ 1.4285, 0.4716, 0.0266],
[ 0.5437, -0.4439, 3.3420],
[-1.7725, -0.6305, -0.5249],
[ 0.4050, 0.7570, 0.1590],
[ 0.7917, -0.1235, 1.0436]])
print(x)
print(x[:, 1])
tensor([[ 1.1492, -0.3027, -0.3889],
[-0.0061, -0.8942, 2.5887],
[-1.8800, -1.1619, -0.6853],
[-0.2841, 0.1407, 0.0970],
[ 0.0358, -0.4802, 0.7781]])
tensor([-0.3027, -0.8942, -1.1619, 0.1407, -0.4802])
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)
print(x.size(), y.size(), z.size())
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
x = torch.randn(1)
print(x)
print(x.item())
tensor([2.9229])
2.922903060913086
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,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,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)
print(z.to("cpu", torch.double))
tensor([4.3497, 5.0759, 2.6086, 2.4112, 3.5776, 5.4971, 4.8984, 1.5604, 3.7739,
3.1584, 2.7664, 0.9340, 2.6396, 2.8920, 2.3703, 1.5383])
tensor([4.3497, 5.0759, 2.6086, 2.4112, 3.5776, 5.4971, 4.8984, 1.5604, 3.7739,
3.1584, 2.7664, 0.9340, 2.6396, 2.8920, 2.3703, 1.5383],
dtype=torch.float64)