pytinytensor 0.1.6__tar.gz → 0.1.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pytinytensor-0.1.6/pytinytensor.egg-info → pytinytensor-0.1.7}/PKG-INFO +3 -1
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/README.md +3 -1
- {pytinytensor-0.1.6 → pytinytensor-0.1.7/pytinytensor.egg-info}/PKG-INFO +3 -1
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/pytinytensor.egg-info/SOURCES.txt +2 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/setup.py +1 -1
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/nn/__init__.py +7 -4
- pytinytensor-0.1.7/tinytensor/nn/activations.py +68 -0
- pytinytensor-0.1.7/tinytensor/nn/embedding.py +33 -0
- pytinytensor-0.1.7/tinytensor/nn/losses.py +58 -0
- pytinytensor-0.1.7/tinytensor/nn/rnn.py +140 -0
- pytinytensor-0.1.6/tinytensor/nn/activations.py +0 -37
- pytinytensor-0.1.6/tinytensor/nn/losses.py +0 -12
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/LICENSE +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/pyproject.toml +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/pytinytensor.egg-info/dependency_links.txt +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/pytinytensor.egg-info/requires.txt +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/pytinytensor.egg-info/top_level.txt +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/setup.cfg +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tests/test_autograd.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tests/test_data.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tests/test_losses.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tests/test_nn.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tests/test_optim.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tests/test_tensor_math.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/__init__.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/backends/__init__.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/backends/cpu_numpy.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/backends/cuda_binding.cpp +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/backends/cuda_gpu.cu +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/backends/cuda_gpu.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/config.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/core/__init__.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/core/autograd.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/core/ops.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/core/tensor.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/data/__init__.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/data/dataloader.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/data/dataset.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/nn/batchnorm.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/nn/conv.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/nn/dropout.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/nn/flatten.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/nn/functional.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/nn/linear.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/nn/modules.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/nn/pooling.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/nn/utils.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/optim/__init__.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/optim/lr_scheduler.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/optim/optimizer.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/utils/__init__.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/utils/bar.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/utils/early_stopping.py +0 -0
- {pytinytensor-0.1.6 → pytinytensor-0.1.7}/tinytensor/utils/summary.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pytinytensor
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.7
|
|
4
4
|
Summary: мини ИИ фреймворк от IbrokimN ( github/IbrokhimN )
|
|
5
5
|
Home-page: https://github.com/IbrokhimN/tinytensor
|
|
6
6
|
Author: IbrokhimN
|
|
@@ -23,6 +23,8 @@ Dynamic: summary
|
|
|
23
23
|
|
|
24
24
|
# tinytensor
|
|
25
25
|
|
|
26
|
+
Новые доки напишу в след версии
|
|
27
|
+
|
|
26
28
|
Маленький самописный autograd на numpy. Тензоры, backprop, пара слоев,
|
|
27
29
|
лоссы, оптимизаторы и даталоадер. По сути свой мини-pytorch, только без
|
|
28
30
|
плюшек и без cuda (пока).
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# tinytensor
|
|
2
2
|
|
|
3
|
+
Новые доки напишу в след версии
|
|
4
|
+
|
|
3
5
|
Маленький самописный autograd на numpy. Тензоры, backprop, пара слоев,
|
|
4
6
|
лоссы, оптимизаторы и даталоадер. По сути свой мини-pytorch, только без
|
|
5
7
|
плюшек и без cuda (пока).
|
|
@@ -215,4 +217,4 @@ tinytensor/
|
|
|
215
217
|
- [Loshchilov & Hutter - AdamW](https://arxiv.org/abs/1711.05101)
|
|
216
218
|
- [Hendrycks & Gimpel - GELU](https://arxiv.org/abs/1606.08415)
|
|
217
219
|
- [PyTorch - torch.nn.Module](https://pytorch.org/docs/stable/generated/torch.nn.Module.html)
|
|
218
|
-
- [PyTorch - torch.utils.data.DataLoader](https://pytorch.org/docs/stable/data.html)
|
|
220
|
+
- [PyTorch - torch.utils.data.DataLoader](https://pytorch.org/docs/stable/data.html)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pytinytensor
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.7
|
|
4
4
|
Summary: мини ИИ фреймворк от IbrokimN ( github/IbrokhimN )
|
|
5
5
|
Home-page: https://github.com/IbrokhimN/tinytensor
|
|
6
6
|
Author: IbrokhimN
|
|
@@ -23,6 +23,8 @@ Dynamic: summary
|
|
|
23
23
|
|
|
24
24
|
# tinytensor
|
|
25
25
|
|
|
26
|
+
Новые доки напишу в след версии
|
|
27
|
+
|
|
26
28
|
Маленький самописный autograd на numpy. Тензоры, backprop, пара слоев,
|
|
27
29
|
лоссы, оптимизаторы и даталоадер. По сути свой мини-pytorch, только без
|
|
28
30
|
плюшек и без cuda (пока).
|
|
@@ -32,12 +32,14 @@ tinytensor/nn/activations.py
|
|
|
32
32
|
tinytensor/nn/batchnorm.py
|
|
33
33
|
tinytensor/nn/conv.py
|
|
34
34
|
tinytensor/nn/dropout.py
|
|
35
|
+
tinytensor/nn/embedding.py
|
|
35
36
|
tinytensor/nn/flatten.py
|
|
36
37
|
tinytensor/nn/functional.py
|
|
37
38
|
tinytensor/nn/linear.py
|
|
38
39
|
tinytensor/nn/losses.py
|
|
39
40
|
tinytensor/nn/modules.py
|
|
40
41
|
tinytensor/nn/pooling.py
|
|
42
|
+
tinytensor/nn/rnn.py
|
|
41
43
|
tinytensor/nn/utils.py
|
|
42
44
|
tinytensor/optim/__init__.py
|
|
43
45
|
tinytensor/optim/lr_scheduler.py
|
|
@@ -4,13 +4,16 @@ from tinytensor.nn.conv import Conv2d
|
|
|
4
4
|
from tinytensor.nn.pooling import MaxPool2d, AvgPool2d
|
|
5
5
|
from tinytensor.nn.flatten import Flatten
|
|
6
6
|
from tinytensor.nn.batchnorm import BatchNorm2d
|
|
7
|
-
from tinytensor.nn.
|
|
7
|
+
from tinytensor.nn.embedding import Embedding
|
|
8
|
+
from tinytensor.nn.rnn import RNNCell, RNN
|
|
9
|
+
from tinytensor.nn.activations import ReLU, LeReLU, Sigmod, Tanh, GELU, Softmax
|
|
8
10
|
from tinytensor.nn.dropout import Dropout
|
|
9
|
-
from tinytensor.nn.losses import MSELoss
|
|
11
|
+
from tinytensor.nn.losses import MSELoss, CrossEntropyLoss
|
|
10
12
|
|
|
11
13
|
__all__ = [
|
|
12
14
|
"Module", "Sequential",
|
|
13
15
|
"Linear", "Conv2d", "MaxPool2d", "AvgPool2d", "Flatten", "BatchNorm2d",
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
+
"Embedding", "RNNCell", "RNN",
|
|
17
|
+
"ReLU", "LeReLU", "Sigmod", "Tanh", "GELU", "Softmax",
|
|
18
|
+
"Dropout", "MSELoss", "CrossEntropyLoss"
|
|
16
19
|
]
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from tinytensor.core.tensor import Tensor
|
|
4
|
+
from tinytensor.nn.modules import Module
|
|
5
|
+
|
|
6
|
+
class ReLU(Module):
|
|
7
|
+
def forward(self, x):
|
|
8
|
+
return x.relu()
|
|
9
|
+
|
|
10
|
+
class LeReLU(Module):
|
|
11
|
+
# у нас есть тут альфа
|
|
12
|
+
def __init__(self, alpha=0.01):
|
|
13
|
+
super().__init__()
|
|
14
|
+
self.alpha = alpha
|
|
15
|
+
|
|
16
|
+
def forward(self, x):
|
|
17
|
+
return x.leaky_relu(alpha=self.alpha)
|
|
18
|
+
|
|
19
|
+
class Sigmod(Module):
|
|
20
|
+
def forward(self, x):
|
|
21
|
+
return x.sigmoid()
|
|
22
|
+
|
|
23
|
+
class Tanh(Module):
|
|
24
|
+
def forward(self, x):
|
|
25
|
+
return x.tanh()
|
|
26
|
+
|
|
27
|
+
class GELU(Module):
|
|
28
|
+
def forward(self, x):
|
|
29
|
+
return x.gelu()
|
|
30
|
+
|
|
31
|
+
class Softmax(Module):
|
|
32
|
+
def __init__(self, dim=1):
|
|
33
|
+
super().__init__()
|
|
34
|
+
self.dim = dim
|
|
35
|
+
|
|
36
|
+
def forward(self, x):
|
|
37
|
+
max_x = np.max(x.data, axis=self.dim, keepdims=True)
|
|
38
|
+
exp_x = np.exp(x.data - max_x)
|
|
39
|
+
|
|
40
|
+
out_data = exp_x / np.sum(exp_x, axis=self.dim, keepdims=True)
|
|
41
|
+
|
|
42
|
+
out = Tensor(out_data, requires_grad=x.requires_grad, device=x.device)
|
|
43
|
+
|
|
44
|
+
if out.requires_grad:
|
|
45
|
+
out._prev = {x}
|
|
46
|
+
|
|
47
|
+
def _backward():
|
|
48
|
+
dy = out.grad
|
|
49
|
+
dot = np.sum(dy * out_data, axis=self.dim, keepdims=True)
|
|
50
|
+
dx = out_data * (dy - dot)
|
|
51
|
+
|
|
52
|
+
if x.grad is None:
|
|
53
|
+
x.grad = np.zeros_like(x.data, dtype=np.float32)
|
|
54
|
+
x.grad += dx
|
|
55
|
+
|
|
56
|
+
out._backward = _backward
|
|
57
|
+
|
|
58
|
+
return out
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
# ToDo:
|
|
63
|
+
# ReLU [x]
|
|
64
|
+
# LeReLU [x]
|
|
65
|
+
# Sigmoid [x]
|
|
66
|
+
# Tanh [x]
|
|
67
|
+
# GELU [x]
|
|
68
|
+
# kama soska
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from tinytensor.nn.modules import Module
|
|
4
|
+
from tinytensor.core.tensor import Tensor
|
|
5
|
+
|
|
6
|
+
class Embedding(Module):
|
|
7
|
+
def __init__(self, num_embeddings: int, dim_embeddings:int ):
|
|
8
|
+
super().__init__()
|
|
9
|
+
self.num_embeddings = num_embeddings
|
|
10
|
+
self.dim_embeddings = dim_embeddings
|
|
11
|
+
|
|
12
|
+
weight_data = np.random.randn(num_embeddings, dim_embeddings).astype(np.float32) * 0.01
|
|
13
|
+
|
|
14
|
+
self.weight = Tensor(weight_data, requires_grad=True)
|
|
15
|
+
|
|
16
|
+
def forward(self, x):
|
|
17
|
+
#x эт тензор с целыми числами
|
|
18
|
+
indices = x.data.astype(np.int64)
|
|
19
|
+
out_data = self.weight.data[indices]
|
|
20
|
+
|
|
21
|
+
out = Tensor(out_data, requires_grad=self.weight.requires_grad, device=self.weight.device)
|
|
22
|
+
|
|
23
|
+
if out.requires_grad:
|
|
24
|
+
out._prev = {self.weight}
|
|
25
|
+
|
|
26
|
+
def _backward():
|
|
27
|
+
if self.weight.grad is None:
|
|
28
|
+
self.weight.grad = np.zeros_like(self.weight.data, dtype=np.float32)
|
|
29
|
+
np.add.at(self.weight.grad, indices, out.grad)
|
|
30
|
+
|
|
31
|
+
out._backward = _backward
|
|
32
|
+
|
|
33
|
+
return out
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from tinytensor.core.tensor import Tensor
|
|
3
|
+
from tinytensor.nn.modules import Module
|
|
4
|
+
# 1/n * sum((n, i = 1), yi - y`i)^2
|
|
5
|
+
class MSELoss(Module):
|
|
6
|
+
def forward(self, y_pred, y_true):
|
|
7
|
+
diff = y_pred - y_true
|
|
8
|
+
sq_diff = diff ** 2
|
|
9
|
+
return sq_diff.sum() * (1.0/y_pred.data.size)
|
|
10
|
+
|
|
11
|
+
# -log((e-xtar - M)/(sum(exj-M)))=-(xt-M)+log sum(exj-M)
|
|
12
|
+
class CrossEntropyLoss(Module):
|
|
13
|
+
def __init__(self, eps=1e-12):
|
|
14
|
+
super().__init__()
|
|
15
|
+
self.eps = eps
|
|
16
|
+
|
|
17
|
+
def forward(self, logits, targets):
|
|
18
|
+
x = logits.data
|
|
19
|
+
batch_size = x.shape[0]
|
|
20
|
+
|
|
21
|
+
max_logits = np.max(x, axis=1, keepdims=True)
|
|
22
|
+
shifted_logits = x - max_logits
|
|
23
|
+
|
|
24
|
+
#знаменатель софтмакс
|
|
25
|
+
exp_shifted = np.exp(shifted_logits)
|
|
26
|
+
sums_exp = np.sum(exp_shifted, axis=1, keepdims=True)
|
|
27
|
+
softmax = exp_shifted / sums_exp
|
|
28
|
+
|
|
29
|
+
#подсчет лог
|
|
30
|
+
#эпсилон чтоб на 0 не поделилось случайно
|
|
31
|
+
log_softmax = shifted_logits - np.log(sums_exp + self.eps)
|
|
32
|
+
|
|
33
|
+
if targets.data.ndim == 1 or targets.data.shape[1] == 1:
|
|
34
|
+
target_indices = targets.data.astype(np.int64).ravel()
|
|
35
|
+
correct_log_probs = log_softmax[np.arange(batch_size), target_indices]
|
|
36
|
+
one_hot = np.zeros_like(x)
|
|
37
|
+
one_hot[np.arange(batch_size), target_indices] = 1.0
|
|
38
|
+
|
|
39
|
+
else:
|
|
40
|
+
correct_log_probs = np.sum(log_softmax * targets.data, axis=1)
|
|
41
|
+
one_hot = targets.data
|
|
42
|
+
|
|
43
|
+
loss = -np.mean(correct_log_probs)
|
|
44
|
+
|
|
45
|
+
out = Tensor(loss, requires_grad=logits.requires_grad, device=logits.device)
|
|
46
|
+
|
|
47
|
+
if out.requires_grad:
|
|
48
|
+
out._prev = {logits}
|
|
49
|
+
|
|
50
|
+
def _backward():
|
|
51
|
+
if logits.grad is None:
|
|
52
|
+
logits.grad = np.zeros_like(logits.data, dtype=np.float32)
|
|
53
|
+
logits.grad += (softmax - one_hot) / batch_size * out.grad
|
|
54
|
+
|
|
55
|
+
out._backward = _backward
|
|
56
|
+
|
|
57
|
+
return out
|
|
58
|
+
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from tinytensor.core.tensor import Tensor
|
|
3
|
+
from tinytensor.nn.modules import Module
|
|
4
|
+
|
|
5
|
+
class RNNCell(Module):
|
|
6
|
+
def __init__(self,input_size: int, hidden_size: int):
|
|
7
|
+
super().__init__()
|
|
8
|
+
self.input_size = input_size
|
|
9
|
+
self.hidden_size = hidden_size
|
|
10
|
+
|
|
11
|
+
std = 1.0 / np.sqrt(hidden_size)
|
|
12
|
+
w_ih = np.random.uniform(-std, std, (hidden_size, input_size)).astype(np.float32)
|
|
13
|
+
b_ih = np.zeros((hidden_size,), dtype=np.float32)
|
|
14
|
+
|
|
15
|
+
#веса скрытог состояния
|
|
16
|
+
w_hh = np.random.uniform(-std, std, (hidden_size, hidden_size)).astype(np.float32)
|
|
17
|
+
b_hh = np.zeros((hidden_size,), dtype=np.float32)
|
|
18
|
+
|
|
19
|
+
self.weight_ih = Tensor(w_ih, requires_grad=True)
|
|
20
|
+
self.bias_ih = Tensor(b_ih, requires_grad=True)
|
|
21
|
+
|
|
22
|
+
self.weight_hh = Tensor(w_hh, requires_grad=True)
|
|
23
|
+
self.bias_hh = Tensor(b_hh, requires_grad=True)
|
|
24
|
+
|
|
25
|
+
def forward(self, x, h=None):
|
|
26
|
+
batch_size = x.data.shape[0]
|
|
27
|
+
|
|
28
|
+
if h is None:
|
|
29
|
+
h = Tensor(np.zeros((batch_size, self.hidden_size), dtype=np.float32))
|
|
30
|
+
|
|
31
|
+
# h_n=tanh(x@W_ih.T+ b_ih+h@ W_hh.T+b_hh)
|
|
32
|
+
gate_x = x.data @ self.weight_ih.data.T + self.bias_ih.data
|
|
33
|
+
gate_h = h.data @ self.weight_hh.data.T + self.bias_hh.data
|
|
34
|
+
|
|
35
|
+
h_next_data = np.tanh(gate_x + gate_h)
|
|
36
|
+
|
|
37
|
+
out = Tensor(
|
|
38
|
+
h_next_data,
|
|
39
|
+
requires_grad=(
|
|
40
|
+
x.requires_grad or h.requires_grad
|
|
41
|
+
or self.weight_ih.requires_grad or self.bias_ih.requires_grad
|
|
42
|
+
or self.weight_hh.requires_grad or self.bias_hh.requires_grad
|
|
43
|
+
),
|
|
44
|
+
device=x.device,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
if out.requires_grad:
|
|
48
|
+
out._prev = {x, h, self.weight_ih, self.bias_ih, self.weight_hh, self.bias_hh}
|
|
49
|
+
|
|
50
|
+
def _backward():
|
|
51
|
+
dgate = out.grad * (1.0 - h_next_data ** 2)
|
|
52
|
+
|
|
53
|
+
if self.weight_ih.requires_grad:
|
|
54
|
+
if self.weight_ih.grad is None:
|
|
55
|
+
self.weight_ih.grad = np.zeros_like(self.weight_ih.data, dtype=np.float32)
|
|
56
|
+
self.weight_ih.grad += dgate.T @ x.data
|
|
57
|
+
|
|
58
|
+
if self.bias_ih.requires_grad:
|
|
59
|
+
if self.bias_ih.grad is None:
|
|
60
|
+
self.bias_ih.grad = np.zeros_like(self.bias_ih.data, dtype=np.float32)
|
|
61
|
+
self.bias_ih.grad += np.sum(dgate, axis=0)
|
|
62
|
+
|
|
63
|
+
if self.weight_hh.requires_grad:
|
|
64
|
+
if self.weight_hh.grad is None:
|
|
65
|
+
self.weight_hh.grad = np.zeros_like(self.weight_hh.data, dtype=np.float32)
|
|
66
|
+
self.weight_hh.grad += dgate.T @ h.data
|
|
67
|
+
|
|
68
|
+
if self.bias_hh.requires_grad:
|
|
69
|
+
if self.bias_hh.grad is None:
|
|
70
|
+
self.bias_hh.grad = np.zeros_like(self.bias_hh.data, dtype=np.float32)
|
|
71
|
+
self.bias_hh.grad += np.sum(dgate, axis=0)
|
|
72
|
+
|
|
73
|
+
if x.requires_grad:
|
|
74
|
+
if x.grad is None:
|
|
75
|
+
x.grad = np.zeros_like(x.data, dtype=np.float32)
|
|
76
|
+
x.grad += dgate @ self.weight_ih.data
|
|
77
|
+
|
|
78
|
+
if h.requires_grad:
|
|
79
|
+
if h.grad is None:
|
|
80
|
+
h.grad = np.zeros_like(h.data, dtype=np.float32)
|
|
81
|
+
h.grad += dgate @ self.weight_hh.data
|
|
82
|
+
|
|
83
|
+
out._backward = _backward
|
|
84
|
+
|
|
85
|
+
return out
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class RNN(Module):
|
|
89
|
+
def __init__(self, input_size: int, hidden_size: int):
|
|
90
|
+
super().__init__()
|
|
91
|
+
self.cell = RNNCell(input_size, hidden_size)
|
|
92
|
+
|
|
93
|
+
def forward(self, x, h_0=None):
|
|
94
|
+
#x.shape->(batch sSize, Seq len,input size)
|
|
95
|
+
batch_size, seq_len, _ = x.data.shape
|
|
96
|
+
|
|
97
|
+
h = h_0
|
|
98
|
+
outputs = []
|
|
99
|
+
|
|
100
|
+
#по циклу по времени т
|
|
101
|
+
for t in range(seq_len):
|
|
102
|
+
x_t = Tensor(x.data[:, t, :], requires_grad=x.requires_grad)
|
|
103
|
+
|
|
104
|
+
if x.requires_grad:
|
|
105
|
+
x_t._prev = {x}
|
|
106
|
+
|
|
107
|
+
def make_backward(t, x_t):
|
|
108
|
+
def _backward():
|
|
109
|
+
if x.grad is None:
|
|
110
|
+
x.grad = np.zeros_like(x.data, dtype=np.float32)
|
|
111
|
+
x.grad[:, t, :] += x_t.grad
|
|
112
|
+
return _backward
|
|
113
|
+
|
|
114
|
+
x_t._backward = make_backward(t, x_t)
|
|
115
|
+
|
|
116
|
+
h = self.cell(x_t, h)
|
|
117
|
+
outputs.append(h)
|
|
118
|
+
|
|
119
|
+
#сборка
|
|
120
|
+
out_data = np.stack([o.data for o in outputs], axis=1)
|
|
121
|
+
|
|
122
|
+
out = Tensor(
|
|
123
|
+
out_data,
|
|
124
|
+
requires_grad=any(o.requires_grad for o in outputs),
|
|
125
|
+
device=x.device,
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
if out.requires_grad:
|
|
129
|
+
out._prev = set(outputs)
|
|
130
|
+
|
|
131
|
+
def _backward():
|
|
132
|
+
for t, o in enumerate(outputs):
|
|
133
|
+
if o.requires_grad:
|
|
134
|
+
if o.grad is None:
|
|
135
|
+
o.grad = np.zeros_like(o.data, dtype=np.float32)
|
|
136
|
+
o.grad += out.grad[:, t, :]
|
|
137
|
+
|
|
138
|
+
out._backward = _backward
|
|
139
|
+
|
|
140
|
+
return out, h
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
from tinytensor.nn.modules import Module
|
|
2
|
-
|
|
3
|
-
class ReLU(Module):
|
|
4
|
-
def forward(self, x):
|
|
5
|
-
return x.relu()
|
|
6
|
-
|
|
7
|
-
class LeReLU(Module):
|
|
8
|
-
# у нас есть тут альфа
|
|
9
|
-
def __init__(self, alpha=0.01):
|
|
10
|
-
super().__init__()
|
|
11
|
-
self.alpha = alpha
|
|
12
|
-
|
|
13
|
-
def forward(self, x):
|
|
14
|
-
return x.leaky_relu(alpha=self.alpha)
|
|
15
|
-
|
|
16
|
-
class Sigmod(Module):
|
|
17
|
-
def forward(self, x):
|
|
18
|
-
return x.sigmoid()
|
|
19
|
-
|
|
20
|
-
class Tanh(Module):
|
|
21
|
-
def forward(self, x):
|
|
22
|
-
return x.tanh()
|
|
23
|
-
|
|
24
|
-
class GELU(Module):
|
|
25
|
-
def forward(self, x):
|
|
26
|
-
return x.gelu()
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
# ToDo:
|
|
32
|
-
# ReLU [x]
|
|
33
|
-
# LeReLU [x]
|
|
34
|
-
# Sigmoid [x]
|
|
35
|
-
# Tanh [x]
|
|
36
|
-
# GELU [x]
|
|
37
|
-
# kama soska
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import numpy as np
|
|
2
|
-
from tinytensor.nn.modules import Module
|
|
3
|
-
# 1/n * sum((n, i = 1), yi - y`i)^2
|
|
4
|
-
class MSELoss(Module):
|
|
5
|
-
def forward(self, y_pred, y_true):
|
|
6
|
-
diff = y_pred - y_true
|
|
7
|
-
sq_diff = diff ** 2
|
|
8
|
-
return sq_diff.sum() * (1.0/y_pred.data.size)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
# мб в будущем добавлю cross entropy loss
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|