operations unit tests

This commit is contained in:
lucasdelimanogueira 2024-05-07 11:52:56 -03:00
parent ef3868066e
commit 2c2569d482
18 changed files with 239 additions and 6866 deletions

View file

@ -591,4 +591,4 @@ class Tensor:
self.grad = None
self.grad_fn = None
return self
return self

View file

@ -1,98 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2, 0, 2, 2] [2, 3, 3]\n",
"Tensor shape: [2, 3, 3]\n",
"Tensor created successfully\n",
"Tensor information:\n",
"Number of dimensions: 3\n",
"Shape: [2, 3, 3]\n",
"Data:\n",
"[0.00, 1.00, 2.00, 0.00, 2.00, 2.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00]\n"
]
}
],
"source": [
"from tensor import Tensor\n",
"\n",
"data = [[0, 1, 2], [0, 2, 2], [0, 1, 2]]\n",
"tensor = Tensor(data)\n",
"print(\"Tensor shape:\", tensor.shape)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tensor created successfully\n",
"Tensor information:\n",
"Number of dimensions: 2\n",
"Shape: [3, 3]\n",
"Data:\n",
"[0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90]\n"
]
},
{
"data": {
"text/plain": [
"0.10000000149011612 0.20000000298023224 0.30000001192092896 \n",
"0.4000000059604645 0.5 0.6000000238418579 \n",
"0.699999988079071 0.800000011920929 0.8999999761581421"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"data = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]\n",
"shape = [3, 3]\n",
"tensor = Tensor(data, shape)\n",
"tensor\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

1
norch/utils/__init__.py Normal file
View file

@ -0,0 +1 @@
from .utils import *

View file

@ -1,5 +1,5 @@
import random
import numpy as np
import torch
def generate_random_list(shape):
@ -14,4 +14,27 @@ def generate_random_list(shape):
return [random.uniform(-1, 1) for _ in range(shape[0])]
else:
return [generate_random_list(inner_shape) for _ in range(shape[0])]
def to_torch(custom_tensor):
shape = custom_tensor.shape
pytorch_tensor = torch.zeros(shape)
def _iterate_indices(shape):
if len(shape) == 0:
yield ()
else:
for index in range(shape[0]):
for sub_indices in _iterate_indices(shape[1:]):
yield (index,) + sub_indices
# Iterate over all elements using the custom tensor's __getitem__ method
for indices in _iterate_indices(shape):
value = custom_tensor[indices]
pytorch_tensor[tuple(indices)] = value
return pytorch_tensor
def compare_torch(tensor1, tensor2, epsilon=1e-5):
diff = torch.abs(tensor1 - tensor2)
return torch.all(diff < epsilon)

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

26
test.py
View file

@ -20,8 +20,11 @@ if __name__ == "__main__":
import random
import numpy as np
import psutil
from norch.utils import utils
"""a = norch.Tensor([
a = norch.Tensor([
[[1.234, 2.123], [3.635, 4.456], [5.678, 6.789]],
[[7.890, 8.901], [9.012, 1.234], [2.345, 3.456]],
[[4.567, 5.678], [6.789, 7.890], [8.901, 9.012]],
@ -29,6 +32,27 @@ if __name__ == "__main__":
[[7.890, 8.901], [9.012, 1.234], [2.345, 3.456]]
], requires_grad=True)
print(a)
print(a[0, 2,0])
a = utils.to_torch(a)
import torch
b = torch.tensor([
[[1.234, 2.123], [3.635, 4.456], [5.678, 6.789]],
[[7.890, 8.901], [9.012, 1.234], [2.345, 3.456]],
[[4.567, 5.678], [6.789, 7.890], [8.901, 9.012]],
[[1.234, 2.345], [3.456, 4.567], [5.678, 6.789]],
[[7.890, 8.901], [9.012, 1.234], [2.345, 3.456]]
])
print(utils.torch_compare(a, b))
exit()
"""
b = norch.Tensor([[
[1.234, 2.123, 1.5],
[5.678, 6.789, 1.293],

1
tests/__init__.py Normal file
View file

@ -0,0 +1 @@
from .test_operations import *

186
tests/test_operations.py Normal file
View file

@ -0,0 +1,186 @@
import unittest
import norch
from norch import utils
import torch
class TestTensorOperations(unittest.TestCase):
def test_creation_and_conversion(self):
"""
Test creation and convertion of norch tensor to pytorch
"""
norch_tensor = norch.Tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
torch_tensor = utils.to_torch(norch_tensor)
self.assertTrue(torch.is_tensor(torch_tensor))
def test_addition(self):
"""
Test addition two tensors: tensor1 + tensor2
"""
norch_tensor1 = norch.Tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
norch_tensor2 = norch.Tensor([[[1, 1], [1, 1]], [[1, 1], [1, 1]]])
norch_result = norch_tensor1 + norch_tensor2
torch_result = utils.to_torch(norch_result)
torch_tensor1 = torch.tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
torch_tensor2 = torch.tensor([[[1, 1], [1, 1]], [[1, 1], [1, 1]]])
torch_expected = torch_tensor1 + torch_tensor2
self.assertTrue(utils.compare_torch(torch_result, torch_expected))
def test_subtraction(self):
"""
Test subtraction of two tensors: tensor1 - tensor2
"""
norch_tensor1 = norch.Tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
norch_tensor2 = norch.Tensor([[[1, 1], [1, 1]], [[1, 1], [1, 1]]])
norch_result = norch_tensor1 - norch_tensor2
torch_result = utils.to_torch(norch_result)
torch_tensor1 = torch.tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
torch_tensor2 = torch.tensor([[[1, 1], [1, 1]], [[1, 1], [1, 1]]])
torch_expected = torch_tensor1 - torch_tensor2
self.assertTrue(utils.compare_torch(torch_result, torch_expected))
def test_division_by_scalar(self):
"""
Test division of a tensor by a scalar: tensor / scalar
"""
norch_tensor = norch.Tensor([[[2, 4], [6, -8]], [[10, 12], [14, 16]]])
scalar = 2
norch_result = norch_tensor / scalar
torch_result = utils.to_torch(norch_result)
torch_tensor = torch.tensor([[[2, 4], [6, -8]], [[10, 12], [14, 16]]])
torch_expected = torch_tensor / scalar
self.assertTrue(utils.compare_torch(torch_result, torch_expected))
def test_scalar_division_by_tensor(self):
"""
Test scalar division by a tensor: scalar / tensor
"""
scalar = 10
norch_tensor = norch.Tensor([[[2, 4], [6, -8]], [[10, 12], [14, 16]]])
norch_result = scalar / norch_tensor
torch_result = utils.to_torch(norch_result)
torch_tensor = torch.tensor([[[2, 4], [6, -8]], [[10, 12], [14, 16]]])
torch_expected = scalar / torch_tensor
self.assertTrue(utils.compare_torch(torch_result, torch_expected))
def test_matrix_multiplication(self):
"""
Test matrix multiplication: tensor1 @ tensor2
"""
norch_tensor1 = norch.Tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
norch_tensor2 = norch.Tensor([[[1, 0], [0, 1]], [[-1, 0], [0, -1]]])
norch_result = norch_tensor1 @ norch_tensor2
torch_result = utils.to_torch(norch_result)
torch_tensor1 = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
torch_tensor2 = torch.tensor([[[1, 0], [0, 1]], [[-1, 0], [0, -1]]])
torch_expected = torch_tensor1 @ torch_tensor2
self.assertTrue(utils.compare_torch(torch_result, torch_expected))
def test_elementwise_multiplication_by_scalar(self):
"""
Test elementwise multiplication of a tensor by a scalar: tensor * scalar
"""
norch_tensor = norch.Tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
scalar = 2
norch_result = norch_tensor * scalar
torch_result = utils.to_torch(norch_result)
torch_tensor = torch.tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
torch_expected = torch_tensor * scalar
self.assertTrue(utils.compare_torch(torch_result, torch_expected))
def test_elementwise_multiplication_by_tensor(self):
"""
Test elementwise multiplication of two tensors: tensor1 * tensor2
"""
norch_tensor1 = norch.Tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
norch_tensor2 = norch.Tensor([[[2, 2], [2, 2]], [[2, 2], [2, 2]]])
norch_result = norch_tensor1 * norch_tensor2
torch_result = utils.to_torch(norch_result)
torch_tensor1 = torch.tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
torch_tensor2 = torch.tensor([[[2, 2], [2, 2]], [[2, 2], [2, 2]]])
torch_expected = torch_tensor1 * torch_tensor2
self.assertTrue(utils.compare_torch(torch_result, torch_expected))
def test_reshape(self):
"""
Test reshaping of a tensor: tensor.reshape(shape)
"""
norch_tensor = norch.Tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
new_shape = [2, 4]
norch_result = norch_tensor.reshape(new_shape)
torch_result = utils.to_torch(norch_result)
torch_tensor = torch.tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
torch_expected = torch_tensor.reshape(new_shape)
self.assertTrue(utils.compare_torch(torch_result, torch_expected))
def test_transpose(self):
"""
Test transposition of a tensor: tensor.transpose(dim1, dim2)
"""
norch_tensor = norch.Tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
dim1, dim2 = 0, 2
norch_result = norch_tensor.transpose(dim1, dim2)
torch_result = utils.to_torch(norch_result)
torch_tensor = torch.tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
torch_expected = torch_tensor.transpose(dim1, dim2)
self.assertTrue(utils.compare_torch(torch_result, torch_expected))
def test_logarithm(self):
"""
Test elementwise logarithm of a tensor: tensor.log()
"""
norch_tensor = norch.Tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
norch_result = norch_tensor.log()
torch_result = utils.to_torch(norch_result)
torch_tensor = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
torch_expected = torch.log(torch_tensor)
self.assertTrue(utils.compare_torch(torch_result, torch_expected))
def test_sum(self):
"""
Test summation of a tensor: tensor.sum()
"""
norch_tensor = norch.Tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
norch_result = norch_tensor.sum()
torch_result = utils.to_torch(norch_result)
torch_tensor = torch.tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
torch_expected = torch.sum(torch_tensor)
self.assertTrue(utils.compare_torch(torch_result, torch_expected))
def test_transpose_T(self):
"""
Test transposition of a tensor: tensor.T
"""
norch_tensor = norch.Tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
norch_result = norch_tensor.T
torch_result = utils.to_torch(norch_result)
torch_tensor = torch.tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]])
torch_expected = torch.transpose(torch_tensor, 0, 2)
self.assertTrue(utils.compare_torch(torch_result, torch_expected))
if __name__ == '__main__':
unittest.main()