from __future__ import print_function
import torch
x = torch.empty(5,3)
print(x)
x = torch.rand(5,3)
print(x)
x = torch.randn(5)
print(x)
x = torch.zeros(5,3,dtype=torch.long)
print(x)
x = x.new_ones(5,3,dtype=torch.double)
print(x)
x = torch.tensor([5.5,3])
print(x)
x = x.new_ones(5,3,dtype=torch.double)
x = torch.randn_like(x,dtype=torch.float)
print(x.size())
y = torch.rand(5,3)
print(x+y)
print(torch.add(x,y))
result = torch.empty(5,3)
torch.add(x,y,out=result)
print(result)
y.add_(x)
print(y)
print(y[:,1])
x = torch.randn(4,4)
y = x.view(16)
z = x.view(-1,8)
print(x.size(),y.size(),z.size())
x = torch.randn(1)
print(x)
print(x.type())
print(x.item())
a = torch.ones(5)
print(a)
b = a.numpy()
print(b)
a.add_(1)
print(a)
print(b)
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a,1,out=a)
print(a,b)
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))
import torch
x = torch.ones(2,2,requires_grad=True)
print(x)
y = x + 2
print(y.grad_fn)
z = y*y*3
out = z.mean()
print(z,out)
out.backward()
print(x.grad)
x = torch.randn(3,requires_grad=True)
y = x*2
while y.data.norm() < 1000:
y = y * 2
v = torch.tensor([0.1,1.0,0.001],dtype=torch.float32)
y.backward(v)
print(x.grad)
print(x.requires_grad)
print((x**2).requires_grad)
with torch.no_grad():
print((x ** 2).requires_grad)