mirror of
https://github.com/aladdinpersson/Machine-Learning-Collection.git
synced 2026-04-10 12:33:44 +00:00
updated basic tutorials, better comments, code revision, checked it works with latest pytorch version
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user