from __future__ import print_function
import torch as t
import numpy as np
print('-'*30)
print('t.__version__: ' + t.__version__)
print('-'*30)
x = t.Tensor(5, 3)
print('t.Tensor(5, 3): \n' + str(x))
x = t.Tensor([[1,2],[3,4]])
print('t.Tensor([[1,2],[3,4]]): \n' + str(x))
print('-'*30)
x = t.rand(5, 3)
print('x = t.rand(5, 3): -x\n' + str(x))
print('-'*30)
print('x.size(): ' + str(x.size()))
print('x.size()[0]: ' + str(x.size()[0]) + ', x.size(0): ' + str(x.size(0)))
print('x.size()[1]: ' + str(x.size()[1]) + ', x.size(1): ' + str(x.size(1)))
print('-'*30)
x = t.rand(5,3)
y = t.rand(5,3)
print('x: \n' + str(x))
print('y: \n' + str(y))
print('x+y: \n' + str(x+y))
print('t.add(x, y): \n' + str(t.add(x, y)))
result = t.Tensor(5, 3)
t.add(x, y, out=result)
print('t.add(x, y, out=result): -result: \n' + str(result))
print('-'*30)
print('y: \n' + str(y))
y.add(x)
print('y.add(x): -y\n' + str(y))
y.add_(x)
print('y.add_(x): -y\n' + str(y))
print('-'*30)
print('x: \n' + str(x))
print('x[0, :]: \n' + str(x[0, :]))
print('x[:, 1]: \n' + str(x[:, 1]))
print('-'*30)
a = t.ones(5)
print('a = t.ones(5) : -a\n' + str(a))
print('-'*30)
b = a.numpy()
print('b = a.numpy() : -b\n' + str(b))
print('-'*30)
a = np.ones(5)
b = t.from_numpy(a)
print('a = np.ones(5) : -a\n' + str(a))
print('b = t.from_numpy(a) : -b\n' + str(b))
print('-'*30)
b.add_(1)
print('b.add_(1) : -a\n' + str(a))
print('b.add_(1) : -b\n' + str(b))
print('-'*30)
scalar = b[0]
print('b: \n' + str(b))
print('scalar = b[0] : \n' + str(scalar))
print('scalar.size() : ' + str(scalar.size()))
print('scalar.item() : ' + str(scalar.item()))
print('-'*30)
tensor = t.tensor([2])
print('tensor: ' + str(tensor))
print('scalar: ' + str(scalar))
print('tensor.size(): ' + str(tensor.size()))
print('scalar.size(): ' + str(scalar.size()))
print('tensor.item(): ' + str(tensor.item()))
print('scalar.item(): ' + str(scalar.item()))
print('-'*30)
tensor = t.tensor([3, 4])
scalar = t.tensor(3)
print('scalar: ' + str(scalar))
old_tensor = tensor
new_tensor = old_tensor.clone()
new_tensor[0] = 1111
print('old_tensor: ' + str(old_tensor))
print('new_tensor = old_tensor.clone(): -new_tensor[0] = 1111: -\nnew_tensor: ' + str(new_tensor))
print('-'*30)
new_tensor = old_tensor.detach()
new_tensor[0] = 111
print('old_tensor: ' + str(old_tensor))
print('new_tensor = old_tensor.datach(): -new_tensor[0] = 1111: -\nnew_tensor: ' + str(new_tensor))
print('-'*30)
device = t.device('cuda:0' if t.cuda.is_available() else "cpu")
x = x.to(device)
y = y.to(x.device)
z = x+y
print('x = x.to(device) : -x\n' + str(x))
print('y = y.to(x.device) : -y\n' + str(y))
print('z = x+y : -z\n' + str(z))
|