diff --git a/norch/__pycache__/tensor.cpython-38.pyc b/norch/__pycache__/tensor.cpython-38.pyc index 77de118..d22bb34 100644 Binary files a/norch/__pycache__/tensor.cpython-38.pyc and b/norch/__pycache__/tensor.cpython-38.pyc differ diff --git a/norch/nn/__init__.py b/norch/nn/__init__.py index fc6fa1a..12c8a92 100644 --- a/norch/nn/__init__.py +++ b/norch/nn/__init__.py @@ -1,2 +1,2 @@ from .modules import * -from .activations import * \ No newline at end of file +from .activation import * \ No newline at end of file diff --git a/norch/nn/__pycache__/__init__.cpython-38.pyc b/norch/nn/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 0000000..8726808 Binary files /dev/null and b/norch/nn/__pycache__/__init__.cpython-38.pyc differ diff --git a/norch/nn/__pycache__/activation.cpython-38.pyc b/norch/nn/__pycache__/activation.cpython-38.pyc new file mode 100644 index 0000000..f6d4dfe Binary files /dev/null and b/norch/nn/__pycache__/activation.cpython-38.pyc differ diff --git a/norch/nn/__pycache__/module.cpython-38.pyc b/norch/nn/__pycache__/module.cpython-38.pyc new file mode 100644 index 0000000..32c92f5 Binary files /dev/null and b/norch/nn/__pycache__/module.cpython-38.pyc differ diff --git a/norch/nn/__pycache__/parameter.cpython-38.pyc b/norch/nn/__pycache__/parameter.cpython-38.pyc new file mode 100644 index 0000000..7c686b8 Binary files /dev/null and b/norch/nn/__pycache__/parameter.cpython-38.pyc differ diff --git a/norch/nn/activation.py b/norch/nn/activation.py index 1ab272f..21cf891 100644 --- a/norch/nn/activation.py +++ b/norch/nn/activation.py @@ -1,4 +1,4 @@ -from module import Module +from .module import Module import math class Activation(Module): diff --git a/norch/nn/module.py b/norch/nn/module.py index 88b408a..92cc0ca 100644 --- a/norch/nn/module.py +++ b/norch/nn/module.py @@ -1,4 +1,4 @@ -from parameter import Parameter +from .parameter import Parameter from collections import OrderedDict from abc import ABC import pickle @@ -78,4 +78,12 @@ class Module(ABC): return f'{string}\n)' def get_name(self): - return self.__class__.__name__ \ No newline at end of file + return self.__class__.__name__ + + def __setattr__(self, key, value): + self.__dict__[key] = value + + if isinstance(value, Module): + self._modules[key] = value + elif isinstance(value, Parameter): + self._params[key] = value \ No newline at end of file diff --git a/norch/nn/modules/__init__.py b/norch/nn/modules/__init__.py new file mode 100644 index 0000000..5c73a2e --- /dev/null +++ b/norch/nn/modules/__init__.py @@ -0,0 +1 @@ +from .linear import * \ No newline at end of file diff --git a/norch/nn/modules/__pycache__/__init__.cpython-38.pyc b/norch/nn/modules/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 0000000..1921846 Binary files /dev/null and b/norch/nn/modules/__pycache__/__init__.cpython-38.pyc differ diff --git a/norch/nn/modules/__pycache__/linear.cpython-38.pyc b/norch/nn/modules/__pycache__/linear.cpython-38.pyc new file mode 100644 index 0000000..3b211f6 Binary files /dev/null and b/norch/nn/modules/__pycache__/linear.cpython-38.pyc differ diff --git a/norch/nn/modules/linear.py b/norch/nn/modules/linear.py index 032670e..83ce02d 100644 --- a/norch/nn/modules/linear.py +++ b/norch/nn/modules/linear.py @@ -1,15 +1,16 @@ -from module import Module -from parameter import Parameter +from ..module import Module +from ..parameter import Parameter class Linear(Module): def __init__(self, input_dim, output_dim): super().__init__() self.input_dim = input_dim self.output_dim = output_dim - self.weight = Parameter(shape=[self.input_dim, self.output_dim]) + self.weight = Parameter(shape=[self.output_dim, self.input_dim]) self.bias = Parameter(shape=[self.output_dim, 1]) def forward(self, x): + print(self.weight.shape, x.shape, self.bias.shape, "@@@@@@\n\n\n\n") z = self.weight @ x + self.bias return z diff --git a/norch/nn/parameter.py b/norch/nn/parameter.py index 152d951..8203ba4 100644 --- a/norch/nn/parameter.py +++ b/norch/nn/parameter.py @@ -1,4 +1,5 @@ -from tensor import Tensor +from norch.tensor import Tensor +from norch.utils import utils import random class Parameter(Tensor): @@ -6,9 +7,6 @@ class Parameter(Tensor): A parameter is a trainable tensor. """ def __init__(self, shape): - data = [] - for dim_size in reversed(shape): - random_dim = [random.random() for _ in range(dim_size)] - data.insert(0, random_dim) + data = utils.generate_random_list(shape=shape) super().__init__(data, requires_grad=True) \ No newline at end of file diff --git a/norch/tensor.py b/norch/tensor.py index 91b6e2f..27ac305 100644 --- a/norch/tensor.py +++ b/norch/tensor.py @@ -571,4 +571,4 @@ class Tensor: result_data.ndim = self.ndim result_data.device = self.device - return result_data + return result_data \ No newline at end of file diff --git a/norch/utils/__pycache__/utils.cpython-38.pyc b/norch/utils/__pycache__/utils.cpython-38.pyc new file mode 100644 index 0000000..1abde9b Binary files /dev/null and b/norch/utils/__pycache__/utils.cpython-38.pyc differ diff --git a/norch/utils/utils.py b/norch/utils/utils.py new file mode 100644 index 0000000..2614a09 --- /dev/null +++ b/norch/utils/utils.py @@ -0,0 +1,15 @@ +import random + +def generate_random_list(shape): + """ + Generate a list with random numbers and shape 'shape' + """ + if len(shape) == 0: + return [] + else: + inner_shape = shape[1:] + if len(inner_shape) == 0: + return [random.random()] * shape[0] + else: + return [generate_random_list(inner_shape) for _ in range(shape[0])] + \ No newline at end of file diff --git a/test.py b/test.py index a490bd6..706ab10 100644 --- a/test.py +++ b/test.py @@ -67,19 +67,45 @@ if __name__ == "__main__": print(a.grad)""" + import norch.nn as nn + + class MeuModulo(nn.Module): + def __init__(self): + super(MeuModulo, self).__init__() + + self.layer1 = nn.Linear(10, 100) + self.layer2 = nn.Linear(100, 2) + self.sigmoid = nn.Sigmoid() + + def forward(self, x): + out = self.layer1(x) + out = self.layer2(out) + out = self.sigmoid(out) + + return out + + modelo = MeuModulo() + input_list = [[0.01 for _ in range(10)]] + input = norch.Tensor(input_list).T + output = modelo(input) + print(output) + + exit() + #### testar transpose axes!!!! make it contiguous - tensor1 = norch.Tensor([[[1, 2], [3, 4], [5, 6]], + """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, 30]]], requires_grad=True) + [[25, 26], [27, 28], [29, 0.030]]], requires_grad=True) - result = (-10) - tensor1 + op = nn.Sigmoid() + result = op(tensor1) result = result.sum() result.backward() print(tensor1.grad) - exit() + exit()""" # Reshape tensor1 to 2x3x5 reshaped_tensor = tensor1.transpose(1, 0)