Advertisement
Eternoseeker

Basic functions on tensors

Aug 10th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | Source Code | 0 0
  1. import torch
  2. import numpy
  3. singleton = torch.tensor(8)
  4. print(singleton.item())
  5. #random_image_size_tensor = torch.rand(size=(224, 224, 3))
  6. #print(random_image_size_tensor.shape)
  7. #print(random_image_size_tensor.ndim)
  8. # zero = torch.zeros(size=(5, 5))
  9. # ones = torch.ones(size=(5, 5))
  10. # similarZeros = torch.zeros_like(zero)
  11. # toTen = torch.range(0, 10)
  12. # print(toTen)
  13. # toTen2 = torch.arange(start=0, end=100, step=3)
  14. # print(toTen2)
  15. tensor = torch.tensor([[1, 2, 3], [6, 7, 8]], dtype=torch.int32)
  16. print(f"Shape of the tensor is: {tensor.shape}")
  17. print(f"Dimension of the tensor is: {tensor.ndim}")
  18. print(f"Datatype of the tensor is: {tensor.dtype}")
  19. datatypecustom = torch.tensor([7, 8, 9], dtype=torch.float64)
  20. print(f"Custom datatype set to float64: {datatypecustom.dtype}")
  21. sample = torch.arange(start=0, end=10, step=2)
  22. print(sample)
  23. # sample = sample + 1
  24. # sample = sample * sample
  25. # Matrix Multiplication
  26. #new = sample @ sample
  27. #sample = torch.matmul(sample, sample)
  28. #print(sample)
  29. #for i in range(len(sample)):
  30. #    sample = torch.mul(sample, i)
  31. #Transpose Matrix
  32. matrix = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  33. print(matrix)
  34. print(matrix.T)
  35. matrix = torch.matmul(matrix, matrix.T)
  36. print(matrix)
  37. mat = torch.randn(3,3)
  38. det = torch.linalg.det(mat)
  39. print(det)
  40. print(mat)
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement