updated basic tutorials, better comments, code revision, checked it works with latest pytorch version

This commit is contained in:
Aladdin Persson
2022-12-19 23:39:48 +01:00
parent 3f53d68c4f
commit cd607c395c
14 changed files with 162 additions and 88 deletions

View File

@@ -13,10 +13,11 @@ numpy array and vice-versa.
Programmed by Aladdin Persson
* 2020-06-27: Initial coding
* 2022-12-19: Small revision of code, checked that it works with latest PyTorch version
"""
import torch
import numpy as np
# ================================================================= #
# Initializing Tensor #
@@ -77,8 +78,6 @@ print(
print(f"Converted float64 {tensor.double()}") # Converted to float64
# Array to Tensor conversion and vice-versa
import numpy as np
np_array = np.zeros((5, 5))
tensor = torch.from_numpy(np_array)
np_array_again = (
@@ -112,7 +111,7 @@ t += x # Also inplace: t = t + x is not inplace, bit confusing.
# -- Exponentiation (Element wise if vector or matrices) --
z = x.pow(2) # z = [1, 4, 9]
z = x ** 2 # z = [1, 4, 9]
z = x**2 # z = [1, 4, 9]
# -- Simple Comparison --
@@ -153,7 +152,7 @@ z = (
x1 - x2
) # Shape of z is 5x5: How? The 1x5 vector (x2) is subtracted for each row in the 5x5 (x1)
z = (
x1 ** x2
x1**x2
) # Shape of z is 5x5: How? Broadcasting! Element wise exponentiation for every row
# Other useful tensor operations