1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| import torch
x = torch.tensor([1, 2, 3]) y = torch.tensor([7, 8, 9])
z1 = torch.empty(3) torch.add(x, y, out=z1) print(z1)
z2 = torch.add(x, y)
z3 = x + y
z4 = x - y print(z4)
z5 = torch.true_divide(x, y)
t = torch.zeros(3) t.add_(x) t += x
z6 = x.pow(2) z6 = x ** 2
z7 = x > 0 z7 = y < 0
x1 = torch.rand((2, 5)) x2 = torch.rand((5, 3)) x3 = torch.mm(x1, x2) x3 = x1.mm(x2)
matrix_exp = torch.rand(5, 5) matrix_exp.matrix_power(3)
z8 = x * y print(z8)
z9 = torch.dot(x, y) print(z9)
batch = 32 n = 10 m = 20 p = 30
tensor1 = torch.rand((batch, n, m)) tensor2 = torch.rand((batch, m, p)) out_mm = torch.bmm(tensor1, tensor2)
x1 = torch.rand((5, 5)) x2 = torch.rand((1, 5))
z = x1 - x2 z = x1 ** x2
x = torch.tensor([[1, 2, 3],[4, 5, 6],[7, 8, 9]]) sum_x = torch.sum(x, dim=0) print(sum_x)
values, indices = torch.max(x, dim = 0) values, indices = torch.min(x, dim = 1)
abs_x = torch.abs(x)
z = torch.argmax(x, dim = 0) z = torch.argmin(x, dim = 1)
mean_x = torch.mean(x.float(), dim = 0) z = torch.eq(x, y)
sorted_x, indices = torch.sort(x, dim = 0, descending = False)
z = torch.clamp(x, min = 0, max = 10)
|