diff --git a/01-tensors.py b/01-tensors.py index 46455bf..0fef88d 100644 --- a/01-tensors.py +++ b/01-tensors.py @@ -42,3 +42,36 @@ tensor_attr = torch.rand(3,4).to('xpu') tensor = torch.ones(4,4).to('xpu') print(f"First row: {tensor[0]}") +print(f"First column: {tensor[:,0]}") +print(f"Last column: {tensor[...,-1]}") +print(tensor) + +# joining tensors using torch.cat +t1 = torch.cat([tensor, tensor, tensor], dim=1).to('xpu') +print(t1) + + + +# different ways to do matrix multiplication (y1, y2, y3 will have same values) +y1 = tensor @ tensor.T +y2 = tensor.matmul(tensor.T) + +y3 = torch.rand_like(y1) # create a new tensor with same shape as y1 +torch.matmul(tensor, tensor.T, out=y3) + + +# comput the element-wise product (z1, z2, z3 will have same values) +z1 = tensor * tensor +z2 = tensor.mul(tensor) + +z3 = torch.rand_like(z1) +torch.mul(tensor, tensor, out=z3) + +# convert a one-item tensor into a number with `item()` +agg = tensor.sum() +agg_item = agg.item() +print(agg, type(agg)) +print(agg_item, type(agg_item)) + +# in-place operations, will assign the result into the operand +tensor.add_(5) # will assign the result to tensor (ie. override the original tensor) \ No newline at end of file