small changes
This commit is contained in:
parent
68bee8b457
commit
e6104e43c9
18 changed files with 468 additions and 287 deletions
18
README.md
18
README.md
|
|
@ -1,2 +1,16 @@
|
|||
# foo
|
||||
Recreating PyTorch from scratch
|
||||
# PyNorch
|
||||
Recreating PyTorch from scratch (C/C++, CUDA and Python, with GPU support and automatic differentiation!)
|
||||
|
||||
# 1 - About
|
||||
**PyNorch** is a deep learning framework constructed using C/C++, CUDA and Python. This is a personal project with educational purpose only! `Norch` means **NOT** PyTorch, and we have **NO** claims to rivaling the already established PyTorch. The main objective of **PyNorch** was to give a brief understanding of how a deep learning framework works internally. It implements the Tensor object, GPU support and an automatic differentiation system.
|
||||
|
||||
# 2 - Installation
|
||||
```css
|
||||
$ sudo apt install nvidia-cuda-toolkit
|
||||
$ git clone https://github.com/lucasdelimanogueira/PyNorch.git
|
||||
$ cd build
|
||||
$ make
|
||||
$ cd ..
|
||||
```
|
||||
|
||||
# 3 - Get started
|
||||
311
examples/getting_started.ipynb
Normal file
311
examples/getting_started.ipynb
Normal file
|
|
@ -0,0 +1,311 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Tensor operations"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/home/lln/Documentos/recreate_pytorch/PyNorch\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%cd .."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 1 - Basic operations"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"t1 =\n",
|
||||
"tensor([[1.0, 2.0,],\n",
|
||||
"[3.0, 4.0,]], device=\"cpu\", requires_grad=False)\n",
|
||||
"t2 =\n",
|
||||
"tensor([[4.0, 3.0,],\n",
|
||||
"[2.0, 1.0,]], device=\"cpu\", requires_grad=False)\n",
|
||||
"\n",
|
||||
"Some basic operations\n",
|
||||
"x1 + x2: \n",
|
||||
"tensor([[5.0, 5.0,],\n",
|
||||
"[5.0, 5.0,]], device=\"cpu\", requires_grad=False)\n",
|
||||
"x1 - x2: \n",
|
||||
"tensor([[-3.0, -1.0,],\n",
|
||||
"[1.0, 3.0,]], device=\"cpu\", requires_grad=False)\n",
|
||||
"x1 * x2: \n",
|
||||
"tensor([[4.0, 6.0,],\n",
|
||||
"[6.0, 4.0,]], device=\"cpu\", requires_grad=False)\n",
|
||||
"x1 / x2: \n",
|
||||
"tensor([[0.25, 0.6666666865348816,],\n",
|
||||
"[1.5, 4.0,]], device=\"cpu\", requires_grad=False)\n",
|
||||
"x1 / 10: \n",
|
||||
"tensor([[0.10000000149011612, 0.20000000298023224,],\n",
|
||||
"[0.30000001192092896, 0.4000000059604645,]], device=\"cpu\", requires_grad=False)\n",
|
||||
"x1 @ x2: \n",
|
||||
"tensor([[8.0, 5.0,],\n",
|
||||
"[20.0, 13.0,]], device=\"cpu\", requires_grad=False)\n",
|
||||
"x1 ** 2: \n",
|
||||
"tensor([[1.0, 4.0,],\n",
|
||||
"[9.0, 16.0,]], device=\"cpu\", requires_grad=False)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import norch\n",
|
||||
"\n",
|
||||
"x1 = norch.Tensor([[1, 2], \n",
|
||||
" [3, 4]])\n",
|
||||
"x2 = norch.Tensor([[4, 3], \n",
|
||||
" [2, 1]])\n",
|
||||
"\n",
|
||||
"print(f\"t1 =\\n{x1}\")\n",
|
||||
"print(f\"t2 =\\n{x2}\")\n",
|
||||
"\n",
|
||||
"print(\"\\nSome basic operations\")\n",
|
||||
"print(f\"x1 + x2: \\n{x1 + x2}\")\n",
|
||||
"print(f\"x1 - x2: \\n{x1 - x2}\")\n",
|
||||
"print(f\"x1 * x2: \\n{x1 * x2}\")\n",
|
||||
"print(f\"x1 / x2: \\n{x1 / x2}\")\n",
|
||||
"print(f\"x1 / 10: \\n{x1 / 10}\")\n",
|
||||
"print(f\"x1 @ x2: \\n{x1 @ x2}\")\n",
|
||||
"print(f\"x1 ** 2: \\n{x1 ** 2}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"x1 reshape: \n",
|
||||
"tensor([[1.0, 2.0, 3.0, 4.0,]], device=\"cpu\", requires_grad=False)\n",
|
||||
"x1 transpose axes: \n",
|
||||
"tensor([[1.0, 3.0,],\n",
|
||||
"[2.0, 4.0,]], device=\"cpu\", requires_grad=False)\n",
|
||||
"x1 transpose: \n",
|
||||
"tensor([[1.0, 3.0,],\n",
|
||||
"[2.0, 4.0,]], device=\"cpu\", requires_grad=False)\n",
|
||||
"x1 zeros_like: \n",
|
||||
"tensor([[0.0, 0.0,],\n",
|
||||
"[0.0, 0.0,]], device=\"cpu\", requires_grad=None)\n",
|
||||
"x1 ones_like: \n",
|
||||
"tensor([[1.0, 1.0,],\n",
|
||||
"[1.0, 1.0,]], device=\"cpu\", requires_grad=None)\n",
|
||||
"sin(x1): \n",
|
||||
"tensor([[0.8414709568023682, 0.9092974066734314,],\n",
|
||||
"[0.14112000167369843, -0.756802499294281,]], device=\"cpu\", requires_grad=False)\n",
|
||||
"cos(x1): \n",
|
||||
"tensor([[0.5403022766113281, -0.416146844625473,],\n",
|
||||
"[-0.9899924993515015, -0.6536436080932617,]], device=\"cpu\", requires_grad=False)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(f\"x1 reshape: \\n{x1.reshape([1, 4])}\")\n",
|
||||
"print(f\"x1 transpose axes: \\n{x1.transpose(1, 0)}\")\n",
|
||||
"print(f\"x1 transpose: \\n{x1.T}\")\n",
|
||||
"\n",
|
||||
"print(f\"x1 zeros_like: \\n{x1.zeros_like()}\")\n",
|
||||
"print(f\"x1 ones_like: \\n{x1.ones_like()}\")\n",
|
||||
"\n",
|
||||
"print(f\"sin(x1): \\n{x1.sin()}\")\n",
|
||||
"print(f\"cos(x1): \\n{x1.cos()}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 2 - Autograd"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"a.shape: [5, 3, 2]\n",
|
||||
"b.shape: [5, 4, 3]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"gradient a: tensor([[\n",
|
||||
"[18.437000274658203, 18.437000274658203,],\n",
|
||||
" [22.269001007080078, 22.269001007080078,],\n",
|
||||
" [5.723199844360352, 5.723199844360352,]],\n",
|
||||
"[\n",
|
||||
"[18.437000274658203, 18.437000274658203,],\n",
|
||||
" [22.269001007080078, 22.269001007080078,],\n",
|
||||
" [5.723199844360352, 5.723199844360352,]],\n",
|
||||
"[\n",
|
||||
"[18.437000274658203, 18.437000274658203,],\n",
|
||||
" [22.269001007080078, 22.269001007080078,],\n",
|
||||
" [5.723199844360352, 5.723199844360352,]],\n",
|
||||
"[\n",
|
||||
"[18.437000274658203, 18.437000274658203,],\n",
|
||||
" [22.269001007080078, 22.269001007080078,],\n",
|
||||
" [5.723199844360352, 5.723199844360352,]],\n",
|
||||
"[\n",
|
||||
"[18.437000274658203, 18.437000274658203,],\n",
|
||||
" [22.269001007080078, 22.269001007080078,],\n",
|
||||
" [5.723199844360352, 5.723199844360352,]]], device=\"cpu\", requires_grad=None)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a = norch.Tensor([\n",
|
||||
" [[1.234, 2.123], [3.635, 4.456], [5.678, 6.789]],\n",
|
||||
" [[7.890, 8.901], [9.012, 1.234], [2.345, 3.456]],\n",
|
||||
" [[4.567, 5.678], [6.789, 7.890], [8.901, 9.012]],\n",
|
||||
" [[1.234, 2.345], [3.456, 4.567], [5.678, 6.789]],\n",
|
||||
" [[7.890, 8.901], [9.012, 1.234], [2.345, 3.456]]\n",
|
||||
" ], requires_grad=True)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"b = norch.Tensor([[\n",
|
||||
" [1.234, 2.123, 1.5],\n",
|
||||
" [5.678, 6.789, 1.293],\n",
|
||||
" [3.635, 4.456, 1.0202],\n",
|
||||
" [7.890, 8.901, 1.91],\n",
|
||||
" ],[\n",
|
||||
" [1.234, 2.123, 1.5],\n",
|
||||
" [5.678, 6.789, 1.293],\n",
|
||||
" [3.635, 4.456, 1.0202],\n",
|
||||
" [7.890, 8.901, 1.91],\n",
|
||||
" ],[\n",
|
||||
" [1.234, 2.123, 1.5],\n",
|
||||
" [5.678, 6.789, 1.293],\n",
|
||||
" [3.635, 4.456, 1.0202],\n",
|
||||
" [7.890, 8.901, 1.91],\n",
|
||||
" ],[\n",
|
||||
" [1.234, 2.123, 1.5],\n",
|
||||
" [5.678, 6.789, 1.293],\n",
|
||||
" [3.635, 4.456, 1.0202],\n",
|
||||
" [7.890, 8.901, 1.91],\n",
|
||||
" ],[\n",
|
||||
" [1.234, 2.123, 1.5],\n",
|
||||
" [5.678, 6.789, 1.293],\n",
|
||||
" [3.635, 4.456, 1.0202],\n",
|
||||
" [7.890, 8.901, 1.91],\n",
|
||||
" ]])\n",
|
||||
"\n",
|
||||
"print(f\"a.shape: {a.shape}\")\n",
|
||||
"print(f\"b.shape: {b.shape}\\n\\n\")\n",
|
||||
"\n",
|
||||
"result = b @ a\n",
|
||||
"result = result.sum()\n",
|
||||
"result.backward()\n",
|
||||
"\n",
|
||||
"print(f\"gradient a: {a.grad}\")\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Modules"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import norch.nn as nn\n",
|
||||
"\n",
|
||||
"class MyModule(nn.Module):\n",
|
||||
" def __init__(self):\n",
|
||||
" super(MyModule, self).__init__()\n",
|
||||
"\n",
|
||||
" self.layer1 = nn.Linear(100, 1000)\n",
|
||||
" self.sigmoid1 = nn.Sigmoid()\n",
|
||||
" self.layer2 = nn.Linear(1000, 2)\n",
|
||||
" self.sigmoid2 = nn.Sigmoid()\n",
|
||||
"\n",
|
||||
" def forward(self, x):\n",
|
||||
" out = self.layer1(x)\n",
|
||||
" out = self.sigmoid1(out)\n",
|
||||
" out = self.layer2(out)\n",
|
||||
" out = self.sigmoid2(out)\n",
|
||||
"\n",
|
||||
" return out"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"MyModule(\n",
|
||||
" (layer1): Linear(input_dim=100, output_dim=1000, bias=True)\n",
|
||||
" (sigmoid1): Sigmoid()\n",
|
||||
" (layer2): Linear(input_dim=1000, output_dim=2, bias=True)\n",
|
||||
" (sigmoid2): Sigmoid()\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model = MyModule()\n",
|
||||
"model"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.8.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
|
|
@ -16,7 +16,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/home/lln/Documentos/recreate_pytorch/foo\n"
|
||||
"/home/lln/Documentos/recreate_pytorch/PyNorch\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -26,7 +26,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
|
|
@ -107,6 +107,30 @@
|
|||
" loss_list.append(loss[0])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"MyModel(\n",
|
||||
" (fc1): Linear(input_dim=1, output_dim=10, bias=True)\n",
|
||||
" (sigmoid): Sigmoid()\n",
|
||||
" (fc2): Linear(input_dim=10, output_dim=1, bias=True)\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
|
|
@ -116,7 +140,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
|
|
@ -144,13 +168,6 @@
|
|||
"plt.xticks(epochs_list)\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
apt install nvidia-cuda-toolkit
|
||||
cd build
|
||||
make
|
||||
make
|
||||
cd ..
|
||||
Binary file not shown.
|
|
@ -18,7 +18,6 @@ void sub_tensor_cpu(Tensor* tensor1, Tensor* tensor2, float* result_data) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
void elementwise_mul_tensor_cpu(Tensor* tensor1, Tensor* tensor2, float* result_data) {
|
||||
|
||||
for (int i = 0; i < tensor1->size; i++) {
|
||||
|
|
|
|||
30
norch/norch.egg-info/PKG-INFO
Normal file
30
norch/norch.egg-info/PKG-INFO
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
Metadata-Version: 2.1
|
||||
Name: norch
|
||||
Version: 0.0.1
|
||||
Summary: A deep learning framework
|
||||
Home-page: https://github.com/lucasdelimanogueira/PyNorch
|
||||
Author: Lucas de Lima
|
||||
Author-email: nogueiralucasdelima@gmail.com
|
||||
Project-URL: Bug Tracker, https://github.com/lucasdelimanogueira/PyNorch/issues
|
||||
Project-URL: Repository, https://github.com/lucasdelimanogueira/PyNorch
|
||||
Classifier: Programming Language :: Python :: 3
|
||||
Classifier: Operating System :: OS Independent
|
||||
Requires-Python: >=3.6
|
||||
Description-Content-Type: text/markdown
|
||||
|
||||
# PyNorch
|
||||
Recreating PyTorch from scratch (C/C++, CUDA and Python, with GPU support and automatic differentiation!)
|
||||
|
||||
# 1 - About
|
||||
**PyNorch** is a deep learning framework constructed using C/C++, CUDA and Python. This is a personal project with educational purpose only! `Norch` means **NOT** PyTorch, and we have **NO** claims to rivaling the already established PyTorch. The main objective of **PyNorch** was to give a brief understanding of how a deep learning framework works internally. It implements the Tensor object, GPU support and an automatic differentiation system.
|
||||
|
||||
# 2 - Installation
|
||||
```css
|
||||
$ sudo apt install nvidia-cuda-toolkit
|
||||
$ git clone https://github.com/lucasdelimanogueira/PyNorch.git
|
||||
$ cd build
|
||||
$ make
|
||||
$ cd ..
|
||||
```
|
||||
|
||||
# 3 - Get started
|
||||
24
norch/norch.egg-info/SOURCES.txt
Normal file
24
norch/norch.egg-info/SOURCES.txt
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
README.md
|
||||
install.sh
|
||||
setup.py
|
||||
norch/nn/__init__.py
|
||||
norch/nn/activation.py
|
||||
norch/nn/loss.py
|
||||
norch/nn/module.py
|
||||
norch/nn/parameter.py
|
||||
norch/nn/modules/__init__.py
|
||||
norch/nn/modules/linear.py
|
||||
norch/norch.egg-info/PKG-INFO
|
||||
norch/norch.egg-info/SOURCES.txt
|
||||
norch/norch.egg-info/dependency_links.txt
|
||||
norch/norch.egg-info/top_level.txt
|
||||
norch/optim/__init__.py
|
||||
norch/optim/optimizer.py
|
||||
norch/optim/optimizers/__init__.py
|
||||
norch/optim/optimizers/sgd.py
|
||||
norch/utils/__init__.py
|
||||
norch/utils/utils.py
|
||||
norch/utils/utils_unittests.py
|
||||
tests/test_autograd.py
|
||||
tests/test_nn.py
|
||||
tests/test_operations.py
|
||||
1
norch/norch.egg-info/dependency_links.txt
Normal file
1
norch/norch.egg-info/dependency_links.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
|
||||
3
norch/norch.egg-info/top_level.txt
Normal file
3
norch/norch.egg-info/top_level.txt
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
nn
|
||||
optim
|
||||
utils
|
||||
|
|
@ -57,7 +57,6 @@ class Tensor:
|
|||
self.grad = None
|
||||
self.grad_fn = None
|
||||
|
||||
|
||||
def flatten(self, nested_list):
|
||||
def flatten_recursively(nested_list):
|
||||
flat_data = []
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -1,6 +1,4 @@
|
|||
import random
|
||||
import torch
|
||||
|
||||
|
||||
def generate_random_list(shape):
|
||||
"""
|
||||
|
|
@ -13,28 +11,4 @@ def generate_random_list(shape):
|
|||
if len(inner_shape) == 0:
|
||||
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)
|
||||
return [generate_random_list(inner_shape) for _ in range(shape[0])]
|
||||
24
norch/utils/utils_unittests.py
Normal file
24
norch/utils/utils_unittests.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import torch
|
||||
|
||||
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)
|
||||
27
setup.py
Normal file
27
setup.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import setuptools
|
||||
|
||||
with open("README.md", "r", encoding = "utf-8") as fh:
|
||||
long_description = fh.read()
|
||||
|
||||
setuptools.setup(
|
||||
name = "norch",
|
||||
version = "0.0.1",
|
||||
scripts=['install.sh'],
|
||||
author = "Lucas de Lima",
|
||||
author_email = "nogueiralucasdelima@gmail.com",
|
||||
description = "A deep learning framework",
|
||||
long_description = long_description,
|
||||
long_description_content_type = "text/markdown",
|
||||
url = "https://github.com/lucasdelimanogueira/PyNorch",
|
||||
project_urls = {
|
||||
"Bug Tracker": "https://github.com/lucasdelimanogueira/PyNorch/issues",
|
||||
"Repository": "https://github.com/lucasdelimanogueira/PyNorch"
|
||||
},
|
||||
classifiers = [
|
||||
"Programming Language :: Python :: 3",
|
||||
"Operating System :: OS Independent",
|
||||
],
|
||||
package_dir = {"": "norch"},
|
||||
packages = setuptools.find_packages(where="norch"),
|
||||
python_requires = ">=3.6"
|
||||
)
|
||||
240
test.py
240
test.py
|
|
@ -1,240 +0,0 @@
|
|||
|
||||
def matrix_sum(matrix1, matrix2):
|
||||
# Check if the matrices can be multiplied
|
||||
if len(matrix1[0]) != len(matrix2):
|
||||
raise ValueError("Matrices cannot be multiplied. Inner dimensions must match.")
|
||||
|
||||
# Initialize the result matrix with zeros
|
||||
result = [[0 for _ in range(len(matrix2[0]))] for _ in range(len(matrix1))]
|
||||
|
||||
# Perform matrix multiplication
|
||||
for i in range(len(matrix1)):
|
||||
for j in range(len(matrix2[0])):
|
||||
result[i][j] += matrix1[i][i] + matrix2[i][j]
|
||||
|
||||
return result
|
||||
|
||||
if __name__ == "__main__":
|
||||
import norch
|
||||
import time
|
||||
import random
|
||||
import numpy as np
|
||||
import psutil
|
||||
from norch.utils import utils
|
||||
|
||||
"""
|
||||
|
||||
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]],
|
||||
[[1.234, 2.345], [3.456, 4.567], [5.678, 6.789]],
|
||||
[[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],
|
||||
[3.635, 4.456, 1.0202],
|
||||
[7.890, 8.901, 1.91],
|
||||
],[
|
||||
[1.234, 2.123, 1.5],
|
||||
[5.678, 6.789, 1.293],
|
||||
[3.635, 4.456, 1.0202],
|
||||
[7.890, 8.901, 1.91],
|
||||
],[
|
||||
[1.234, 2.123, 1.5],
|
||||
[5.678, 6.789, 1.293],
|
||||
[3.635, 4.456, 1.0202],
|
||||
[7.890, 8.901, 1.91],
|
||||
],[
|
||||
[1.234, 2.123, 1.5],
|
||||
[5.678, 6.789, 1.293],
|
||||
[3.635, 4.456, 1.0202],
|
||||
[7.890, 8.901, 1.91],
|
||||
],[
|
||||
[1.234, 2.123, 1.5],
|
||||
[5.678, 6.789, 1.293],
|
||||
[3.635, 4.456, 1.0202],
|
||||
[7.890, 8.901, 1.91],
|
||||
]])
|
||||
|
||||
#print(a.T)
|
||||
#print(a.T.shape)
|
||||
#[5, 3, 2] [4, 3] [5, 4, 2]
|
||||
#print(a.shape, b.shape)
|
||||
#b = norch.Tensor([
|
||||
# [1.234, 2.123, 1.5]])
|
||||
result = b @ a
|
||||
result = result.sum()
|
||||
result.backward()
|
||||
|
||||
print(a.grad)"""
|
||||
|
||||
"""import norch.nn as nn
|
||||
|
||||
cpu_percent = psutil.cpu_percent(interval=1)
|
||||
print(f"CPU Usage: {cpu_percent}%")
|
||||
memory_usage = psutil.virtual_memory()
|
||||
print(f"Memory Usage: {memory_usage.percent}%")
|
||||
|
||||
|
||||
class MeuModulo(nn.Module):
|
||||
def __init__(self):
|
||||
super(MeuModulo, self).__init__()
|
||||
|
||||
self.layer1 = nn.Linear(100, 1000)
|
||||
self.sigmoid1 = nn.Sigmoid()
|
||||
self.layer2 = nn.Linear(1000, 2)
|
||||
self.sigmoid2 = nn.Sigmoid()
|
||||
|
||||
def forward(self, x):
|
||||
out = self.layer1(x)
|
||||
out = self.sigmoid1(out)
|
||||
out = self.layer2(out)
|
||||
out = self.sigmoid2(out)
|
||||
|
||||
return out
|
||||
|
||||
modelo = MeuModulo()
|
||||
input_list = [[0.5 for _ in range(100)]]
|
||||
input = norch.Tensor(input_list).T
|
||||
criterion = nn.MSELoss()
|
||||
optimizer = norch.optim.SGD(modelo.parameters(), lr=0.1)
|
||||
|
||||
target_list = [[random.random() for _ in range(2)]]
|
||||
target = norch.Tensor(target_list).T
|
||||
|
||||
print(modelo)
|
||||
|
||||
ini = time.time()
|
||||
for epoch in range(30):
|
||||
output = modelo(input)
|
||||
loss = criterion(output, target)
|
||||
optimizer.zero_grad()
|
||||
|
||||
loss.backward()
|
||||
optimizer.step()
|
||||
#print(loss)
|
||||
|
||||
fim = time.time()
|
||||
print(fim - ini)"""
|
||||
|
||||
|
||||
#### testar transpose axes!!!! make it contiguous
|
||||
|
||||
"""tensor1 = norch.Tensor([[[1, 2], [3, 4], [5, 6]],
|
||||
[[7, 8], [9, 10], [11, 12]],
|
||||
[[13, 14], [15, 16], [17, 18]],
|
||||
[[19, 20], [21, 22], [23, 24]],
|
||||
[[25, 26], [27, 28], [29, 0.030]]], requires_grad=True)
|
||||
|
||||
#op = nn.Sigmoid()
|
||||
tensor2 =
|
||||
result = tensor1.sum()
|
||||
|
||||
result.backward()
|
||||
print(tensor1.grad)
|
||||
exit()"""
|
||||
|
||||
# Reshape tensor1 to 2x3x5
|
||||
"""tensor1 = norch.Tensor([[[1, 2], [3, 4], [5, 6]],
|
||||
[[7, 8], [9, 10], [11, 12]],
|
||||
[[13, 14], [15, 16], [17, 18]],
|
||||
[[19, 20], [21, 22], [23, 24]],
|
||||
[[25, 26], [27, 28], [29, 0.030]]], requires_grad=True)
|
||||
|
||||
# Create a 5x4 tensor
|
||||
tensor2 = norch.Tensor([[1, 2, 3],
|
||||
[5, 6, 7],
|
||||
[9, 10, 11],
|
||||
[13, 14, 15],
|
||||
[17, 18, 19]])
|
||||
|
||||
|
||||
# Multiply reshaped_tensor by tensor2
|
||||
result = tensor2 @ tensor1
|
||||
|
||||
result = result.sum()
|
||||
result.backward()
|
||||
print(tensor1.grad)"""
|
||||
|
||||
#print(a.shape, b.shape, result.shape)
|
||||
#c = result.sum()
|
||||
#c.backward()
|
||||
#print(a.grad)
|
||||
#print(a.transpose(2,1))
|
||||
|
||||
#a = norch.Tensor([[1, 2, 3], [1, 2, 3], [1, 2, 3]])#.to("cuda")
|
||||
#b = Tensor([[1, 2, 3], [1, 2, 3], [1, 2, 3]])#.to("cuda")
|
||||
#c = Tensor([[1, 2, 3], [1, 2, 3], [1, 2, 3]])#.to("cuda")
|
||||
|
||||
#d = b-c
|
||||
|
||||
a = norch.Tensor([[1, 2.1], [3, -4], [5, 6], [7, 8]], requires_grad=True)#.to("cuda")
|
||||
b = norch.Tensor([[1, 2.1], [3, -4], [5, 6], [7, 8]], requires_grad=True)
|
||||
t = b.reshape([2,4])
|
||||
c = (t @ a)
|
||||
d = c.sum()
|
||||
d.backward()
|
||||
|
||||
print(a.grad)
|
||||
"""#print(a)
|
||||
N = 10
|
||||
a = norch.Tensor([[1 for _ in range(N)] for _ in range(N)])
|
||||
#b = norch.Tensor([[random.uniform(0, 1) for _ in range(N)] for _ in range(N)])
|
||||
#b = Tensor([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
|
||||
#a = Tensor([[[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]]])
|
||||
#b = Tensor([[[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]]])
|
||||
ini = time.time()
|
||||
c = a.sum()
|
||||
|
||||
print("\n#####2######")
|
||||
|
||||
fim = time.time()
|
||||
|
||||
print(fim-ini)
|
||||
print(c)
|
||||
print("\n\n")
|
||||
|
||||
"""
|
||||
"""
|
||||
a = [[random.uniform(0, 1) for _ in range(N)] for _ in range(N)]
|
||||
b = [[random.uniform(0, 1) for _ in range(N)] for _ in range(N)]
|
||||
ini = time.time()
|
||||
result_matrix = matrix_sum(a, b)
|
||||
fim = time.time()
|
||||
print(fim-ini)
|
||||
|
||||
|
||||
print("\n\n")
|
||||
|
||||
ini = time.time()
|
||||
a = np.random.rand(N, N)
|
||||
b = np.random.rand(N, N)
|
||||
result_matrix = a + b
|
||||
fim = time.time()
|
||||
print(fim-ini)
|
||||
|
||||
"""
|
||||
4
test.sh
4
test.sh
|
|
@ -1,4 +0,0 @@
|
|||
cd build
|
||||
make
|
||||
cd ..
|
||||
python3 test.py
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
import unittest
|
||||
import norch
|
||||
from norch import utils
|
||||
from norch.utils import utils_unittests as utils
|
||||
import torch
|
||||
import os
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue