import torch
x = torch.tensor([1, 2, 3])
y = torch.tensor([9, 8, 7])
#加法
z1 = torch.empty(3)
torch.add(x, y, out = z1)
z2 = torch.add(x, y)
z = x + y
print(z)
#减法
z = x - y
print(z)
#除法
z = torch.true_divide(x, y)
#inplace operations
t = torch.zeros(3)
t += x
#平方
z = x.pow(2)
z = x ** 2
z = x > 0
print(z)
z = x < 0
print(z)
#矩阵乘法matrix multiplitation
#矩阵乘法
x1 = torch.rand((2, 5))
x2 = torch.rand((5, 3))
matrix_exp = torch.rand(5, 5)
print(matrix_exp)
print(matrix_exp.matrix_power(3))
z = x * y
print(z)
#点乘
z = torch.dot(x, y)
print(z)
#三维张量相乘
batch = 32
n = 10
m = 20
p = 30
tensor1 = torch.rand((batch, n, m))
tensor2 = torch.rand((batch, m, p))
out_bmm = torch.bmm(tensor1, tensor2) #b, n, p
print(out_bmm)
#广播的例子
x1 = torch.rand((5, 5))
x2 = torch.rand((1, 5))
print(x1)
print(x2)
z = x1 + x2
print(z)
z = x1 ** x2
print(z)
#other useful tensor operations
sum_x = torch.sum(x, dim = 0)
print(sum_x)
values, indices = torch.max(x, dim=0)
print(values)
print(indices)
values, indices = torch.min(x, dim=0)
print(values)
print(indices)
abs_x = torch.abs(x)
print(abs_x)
z = torch.argmax(x, dim = 0)
print(z)
z = torch.argmin(x, dim = 0)
print(z)
mean_x = torch.mean(x.float(), dim = 0)
print(mean_x)
z = torch.eq(x, y)
print(z)
torch.sort(y, dim = 0, descending = False)
sorted_y, indices = torch.sort(y, dim = 0, descending = False)
print(sorted_y)
print(indices)