Fix nn.Module linear
This commit is contained in:
parent
6445563268
commit
1e00930c4e
17 changed files with 66 additions and 17 deletions
Binary file not shown.
|
|
@ -1,2 +1,2 @@
|
|||
from .modules import *
|
||||
from .activations import *
|
||||
from .activation import *
|
||||
BIN
norch/nn/__pycache__/__init__.cpython-38.pyc
Normal file
BIN
norch/nn/__pycache__/__init__.cpython-38.pyc
Normal file
Binary file not shown.
BIN
norch/nn/__pycache__/activation.cpython-38.pyc
Normal file
BIN
norch/nn/__pycache__/activation.cpython-38.pyc
Normal file
Binary file not shown.
BIN
norch/nn/__pycache__/module.cpython-38.pyc
Normal file
BIN
norch/nn/__pycache__/module.cpython-38.pyc
Normal file
Binary file not shown.
BIN
norch/nn/__pycache__/parameter.cpython-38.pyc
Normal file
BIN
norch/nn/__pycache__/parameter.cpython-38.pyc
Normal file
Binary file not shown.
|
|
@ -1,4 +1,4 @@
|
|||
from module import Module
|
||||
from .module import Module
|
||||
import math
|
||||
|
||||
class Activation(Module):
|
||||
|
|
|
|||
|
|
@ -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__
|
||||
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
|
||||
1
norch/nn/modules/__init__.py
Normal file
1
norch/nn/modules/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from .linear import *
|
||||
BIN
norch/nn/modules/__pycache__/__init__.cpython-38.pyc
Normal file
BIN
norch/nn/modules/__pycache__/__init__.cpython-38.pyc
Normal file
Binary file not shown.
BIN
norch/nn/modules/__pycache__/linear.cpython-38.pyc
Normal file
BIN
norch/nn/modules/__pycache__/linear.cpython-38.pyc
Normal file
Binary file not shown.
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
@ -571,4 +571,4 @@ class Tensor:
|
|||
result_data.ndim = self.ndim
|
||||
result_data.device = self.device
|
||||
|
||||
return result_data
|
||||
return result_data
|
||||
BIN
norch/utils/__pycache__/utils.cpython-38.pyc
Normal file
BIN
norch/utils/__pycache__/utils.cpython-38.pyc
Normal file
Binary file not shown.
15
norch/utils/utils.py
Normal file
15
norch/utils/utils.py
Normal file
|
|
@ -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])]
|
||||
|
||||
34
test.py
34
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue